OnPreviewFrame data image to imageView

后端 未结 1 1727
有刺的猬
有刺的猬 2020-12-09 00:19

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 sta

相关标签:
1条回答
  • 2020-12-09 00:33

    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);
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题