Getting image from SurfaceView to ImageView?

前端 未结 1 1007
迷失自我
迷失自我 2021-02-06 17:52

I\'m having a little trouble of getting an image/drawable or a bitmap from a SurfaceView that works as a camera preivew.

    final CameraSurfaceView cameraSurfac         


        
相关标签:
1条回答
  • 2021-02-06 17:55

    It's far more complicated than that. The background of the SurfaceView is not the camera preview. You have to have a class that implements Camera.PreviewCalback. Once you have that, you can get a byte array containing the image that the preview sends. On some phones, you can set the preview to be a JPEG in which case you can decode it straight with BitmapFactory. On other phones that don't support that feature, you'll get by default a YUV 4:2:0 image that you have to convert into a JPEG image.

    On Android 2.2+, you can convert the YUV image to a JPEG like so:

       int w = params.getPreviewSize().width;
       int h = params.getPreviewSize().height;
       int format = params.getPreviewFormat();
       YuvImage image = new YuvImage(data, format, w, h, null);
    
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       Rect area = new Rect(0, 0, w, h);
       image.compressToJpeg(area, 50, out);
       Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
       ivCam.setImageBitmap(bm);
    

    If you're targeting older models, you have to use a conversion algorithm like the one here.

    http://blog.tomgibara.com/post/132956174/yuv420-to-rgb565-conversion-in-android

    A SO source:

    Getting frames from Video Image in Android

    EDIT: If all you want is to show the camera view, then you just add the SurfaceView that your camera is using to a layout that is already displayed like you did in your question. It's already displaying it.

    0 讨论(0)
提交回复
热议问题