android如何保存位图 – buggy代码

栏目: IOS · Android · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/3628016/android-how-to-save-a-bitmap-buggy-code

我试图序列化一个类,我有一个位图变量.这是有点工作的代码….我需要帮助找出仍然有问题的…..

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标志是合适的…怎么知道?

任何帮助?

以下是带内存优化的序列化代码.我使用的静态缓冲区正在增长到最大的位图大小,并且我每次都重复使用.

public class Video implements Serializable{
public long videoId;
public String title;
public String publisher;
public String language;
public Date lastModified;
public Date published;
public String imageUrl;
public String url;
public Bitmap myVideoScreenshotBm;
public Date expireTime;
//public Drawable myVideoScreenshotDrawable;

private static ByteBuffer dst;
private static byte[] bytesar;

public Video (long newVideoId) {
    this.videoId=newVideoId;
}
private void writeObject(ObjectOutputStream out) throws IOException{

    out.writeLong(videoId);

    out.writeObject(title);
    out.writeObject(publisher);
    out.writeObject(language);
    out.writeObject(lastModified);
    out.writeObject(published);
    out.writeObject(expireTime);

    out.writeObject(imageUrl);
    out.writeObject(url);


    out.writeInt(myVideoScreenshotBm.getRowBytes());
    out.writeInt(myVideoScreenshotBm.getHeight());
    out.writeInt(myVideoScreenshotBm.getWidth());

    int bmSize = myVideoScreenshotBm.getRowBytes() * myVideoScreenshotBm.getHeight();
    if(dst==null || bmSize > dst.capacity())
        dst= ByteBuffer.allocate(bmSize);

    out.writeInt(dst.capacity());

    dst.position(0);

    myVideoScreenshotBm.copyPixelsToBuffer(dst);
    if(bytesar==null || bmSize > bytesar.length)
        bytesar=new byte[bmSize];

    dst.position(0);
    dst.get(bytesar);


    out.write(bytesar, 0, bytesar.length);

}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{

    videoId=in.readLong();

    title=(String) in.readObject();
    publisher=(String) in.readObject();
    language=(String) in.readObject();
    lastModified=(Date) in.readObject();
    published=(Date) in.readObject();
    expireTime=(Date) in.readObject();

    imageUrl = (String) in.readObject();
    url = (String) in.readObject();


    int nbRowBytes=in.readInt();
    int height=in.readInt();
    int width=in.readInt();

    int bmSize=in.readInt();



    if(bytesar==null || bmSize > bytesar.length)
        bytesar= new byte[bmSize];


    int offset=0;

    while(in.available()>0){
        offset=offset + in.read(bytesar, offset, in.available());
    }


    if(dst==null || bmSize > dst.capacity())
        dst= ByteBuffer.allocate(bmSize);
    dst.position(0);
    dst.put(bytesar);
    dst.position(0);
    myVideoScreenshotBm=Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    myVideoScreenshotBm.copyPixelsFromBuffer(dst);
    //in.close();
}

}

翻译自:https://stackoverflow.com/questions/3628016/android-how-to-save-a-bitmap-buggy-code


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法霸权

算法霸权

[美] 凯西·奥尼尔 / 马青玲 / 中信出版集团 / 2018-9-1 / 69.00元

数据科学家凯西•奥尼尔认为,我们应该警惕不断渗透和深入我们生活的数学模型——它们的存在,很有可能威胁到我们的社会结构。 我们生活在一个依赖“算法”的时代,它对我们生活的影响越来越大,我们去哪里上学,我是不是应该贷款买车,我们应该花多少钱来买健康保险,这些都不是由人来决定的,而是由大数据模型来决定的。从理论上来说,这一模型应该让社会更加公平,每一个人的衡量标准都是一样的,偏见是不存在的。 ......一起来看看 《算法霸权》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具