Android 如何保存位图 - 有错误的代码
我正在尝试序列化一个包含位图变量的类。这是有点工作的代码。我需要帮助来找出问题所在。
private Bitmap myVideoScreenshotBm;
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeInt(myVideoScreenshotBm.getRowBytes());
out.writeInt(myVideoScreenshotBm.getHeight());
out.writeInt(myVideoScreenshotBm.getWidth());
int bmSize = myVideoScreenshotBm.getHeight() * myVideoScreenshotBm.getRowBytes();
ByteBuffer dst= ByteBuffer.allocate(bmSize);
myVideoScreenshotBm.copyPixelsToBuffer(dst);
byte[] bytesar=new byte[bmSize];
dst.position(0);
dst.get(bytesar);
out.write(bytesar);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
int nbRowBytes=in.readInt();
int height=in.readInt();
int width=in.readInt();
//
int bmSize = nbRowBytes * height;
byte[] toread= new byte[bmSize];
in.read(toread, 0, toread.length);
ByteBuffer dst= ByteBuffer.allocate(bmSize);
dst.put(toread);
dst.position(0);
myVideoScreenshotBm=Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
myVideoScreenshotBm.copyPixelsFromBuffer(dst);
}
我没有收到错误,但我收到的位图是错误的。另外,我不知道如何知道哪个 Bitmap.Config 标志合适。我怎么知道?
I'm trying to serialize a class in which I have a bitmap variable. Here is the code that is a bit working. I need help to find out what is still wrong.
private Bitmap myVideoScreenshotBm;
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeInt(myVideoScreenshotBm.getRowBytes());
out.writeInt(myVideoScreenshotBm.getHeight());
out.writeInt(myVideoScreenshotBm.getWidth());
int bmSize = myVideoScreenshotBm.getHeight() * myVideoScreenshotBm.getRowBytes();
ByteBuffer dst= ByteBuffer.allocate(bmSize);
myVideoScreenshotBm.copyPixelsToBuffer(dst);
byte[] bytesar=new byte[bmSize];
dst.position(0);
dst.get(bytesar);
out.write(bytesar);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
int nbRowBytes=in.readInt();
int height=in.readInt();
int width=in.readInt();
//
int bmSize = nbRowBytes * height;
byte[] toread= new byte[bmSize];
in.read(toread, 0, toread.length);
ByteBuffer dst= ByteBuffer.allocate(bmSize);
dst.put(toread);
dst.position(0);
myVideoScreenshotBm=Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
myVideoScreenshotBm.copyPixelsFromBuffer(dst);
}
I'm not getting an error, but the bitmaps I'm getting are wrong. Also, I do not know how to know which Bitmap.Config flag is suitable. How can I know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是具有内存优化的序列化代码。我使用的静态缓冲区正在增长到最大位图大小,并且每次都会重复使用。
}
Here is the code for a serialization with memory optimisation. Im using a static buffer that is growing to the biggest bitmap size and that I reuse each time.
}
不需要冗余数组+所有逻辑+使用给定的方法来操作缓冲区+utf对于字符串更好并避免强制转换+同步(但无论如何它都不是线程安全的):
No need to have the redundant array + all the logic around + use the given methods to manipulate the buffer + utf is better for strings and avoid the casts + synchronization (but it is not thread safe anyway) :
为了简化事情,您可以对位图以外的所有字段使用标准序列化。只需将位图标记为瞬态,然后使用 out.defaultWriteObject();和 in.defaultReadObject();。这确实清理了代码:
To simplify things, you can use standard serialization for all fields other than the bitmap. Just mark the bitmap as transient, and use out.defaultWriteObject(); and in.defaultReadObject();. This really cleans up the code: