OnPreviewFrame data image to imageView
25,496
It is possible you did not open the Camera in the UI thread. However, you need to ensure setImageBitmap is called in the UI thread:
@Override
public void onPreviewFrame(final byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] bytes = out.toByteArray();
final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
((ImageView) findViewById(R.id.loopback)).setImageBitmap(bitmap);
}
});
}
Author by
Pitr
Updated on July 09, 2022Comments
-
Pitr almost 2 years
i'm trying to get the byte[] from the preview of the camera, convert it to bitmap and display it on a imageview with imageView.setImageBitmap()
i've managed to start the preview and display it on a surfaceView, but i don't know how to convert the byte[] data (that comes in Yuv format i think) in a RGB bitmap to display it on a imageView.
the code i'm trying is the following:
camera = camera.open(); parameters = camera.getParameters(); camera.setParameters(parameters); surfaceHolder = surfaceView.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); camera.setPreviewDisplay(surfaceHolder); camera.setPreviewCallback(this); camera.startPreview();
and the preview callback is this
@Override public void onPreviewFrame(byte[] data, Camera camera) { Camera.Parameters parameters = camera.getParameters(); int width = parameters.getPreviewSize().width; int height = parameters.getPreviewSize().height; ByteArrayOutputStream outstr = new ByteArrayOutputStream(); Rect rect = new Rect(0, 0, width, height); YuvImage yuvimage=new YuvImage(data,ImageFormat.NV21,width,height,null); yuvimage.compressToJpeg(rect, 100, outstr); Bitmap bmp = BitmapFactory.decodeByteArray(outstr.toByteArray(), 0, outstr.size()); imgView1.setImageBitmap(bmp); }
The preview works but the imageView remains empty
Any idea?
-
Sagar Aghara over 7 yearsHey @Bla...i am developing an CustomCaemra App. i had same Problem.i also want multiple camera Preview into single Activity[Like in GridView].Please help me How can i achive this..i already asked question but no one gave me answer....Here is my Question Link.......stackoverflow.com/questions/41392791/…
-
Sagar Aghara over 7 yearsi googled it and tried more tutorial's,libraries[grafica too].but i did not achive this type of Preview[Multiple Cmaera Preview]. i found more problem's same as my problem in Stackoverflow.but same as my Question no one gave answer's.Please see this Link i want this type of View....i.stack.imgur.com/WYHuj.jpg