解码字节数组和复制像素到缓冲区不起作用。SkImageDecoder::Factory 返回 null。

8

我有一个实现了Serializable接口的TouchPoint类,因为它包含Bitmap,所以我为该类编写了writeObject和readObject方法:

private void writeObject(ObjectOutputStream oos) throws IOException {
    long t1 = System.currentTimeMillis();
    oos.defaultWriteObject();
    if(_bmp!=null){
        int bytes = _bmp.getWidth()*_bmp.getHeight()*4;

        ByteBuffer buffer = ByteBuffer.allocate(bytes); 
        _bmp.copyPixelsToBuffer(buffer);

        byte[] array = buffer.array();      

        oos.writeObject(array);

    }
    Log.v("PaintFX","Elapsed Time: "+(System.currentTimeMillis()-t1));
}

private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
    ois.defaultReadObject();
    byte[] data = (byte[]) ois.readObject();
    if(data != null && data.length > 0){
        _bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
    }
}

问题是我遇到了以下错误:

SkImageDecoder::Factory 返回 null

那么我该如何解决呢?我知道可能的解决方案是将 writeObject() 改为:
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
oos.writeObject(byteStream.toByteArray);

但是这种方法几乎慢了10倍以上。

  • copyPixelsToBuffer 写入图像大约需要14毫秒
  • _bmp.compress 大约需要160毫秒

更新发现实际问题是在此之后。

buffer.array();

所有 byte[] 数组元素都是:0。

你有没有收到其他错误信息? 也许,int bytes = _bmp.getRowBytes() * _bmp.getHeight() 可以解决你的问题。 - a.ch.
不,我没有收到其他消息。那并没有解决问题。然而,我找到了解决方法。稍后我会发布答案。 - Cyberon
2个回答

7

最终我找到了一种方法,可以使它同时运行得更快。在使用这种方法时,我遇到了两个问题:

  1. 我还应该传递Bitmap.Config参数,否则我无法解码字节数组
  2. _bmp.compress和_bmp.copyPixelsToBuffer会产生不同的数组,所以我不能使用decodeByteArray。

我是这样解决它们的

private void writeObject(ObjectOutputStream oos) throws IOException {
    oos.defaultWriteObject();

    if(_bmp!=null){
        int bytes = _bmp.getWidth()*_bmp.getHeight()*4;

        ByteBuffer buffer = ByteBuffer.allocate(bytes);
        _bmp.copyPixelsToBuffer(buffer);

        byte[] array = new byte[bytes]; // looks like this is extraneous memory allocation

        if (buffer.hasArray()) {
            try{
                array = buffer.array();
            } catch (BufferUnderflowException e) {
                e.printStackTrace();
            }
        }

        String configName = _bmp.getConfig().name();

        oos.writeObject(array);
        oos.writeInt(_bmp.getWidth());
        oos.writeInt(_bmp.getHeight());
        oos.writeObject(configName);
    } else {
        oos.writeObject(null);
    }
}

private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException{
    ois.defaultReadObject();

    byte[] data = (byte[]) ois.readObject();
    if (data != null) {
        int w = ois.readInt();
        int h = ois.readInt();
        String configName = (String) ois.readObject();

        Bitmap.Config configBmp = Bitmap.Config.valueOf(configName);
        Bitmap bitmap_tmp = Bitmap.createBitmap(w, h, configBmp);
        ByteBuffer buffer = ByteBuffer.wrap(data);

        bitmap_tmp.copyPixelsFromBuffer(buffer);

        _bmp = bitmap_tmp.copy(configBmp,true);

        bitmap_tmp.recycle();
    } else {
        _bmp = null;
    }
}

这对我来说足够快了 - 大约比bmp.compress的方式快15倍。希望这可以帮到你 :)

看起来这个方法比下面的方法慢得多。我做错了什么吗? - Ana

1

将位图转换为字节数组:

Bitmap bmp; // your bitmap
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

使用缓冲流以获得更好的性能。


2
我已经说过这种方法太慢了,因为它要压缩Bitmap,这需要很长时间。我已经找到一种方法,可以比之前快15倍左右。 - Cyberon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接