journeyapps / zxing-android-embedded

Barcode scanner library for Android, based on the ZXing decoder
https://journeyapps.com/
Apache License 2.0
5.68k stars 1.26k forks source link

Ho to set Zoom in Camera using Zxing #728

Open vtangade opened 1 year ago

vtangade commented 1 year ago

I want to set Zoom in Camera but not able achieve this using Zxing, please suggest any solution.

I'm currently using following version :

implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
massimilianochiodi commented 1 year ago

I also need this information

yersx commented 1 year ago

Can someone answer?

douglas-fp commented 1 year ago

If you are using something like BarcodeView, extend it and implement CameraPreview.StateListener, CameraParametersCallback. Then in the constructors, after calling the super, call addStateListener(this)

In the override for previewStarted(), call changeCameraParameters(this). That will pass the callback down to the CameraManager which will then get called to set the desired parameters

public Camera.Parameters changeCameraParameters(Camera.Parameters params) { params.setZoom(params.getMaxZoom() / 2) return params; }

setZoom() uses an index into the available zooms list which you can get from Camera.Parameters if you want to see what values you are dealing with. A Galaxy S22 goes up to 8x, so getMaxZoom() / 2 is about 4x

(Note: BarcodeView doesn't need to implement CameraPreview.StateListener since it has it's own previewStarted function that you can override, but other components you are extending may not have that helper)

sarkiroka commented 10 months ago

Kotlin example:

        val camera = barcodeView.barcodeView.cameraInstance ?: return
        camera.changeCameraParameters { params: Camera.Parameters ->
            if (params.isZoomSupported) {
                val maxZoom = params.maxZoom
                val newZoomLevel = Math.min(maxZoom, zoomLevel.toInt())
                params.zoom = newZoomLevel
            }
            params
        }

where zoomLevel is a Float parameter.