CameraSource is stretched in AlertDialog

Viewed 127

I'm using SurfaceView in my AlertDialog in which i show a CameraSource which then scans barcodes, my main issue is that the camera preview is distort and stretched at the left and right.

The function where i initialize the CameraSource and SurfaceHolder looks like this:

private void initializeBarcodeReader(SurfaceView surfaceView, boolean greenpass) {
    ToneGenerator toneGen = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
    BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(greenpass ? Barcode.QR_CODE : Barcode.ALL_FORMATS)
            .build();
    CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
            .setAutoFocusEnabled(true)
            .build();

    SurfaceHolder previewHolder = surfaceView.getHolder();
    previewHolder.addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                if (ActivityCompat.checkSelfPermission(PtermActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    cameraSource.start(holder);
                } else {
                    ActivityCompat.requestPermissions(PtermActivity.this, new
                            String[]{Manifest.permission.CAMERA}, 201);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }


        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (cameraSource != null) {
                cameraSource.stop();
                cameraSource.release();
            }
        }
    });
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

But the preview is as the following:

enter image description here

As you could see the pen is very stretched as the phone behind, the preview is not as the one in the real Camera app...

How could i fix it?

1 Answers

Camera resolution is chosen based on View dimensions, so that's why the preview is stretched. One thing you can do while I try to find a solution is to partially hide a QR Code Reader View with other views just to get that effect. For example, you can overlap your full-screen QR Code Reader View with a half-size empty View or other components.

Maybe a similar thing appeared here

Related