Home:ALL Converter>Preview is stretched on CameraX PreviewView

Preview is stretched on CameraX PreviewView

Ask Time:2021-03-05T14:03:56         Author:Yohanes Rizky G

Json Formatter

i am exciting to use CameraX on my app., my reference from google officialy CameraX on github, but something wrong., please help me.,

this my code to set aspect ratio

private fun aspectRatio(width: Int, height: Int): Int {
    val previewRatio = max(width, height).toDouble() / min(width, height)
    if (abs(previewRatio - RATIO_4_3_VALUE) <= abs(previewRatio - RATIO_16_9_VALUE)) {
        return AspectRatio.RATIO_4_3
    }
    return AspectRatio.RATIO_16_9
}

And this my preview code

@SuppressLint("UnsafeExperimentalUsageError")private fun startCamera() {
    val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
    viewFinder=binding.surfaceView
    cameraProviderFuture.addListener({
        // Used to bind the lifecycle of cameras to the lifecycle owner
        val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
        val metrics = DisplayMetrics().also { viewFinder.display.getRealMetrics(it) }
        Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")
        val screenAspectRatio = aspectRatio(metrics.widthPixels, metrics.heightPixels)
        Log.d(TAG, "Preview aspect ratio: $screenAspectRatio")
        Size(metrics.widthPixels, metrics.heightPixels)

        val rotation = viewFinder.display.rotation

        // Preview
        val preview = Preview.Builder()
            .setTargetRotation(rotation)
            .setTargetAspectRatio(screenAspectRatio)
            .build()

        imageCapture = if (flashStatus) {
            ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
                .setTargetRotation(rotationimage)
                .setTargetAspectRatio(screenAspectRatio)
                .build()
        } else {
            ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
                .setTargetRotation(rotationimage)
                .build()
        }

        // Select back camera as a default
        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        // Unbind use cases before rebinding
        cameraProvider.unbindAll()
        viewFinder.scaleType=PreviewView.ScaleType.FILL_CENTER

        try {
            // Bind use cases to camera
            val cam = cameraProvider.bindToLifecycle(
                this, cameraSelector, preview, imageCapture)
            if (!flashStatus) {
                if (cam.cameraInfo.hasFlashUnit()) {
                    cam.cameraControl.enableTorch(true)
                }
            } else {
                cam.cameraControl.enableTorch(false)
            }
            // Attach the viewfinder's surface provider to preview use case
            preview.setSurfaceProvider(viewFinder.surfaceProvider)


        } catch (exc: Exception) {
            Log.e(TAG, "Use case binding failed", exc)
        }

    }, ContextCompat.getMainExecutor(this))
}

 

and this output preview output preview is stretched

what is wrong on my code?

thank you for your help me folks.,

UPDATE when my device on dark mode, the preview is normal. preview normal on dark mode how to fixed in normal mode??

Author:Yohanes Rizky G,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/66487592/preview-is-stretched-on-camerax-previewview
yy