2012-05-26 4 views
8

I는 Serializable를 구현하고 비트 맵이 포함되어 있기 때문에 내가의 writeObject와 readObject에 해당 클래스에 대한 쓴 클래스의 터치 포인트가 있습니다decodeByteArray 및 copyPixelsToBuffer가 작동하지 않습니다. SkImageDecoder :: 공장 반환 널

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

어떻게 해결할 수 있습니까? 나는 가능한 솔루션

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

에()의 writeObject를 변경하는 것을 알고하지만이 방법은 속도가 느린 거의 10 + 배이다. 이미지를

  • _bmp.compress ~ 160ms
  • UPDATE를 작성하기위한

    • copyPixelsToBuffer ~ 14ms가 은 찾아 실제 문제가 있음을 그

      buffer.array(); 
      

      모든 바이트 후 [] 배열 요소는 다음과 같습니다. 0

    +0

    다른 오류 메시지가 표시되지 않습니까? 아마도'int bytes = _bmp.getRowBytes() * _bmp.getHeight()'가 문제를 해결할 것입니다. –

    +0

    아니요, 다른 메시지가 표시되지 않습니다. 그것은 문제를 해결하지 못합니다. 그러나, 나는 이것을 해결하는 방법을 찾는다. 나는 나중에 대답을 게시 할 것이다. – Cyberon

    답변

    7

    마지막으로 나는 그것을 더 빨리 작동시킬 수있는 방법을 찾는다. 그없이 나는 바이트 배열

  • _bmp.compress를 디코딩 할 수 _bmp.copyPixelsToBuffer 다른 배열을 내가 그렇게주고,

    1. 나는 또한 Bitmap.Config의 PARAM을 전달해야합니다 :이 방법을 사용하여 두 가지 문제가 발생했습니다 decodeByteArray를 사용할 수 없습니다. bmp.compress 방법 다음 약 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; 
        } 
    } 
    

    이 나를 위해 충분히 빠른 해결. 이 도움이되기를 바랍니다 :)

    +0

    잘 아래의 방법보다 훨씬 느린 것 같습니다. 내가 실수를하고 있니? –

    1

    비트 맵에서 바이트 [] :

    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