프로그래밍/Android-Java
[ANDROID] camera preview frame byte array to bitmap
채연2
2020. 10. 19. 09:48
public void onPreviewFrame(final byte[] bytes, final Camera camera);
위의 onPreviewFrame에서 byte array 데이터를 bitmap 으로 변환하는 방법은
Camera.Parameters parameters = camera.getParameters();
YuvImage yuv = new YuvImage(bytes, parameters.getPreviewFormat(), APP_PREVIEW_WIDTH, APP_PREVIEW_HEIGHT, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, APP_PREVIEW_WIDTH, APP_PREVIEW_HEIGHT), 50, out);
byte[] bytes2 = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(bytes2, 0, bytes2.length);
이렇게 해주면 변환이 된다 !!
혹시 bitmap의 일부 영역만 잘라내서 파일로 저장하고 싶으면
image = Bitmap.createBitmap(image, (int) testrect.left, (int) testrect.top, (int) testrect.width(), (int) testrect.height(), null, true);
Bitmap finalImage = image;
OutputStream fOut = null;
try {
File file = new File("/testpath/" + test++ + ".png");
fOut = new FileOutputStream(file);
finalImage.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
createBitmap으로 원하는 영역만큼 잘라내서 파일로 써주면 된다 !
320x100