2012-05-26 39 views
8

我有一個類接觸點,其實現Serializable因爲其包含的位圖我寫的writeObject和readObject該類:decodeByteArray和copyPixelsToBuffer不工作。 SkImageDecoder ::廠返回null

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()將

ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
_bmp.compress(Bitmap.CompressFormat.PNG, 100, byteStream); 
oos.writeObject(byteStream.toByteArray); 

但這種方法比較慢幾乎10+倍。

寫入圖像
  • _bmp.compress〜160毫秒
  • UPDATE

    • copyPixelsToBuffer〜14MS 找出實際的問題是,經過

      buffer.array(); 
      

      所有的byte []數組元素爲:0

    +0

    你不得到任何其他錯誤消息? 也許,'int bytes = _bmp.getRowBytes()* _bmp.getHeight()'可以解決你的問題。 –

    +0

    沒有,我沒有得到其他消息。這並不能解決問題。但是,我找到了解決這個問題的方法。我稍後會發布答案。 – Cyberon

    回答

    7

    最後我找到一種方法,使其在同一時間工作,更快。我遇到使用這種方法的兩個問題:

    1. 我應該通過Bitmap.Config PARAM也沒有,我可以字節數組
    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; 
        } 
    } 
    

    這是速度不夠快,我 - 約15倍快則bmp.compress方式。希望這有助於:)

    +0

    好吧,它似乎比它慢得多,我是否犯了一些錯誤? –

    1

    位圖爲byte []:

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

    使用Bufferedstreams獲得更好的性能。

    +2

    我已經說過這個方法太慢了,因爲它壓縮了需要很長時間的Bitmap。我已經找到了工作速度快15倍的方法。 – Cyberon

    +0

    還配有緩衝流? – Klaasvaak

    +1

    不幸的是:( – Cyberon

    相關問題