Skip to content Skip to sidebar Skip to footer

How To Zoom The Preview Using Android Camerax Library?

I hope to add a function to zoom in preview picture (Please see Image A) for the sample code at https://github.com/android/camera/tree/master/CameraXBasic I have read the article b

Solution 1:

@HelloCW you are doing the right things, I am able to do zoom in/out using this code

button_plus.setOnClickListener {
    if (right < 100) {
        right += 100
        bottom += 100
        left += 100
        top += 100
        val my = Rect(left, top, right, bottom)
        preview.zoom(my)
    }
}

button_minus.setOnClickListener {
    if (right > 0) {
        right -= 100
        bottom -= 100
        left -= 100
        top -= 100
        val my = Rect(left, top, right, bottom)
        preview.zoom(my)
    }
}

Here is the output enter image description here

enter image description here

Update :

privatefunstartCamera() {
        val metrics = DisplayMetrics().also { view_finder.display.getRealMetrics(it) }
        val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)
        val previewConfig = PreviewConfig.Builder().apply {
            setTargetAspectRatio(screenAspectRatio)
            setTargetRotation(view_finder.display.rotation)
        }.build()
        // Build the viewfinder use case
        preview = Preview(previewConfig)
        preview.setOnPreviewOutputUpdateListener {
            // To update the SurfaceTexture, we have to remove it and re-add itval parent = view_finder.parent as ViewGroup
            parent.removeView(view_finder)
            parent.addView(view_finder, 0)
            view_finder.surfaceTexture = it.surfaceTexture
            updateTransform()
        }
        CameraX.bindToLifecycle(this, preview)
    }

Post a Comment for "How To Zoom The Preview Using Android Camerax Library?"