2013-10-31 3 views
0

내 안드로이드 응용 프로그램에서 카메라에서 가져온 이미지를 바이트 배열로 변환하고 다시 비트 맵으로 변환하여 이미지보기에서 보려고합니다. Bitmap.compress를 통해 쉽게 할 수 있습니다. 그러나 나는 Bitmap.compress없이 그것을하고 싶다.비트 맵을 ByteArray로 또는 그 반대로 변환하십시오.

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] imageBytes = stream.toByteArray(); 

또는

int bytes = bitmap.getByteCount(); 
ByteBuffer buffer = ByteBuffer.allocate(bytes); 
bitmap.copyPixelsToBuffer(buffer); 
byte[] array = buffer.array(); 

바이트 배열 : 문제는 내가 (매 시간 (선) 가난한 이미지)

     Bitmap hello; //image coming from camera 
         ByteBuffer buffer = ByteBuffer.allocate(hello.getByteCount()); 
         hello.copyPixelsToBuffer(buffer); 
         byte[] bytes1 = buffer.array(); 
         byte [] Bits = new byte[bytes1.length*4]; 
         int i; 
        for(i=0;i<bytes1.length;i++) 
       { 
        Bits[i*4] = 
         Bits[i*4+1] = 
         Bits[i*4+2] = (byte) ~bytes1[i]; //Invert the source bits 
        Bits[i*4+3] = -1;//0xff, that's the alpha. 
       } 

       Bitmap bmimage = Bitmap.createBitmap(360,248, Bitmap.Config.ARGB_8888); 
       bmimage.copyPixelsFromBuffer(ByteBuffer.wrap(Bits)); 
       imageView11.setImageBitmap(bmimage); 
+0

그리고 build-in 메소드'compress'를 사용하지 않고 하드 코딩 된 접근 방식을 사용하는 중요한 이유가 있습니까? – Sajmon

+0

그리고 지금, Bitmap.compress를 피하는 이유는 무엇입니까? – pskink

답변

0
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object 
    byte[] b = baos.toByteArray(); 
    ///useing following code 
    String encoded = Base64.encodeToString(b, Base64.DEFAULT); 
+0

질문을 더 자세히 읽어야합니다. 그는 압축 방법을 사용하고 싶지 않다고 말했다. – Sajmon

1

비트 맵 바이트 배열에 흰색 선을 얻고 있다는 것입니다 비트 맵 :

InputStream inputStream = new ByteArrayInputStream(bytes); 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    BitmapFactory.decodeStream(inputStream, null, o); 
+0

비트 맵에 바이트 배열을 할 때 : nullpointer가 있음을 보여줍니다. –

관련 문제