栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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;}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);}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.depreByteArray(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);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486053.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号