安卓摄像头 data 转bitmap

public static Bitmap getBitMap(byte[] data, Camera camera, boolean mIsFrontalCamera) {
   int width = camera.getParameters().getPreviewSize().width;
   int height = camera.getParameters().getPreviewSize().height;
   YuvImage yuvImage = new YuvImage(data, camera.getParameters()
         .getPreviewFormat(), width, height, null);
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   yuvImage.compressToJpeg(new Rect(0, 0, width, height), 80,
         byteArrayOutputStream);
   byte[] jpegData = byteArrayOutputStream.toByteArray();
   // 获取照相后的bitmap
   Bitmap tmpBitmap = BitmapFactory.decodeByteArray(jpegData, 0,
         jpegData.length);
   Matrix matrix = new Matrix();
   matrix.reset();
   if (mIsFrontalCamera) {
      matrix.setRotate(-90);
   } else {
      matrix.setRotate(90);
   }
   tmpBitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, tmpBitmap.getWidth(),
         tmpBitmap.getHeight(), matrix, true);
   tmpBitmap = tmpBitmap.copy(Bitmap.Config.ARGB_8888, true);

   int hight = tmpBitmap.getHeight() > tmpBitmap.getWidth() ? tmpBitmap
         .getHeight() : tmpBitmap.getWidth();

   float scale = hight / 800.0f;

   if (scale > 1) {
      tmpBitmap = Bitmap.createScaledBitmap(tmpBitmap,
            (int) (tmpBitmap.getWidth() / scale),
            (int) (tmpBitmap.getHeight() / scale), false);
   }
   return tmpBitmap;
}

你可能感兴趣的:(android)