I want to use the seekbar to zoom in and out of the camera from zxing

Nguyen Van Cuon 0 Tallied Votes 186 Views Share

I want to use the seekbar to zoom in and out of the camera from zxing

private fun setCameraZoom(zoomLevel: Float) {
        if (!isFrontCamera && ::captureRequestBuilder.isInitialized && ::cameraCaptureSession.isInitialized) {
            val cameraManager = requireActivity().getSystemService(android.content.Context.CAMERA_SERVICE) as CameraManager
            val cameraId = getBackCameraId()
            val cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId)
            val maxZoom = cameraCharacteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM)
            ?: 1f

            // ... (tính toán zoomLevel và getZoomRect)
            val clampedZoomLevel = max(1f, min(zoomLevel, maxZoom))
            try {
                captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, getZoomRect(clampedZoomLevel))
                cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null)
            } catch (e: CameraAccessException) {
                Log.e("CameraZoom", "Error setting zoom: ${e.message}")
            }
        }
    }
    private fun getZoomRect(zoomLevel: Float): Rect {
        val cameraManager = requireActivity().getSystemService(android.content.Context.CAMERA_SERVICE) as CameraManager
        val cameraId = getBackCameraId()
        val cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId)
        val sensorSize = cameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE) ?: return Rect()

        val centerX = sensorSize.width() / 2
        val centerY = sensorSize.height() / 2
        val deltaX = (0.5f * sensorSize.width() / zoomLevel).toInt()
        val deltaY = (0.5f * sensorSize.height() / zoomLevel).toInt()

        return Rect(centerX - deltaX, centerY - deltaY, centerX + deltaX, centerY + deltaY)
    }


    private fun setupBarcodeScanner() {
        Log.d("setupBarcodeScanner", "setupBarcodeScanner")
        defaultCamera = preferenceHelper.getInt(PreferenceHelper.PREF_CAMERA_MODE, CAMERA_MODE_1)
        isFrontCamera = defaultCamera == CAMERA_MODE_2

        val cameraId =
            if (isFrontCamera) android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT
            else android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK


        val formats: Collection<BarcodeFormat> =
            listOf(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39)
        binding.barcodeScanner.barcodeView.decoderFactory = DefaultDecoderFactory(formats)
        binding.barcodeScanner.initializeFromIntent(requireActivity().intent)
        binding.barcodeScanner.decodeContinuous(callback)

        val cameraSettings = CameraSettings()
        cameraSettings.requestedCameraId = cameraId

        binding.barcodeScanner.cameraSettings = cameraSettings
        beepManager = BeepManager(requireActivity())
        binding.barcodeScanner.resume()
        binding.barcodeScanner.setStatusText("")
        qrCodeAnimation = binding.qrCodeAnimation
        loadAndPlayAnimation()

    }
    private fun getBackCameraId(): String {
        val cameraManager = requireActivity().getSystemService(android.content.Context.CAMERA_SERVICE) as CameraManager
        return cameraManager.cameraIdList.firstOrNull { id ->
            val characteristics = cameraManager.getCameraCharacteristics(id)
            characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK
        } ?: throw RuntimeException("Back camera not found")
    }
    private fun getFrontCameraId(): String {
        val cameraManager = requireActivity().getSystemService(android.content.Context.CAMERA_SERVICE) as CameraManager
        return cameraManager.cameraIdList.firstOrNull { id ->
            val characteristics = cameraManager.getCameraCharacteristics(id)
            characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT
        } ?: throw RuntimeException("Front camera not found")
    }
    private fun setupSeekbar(){
        binding.seekBar.setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?,progress: Int, fromUser: Boolean) {
                val zoomLevel = (progress.toFloat() / 10.0f) *10.0f  // Normalize to 1.0 - maxZoom
                binding.barcodeScanner.barcodeView
                setCameraZoom(zoomLevel)
            }
            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                Log.d("CameraZoom", "SeekBar tracking started")
            }
            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                Log.d("CameraZoom", "SeekBar tracking stopped")
            }
        })
    }
rproffitt 2,620 "Nothing to see here." Moderator

This is not an offer to write code but isZoomSupported? Find out with example code that looks for that at https://github.com/zxing/zxing/blob/master/android-core/src/main/java/com/google/zxing/client/android/camera/CameraConfigurationUtils.java

peol 0 Newbie Poster

To use the seekbar to zoom in and out of the camera in ZXing, integrate the SeekBar with the camera's zoom functionality. First, ensure your device supports zoom and obtain the Camera.Parameters object. Link the SeekBar's onChangeListener to adjust the zoom level based on the seekbar's position. Update the Camera.Parameters with the new zoom value and apply the changes. This setup allows users to smoothly zoom in and out by sliding the seekbar, enhancing the barcode scanning experience.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.