10

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?

Pitr
  • 143
  • 1
  • 1
  • 8
  • where you set imageView.setImageBitmap(bmp); ? – Hardik Nov 30 '13 at 09:48
  • After Bitmap bmp = BitmapFactory.decodeByteArray(outstr.toByteArray(), 0, outstr.size()); – Pitr Nov 30 '13 at 09:53
  • make sure outstr.size()!=0!!? – Hardik Nov 30 '13 at 09:54
  • Sorry for the late answer... however, how should i check this? – Pitr Dec 10 '13 at 11:42
  • see outstr.length() or some other function for check size in ByteArrayOutputStream class in doc – Hardik Dec 11 '13 at 04:45
  • Hey..@PitrHey.. 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.......http://stackoverflow.com/questions/41392791/how-to-apply-custom-filters-in-a-camera-surfaceview-preview – Sagar Aghara Jan 21 '17 at 09:15
  • i 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....http://i.stack.imgur.com/WYHuj.jpg – Sagar Aghara Jan 21 '17 at 09:16
  • Please help me if you know .when You will be free..God Bless You...Thanks.. – Sagar Aghara Jan 21 '17 at 09:16

1 Answers1

35

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);
        }
    });
}
blacelle
  • 2,199
  • 1
  • 19
  • 28
  • Hey @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.......http://stackoverflow.com/questions/41392791/how-to-apply-custom-filters-in-a-camera-surfaceview-preview – Sagar Aghara Jan 21 '17 at 09:17
  • i 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....http://i.stack.imgur.com/WYHuj.jpg – Sagar Aghara Jan 21 '17 at 09:18