gpt4 book ai didi

java - 在 Java 中序列化和反序列化 android.graphics.Bitmap

转载 作者:搜寻专家 更新时间:2023-10-30 21:17:08 25 4
gpt4 key购买 nike

我已经开始开发我的第一个 Android 应用程序,并且具有处理多层图像的应用程序的基础。我可以将项目文件的平面版本导出为 PNG,但我希望能够保存分层图像以供以后编辑(包括应用于特定图层的任何选项,例如基于文本的图层)。

无论如何,我已确保需要写入文件的类是“可序列化的”,但由于 android.graphics.Bitmap 不可序列化这一事实而遇到了一些障碍。以下代码实质上将位图作为 PNG 格式输出到 ByteArray 中,并且应该将其作为“readObject”的一部分读回。但是,当代码运行时 - 我可以验证读入的“imageByteArrayLength”变量与输出的变量相同 - 但“位图图像”始终为空。

如有任何帮助,我们将不胜感激。感谢阅读。

private String title;
private int width;
private int height;
private Bitmap sourceImage;
private Canvas sourceCanvas;
private Bitmap currentImage;
private Canvas currentCanvas;
private Paint currentPaint;

private void writeObject(ObjectOutputStream out) throws IOException{
out.writeObject(title);
out.writeInt(width);
out.writeInt(height);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
currentImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageByteArray = stream.toByteArray();

int length = imageByteArray.length;
out.writeInt(length);
out.write(imageByteArray);
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
this.title = (String)in.readObject();
this.width = in.readInt();
this.height = in.readInt();

int imageByteArrayLength = in.readInt();
byte[] imageByteArray = new byte[imageByteArrayLength];
in.read(imageByteArray, 0, imageByteArrayLength);

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;

Bitmap image = BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArrayLength, opt);

sourceImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
currentImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

sourceCanvas = new Canvas(sourceImage);
currentCanvas = new Canvas(currentImage);
currentPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

if ( image != null ) {
sourceCanvas.drawBitmap(image, 0, 0, currentPaint);
}
}

最佳答案

花了一段时间,但我找到了解决这个问题的干净方法。我生成了一个自定义对象 (BitmapDataObject),它实现了 Serializable 并且有一个 byte[] 来存储来自原始位图的 PNG 数据。使用它,数据被正确地存储在 ObjectOutputStream/ObjectInputStream - 这有效地允许一个人通过将它作为 PNG 存储在自定义对象的 byte[] 中来序列化和反序列化位图对象。下面的代码解决了我的查询。

private String title;
private int sourceWidth, currentWidth;
private int sourceHeight, currentHeight;
private Bitmap sourceImage;
private Canvas sourceCanvas;
private Bitmap currentImage;
private Canvas currentCanvas;
private Paint currentPaint;

protected class BitmapDataObject implements Serializable {
private static final long serialVersionUID = 111696345129311948L;
public byte[] imageByteArray;
}

/** Included for serialization - write this layer to the output stream. */
private void writeObject(ObjectOutputStream out) throws IOException{
out.writeObject(title);
out.writeInt(currentWidth);
out.writeInt(currentHeight);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
currentImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
BitmapDataObject bitmapDataObject = new BitmapDataObject();
bitmapDataObject.imageByteArray = stream.toByteArray();

out.writeObject(bitmapDataObject);
}

/** Included for serialization - read this object from the supplied input stream. */
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
title = (String)in.readObject();
sourceWidth = currentWidth = in.readInt();
sourceHeight = currentHeight = in.readInt();

BitmapDataObject bitmapDataObject = (BitmapDataObject)in.readObject();
Bitmap image = BitmapFactory.decodeByteArray(bitmapDataObject.imageByteArray, 0, bitmapDataObject.imageByteArray.length);

sourceImage = Bitmap.createBitmap(sourceWidth, sourceHeight, Bitmap.Config.ARGB_8888);
currentImage = Bitmap.createBitmap(sourceWidth, sourceHeight, Bitmap.Config.ARGB_8888);

sourceCanvas = new Canvas(sourceImage);
currentCanvas = new Canvas(currentImage);

currentPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
thumbnailPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
thumbnailPaint.setARGB(255, 200, 200, 200);
thumbnailPaint.setStyle(Paint.Style.FILL);
}

关于java - 在 Java 中序列化和反序列化 android.graphics.Bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com