如何在kotlin中实现捏合缩放和相机预览

问题描述 投票:0回答:1

我试图在我的应用程序中创建一个相机,允许用户在使用相机时捏合预览屏幕。我一直在尝试实现捏合放大我的相机,但我一点运气都没有。这是我在 github 线程上找到的代码

val listener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
        override fun onScale(detector: ScaleGestureDetector): Boolean {
            // Get the current camera zoom ratio
            val currentZoomRatio: Float = cameraInfo.zoomRatio.value ?: 1F

            // Get by how much the scale has changed due to the user's pinch gesture
            val delta = detector.scaleFactor

            // Update the camera's zoom ratio
            cameraControl.setZoomRatio(currentZoomRatio * delta)
            return true
        }
    }

    val scaleGestureDetector = ScaleGestureDetector(requireContext(), listener)
    progressDialog = ProgressDialog(requireContext(), false)
    binding.cameraPreview.post {
        startCamera()
    }
    binding.ivImageCapture.setOnClickListener {
        progressDialog.show()
        takePicture()
    }

    binding.cameraPreview.setOnTouchListener { _, event ->
        scaleGestureDetector.onTouchEvent(event)
        return@setOnTouchListener true
    }
}

我唯一的错误是“未解析的参考:zoomRatio”。我已将所有依赖项升级到最新版本。

这是我的开始相机功能

@SuppressLint("UnsafeExperimentalUsageError")
private fun startCamera() {
    // Get screen metrics used to setup camera for full screen resolution
    val metrics = DisplayMetrics().also { binding.cameraPreview.display.getRealMetrics(it) }

    val screenAspectRatio = aspectRatio(metrics.widthPixels, metrics.heightPixels)

    val rotation = binding.cameraPreview.display.rotation


    // Bind the CameraProvider to the LifeCycleOwner
    val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()
    val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
    cameraProviderFuture.addListener({

        // CameraProvider
        val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

        // Preview
        val preview = Preview.Builder()
            .setTargetAspectRatio(screenAspectRatio)
            // Set initial target rotation
            .setTargetRotation(rotation)

            .build()

        preview.setSurfaceProvider(binding.cameraPreview.surfaceProvider)

        // ImageCapture
        imageCapture = initializeImageCapture(screenAspectRatio, rotation)

        // ImageAnalysis


        cameraProvider.unbindAll()

        try { val camera = cameraProvider.bindToLifecycle(
            this, cameraSelector, preview, imageCapture
        )
            cameraControl = camera.cameraControl
            cameraInfo = camera.cameraInfo
            cameraControl.setLinearZoom(0.5f)



        } catch (exc: Exception) {
            exc.printStackTrace()
        }
    }, ContextCompat.getMainExecutor(requireContext()))
android kotlin android-camera android-camerax pinchzoom
1个回答
0
投票
 // Listen to pinch gestures
    val listener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
        override fun onScale(detector: ScaleGestureDetector): Boolean {
            // Get the camera's current zoom ratio
            val currentZoomRatio = cameraInfo.zoomState.value?.zoomRatio ?: 0F

            // Get the pinch gesture's scaling factor
            val delta = detector.scaleFactor

            // Update the camera's zoom ratio. This is an asynchronous operation that returns
            // a ListenableFuture, allowing you to listen to when the operation completes.
            cameraControl.setZoomRatio(currentZoomRatio * delta)

            // Return true, as the event was handled
            return true
        }
    }
    val scaleGestureDetector = ScaleGestureDetector(requireContext(), listener)

    // Attach the pinch gesture listener to the viewfinder
    binding.cameraPreview.setOnTouchListener { _, event ->
        scaleGestureDetector.onTouchEvent(event)
        return@setOnTouchListener true
    }

这是我找到的来源

© www.soinside.com 2019 - 2024. All rights reserved.