Computer Science/Android

비트맵 <-> byte 변환, 이미지뷰->비트맵

냐옹냐옹아 2013. 3. 11. 20:03

 

 public Bitmap getViewBitmap(View v) {
  v.clearFocus();
  v.setPressed(false);

  boolean willNotCache = v.willNotCacheDrawing();
  v.setWillNotCacheDrawing(false);

  // Reset the drawing cache background color to fully transparent
  // for the duration of this operation
  int color = v.getDrawingCacheBackgroundColor();
  v.setDrawingCacheBackgroundColor(0);

  if (color != 0) {
   v.destroyDrawingCache();
  }
  v.buildDrawingCache();
  Bitmap cacheBitmap = v.getDrawingCache();
  if (cacheBitmap == null) {
   return null;
  }

  Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

  // Restore the view
  v.destroyDrawingCache();
  v.setWillNotCacheDrawing(willNotCache);
  v.setDrawingCacheBackgroundColor(color);

  return bitmap;
 }

 Bitmap byteTobitmap(byte[] data){
  Bitmap bit = BitmapFactory.decodeByteArray( data , 0 , data.length);
  return bit;
 }
 
 byte[] bitmapTobyte(Bitmap bit){
  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bit.compress(CompressFormat.JPEG , 100 , stream);
  byte[] data = stream.toByteArray();
  return data;
 }