Friday, 13 August 2021

CameraSource is stretched in AlertDialog

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?



from CameraSource is stretched in AlertDialog

No comments:

Post a Comment