Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DecodeBytarray和copyPixelsToBuffer不工作。SkImageDecoder::工厂返回null_Java_Android_Bitmap_Bytearray_Serializable - Fatal编程技术网

Java DecodeBytarray和copyPixelsToBuffer不工作。SkImageDecoder::工厂返回null

Java DecodeBytarray和copyPixelsToBuffer不工作。SkImageDecoder::工厂返回null,java,android,bitmap,bytearray,serializable,Java,Android,Bitmap,Bytearray,Serializable,我有一个实现Serializable的类TouchPoint,因为它包含位图,所以我为该类编写了writeObject和readObject: private void writeObject(ObjectOutputStream oos) throws IOException { long t1 = System.currentTimeMillis(); oos.defaultWriteObject(); if(_bmp!=null){ int byte

我有一个实现Serializable的类TouchPoint,因为它包含位图,所以我为该类编写了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::工厂返回null

那我该怎么修呢。我知道可能的解决方案是将writeObject()更改为

但这种方法的速度要慢近10倍以上

  • copyPixelsToBuffer~14ms用于写入图像
  • _bmp.compress~160ms
更新 发现实际问题是

buffer.array();
所有字节[]数组元素都是:0

位图到字节[]:

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

使用Bufferedstreams可以获得更好的性能。

最后,我找到了一种方法,使它能够工作,同时速度更快。使用此方法时,我遇到两个问题:

  • 我还应该传递Bitmap.Config参数,否则我无法解码字节数组
  • _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倍。希望这有帮助:)

    您没有收到任何其他错误消息吗?也许,
    int bytes=\u bmp.getRowBytes()*\u bmp.getHeight()
    可以解决您的问题。不,我没有收到其他消息。这并不能解决问题。然而,我找到了解决这个问题的方法。稍后我会发布一个答案。我已经说过这个方法太慢了,因为它压缩位图需要很多时间。我已经找到了比下面的方法快15倍的方法。它看起来比下面的方法慢得多。我做错什么了吗?
    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;
        }
    }