3

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 but the following code doesn't work. How can I zoom the preview with CameraX API 1.0.0-alpha05 ?

CameraFragment.kt

 /** Declare and bind preview, capture and analysis use cases */
    private fun bindCameraUseCases() {

       ...

        // Apply declared configs to CameraX using the same lifecycle owner
        CameraX.bindToLifecycle(
                viewLifecycleOwner, preview, imageCapture, imageAnalyzer)

       //I added code
        var my=Rect(0,0,500,500)
        preview.zoom(my)
    }

Image A

enter image description here

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
HelloCW
  • 843
  • 22
  • 125
  • 310

1 Answers1

4

@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 :

 private fun startCamera() {
        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 it
            val 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)
    }
Akshay Raiyani
  • 1,243
  • 9
  • 21
  • Thank you, would you please release your full code? so I can test it. – HelloCW Sep 21 '19 at 07:20
  • I add two buttons in XML and set zoom in/out on that. Here is my startCamera() method(in updated answer) – Akshay Raiyani Sep 23 '19 at 06:36
  • Thank you very much, but I still can't run your project, could you publish your full code? – HelloCW Sep 23 '19 at 08:37
  • You know the Android Studio tell me `updateTransform()` isn't defined, and `left`,`right`, etc. in your code can't be compiled. – HelloCW Sep 23 '19 at 08:43
  • I prepared a demo with help of this - https://codelabs.developers.google.com/codelabs/camerax-getting-started/#0 And just put extra two buttons for zoom in/out. – Akshay Raiyani Sep 23 '19 at 09:30
  • Thank you , could you publish your project to Git? or upload your project to Dropbox, so i can dowmload and test it? – HelloCW Sep 23 '19 at 09:39
  • Actually my demo is not complete now, I am working on it, want to explore more on CameraX, So when it will be complete, will upload it on Git and notify you. And one more thing It's very simple to create a demo from your end, follow the steps which are mentions in codelabs and put two buttons in XML and perform click listener which are mention above. Just try it once. – Akshay Raiyani Sep 23 '19 at 10:10
  • It seems that `setOnPreviewOutputUpdateListener` has been removed? Do you have a current solution? – spartygw Jun 24 '21 at 21:44