I want to use the seekbar to zoom in and out of the camera from zxing
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")
}
})
}
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.