更改 Torch 亮度,而相机预览是工作在Android

x33g5p2x  于 2023-05-05  发布在  Android
关注(0)|答案(1)|浏览(223)

在Android 13(API 33)中,出现了一个新控件来调整手电筒亮度:
public void turnOnTorchWithStrengthLevel ( cameraId, int torchStrength)
https://developer.android.com/reference/android/hardware/camera2/CameraManager#turnOnTorchWithStrengthLevel(java. lang. String,%20int)
但在工作相机预览期间调用此函数会引发异常:*android.hardware.camera2.CameraAccessException:CAMERA_IN_USE(4):turnOnTorchWithStrength等级:2596:由于现有摄像机用户,摄像机“0”的手电筒不可用 *
在预览期间使用简单 Torch 的唯一方法是使用如下选项调整捕获请求构建器:builder.set(CaptureRequest.FLASH_MODE, if (isOn) CaptureRequest.FLASH_MODE_TORCH else CaptureRequest.FLASH_MODE_OFF)但它不允许修改手电筒亮度。
我有这样的代码只是为了在相机预览期间打开/关闭手电筒。它工作正常。

override fun switchTorch(isOn: Boolean) {
        if (cameraOpened.get()) {

            val session = captureSession
            val builderAndHandler = captureRequestBuilderWithHandler

            if (session != null && builderAndHandler != null) {
                val (builder, handler) = builderAndHandler
                session.stopRepeating()
                isTorchInProgress = isOn
                builder.set(CaptureRequest.FLASH_MODE, if (isOn) CaptureRequest.FLASH_MODE_TORCH else CaptureRequest.FLASH_MODE_OFF)
                session.setRepeatingRequest(builder.build(), null, handler)
            }
        } else {
            postponedSwitchTorch = isOn
        }
    }

我试图实现一个功能的Android 13只修改亮度水平如下:

override fun setTorchStrengthLevel(level: Int) {
    Log.d("qaz", "Api2. setTorchStrengthLevel: $level")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (cameraOpened.get()) {

            val session = captureSession
            val builderAndHandler = captureRequestBuilderWithHandler

            if (session != null && builderAndHandler != null) {
                val (builder, handler) = builderAndHandler
                session.stopRepeating()
                cameraManager.turnOnTorchWithStrengthLevel(cameraDevice.id, level)
                builder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH)
                session.setRepeatingRequest(builder.build(), null, handler)
            } else {
                Log.w("qaz", "skip setTorch, because session or buildANdHandler are null")
            }
        } else {
            Log.w("qaz", "skip setTorch, because camera is not opened")
        }
    } else {
        throw IllegalAccessException("This function should be called only on Android T or higher")
    }
}

调用**turnOnTorchWithStrengthLevel(cameraDevice.id,level)***CameraAccessException时抛出异常:CAMERA_IN_USE(4):turnOnTorchWithStrength等级:2596:由于现有摄像机用户,摄像机“0”的手电筒不可用
如何在相机预览在Android 13(T)上工作时更改手电筒亮度?

ee7vknir

ee7vknir1#

我最近遇到了同样的问题,直到现在才找到一个好的解决方案。这是我在AN 13上工作的代码,也许它对你或其他人有帮助:

private fun startTorch(cameraId: String) {
    cameraManager.torchCamera?.let {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
            cameraOptions.containsKey(cameraId)
        ) {
            val characteristics = cameraManager.getCameraCharacteristics(it)
            val supportsMaxLevel = characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL)
            if (supportsMaxLevel != null && supportsMaxLevel > 1) {
                cameraManager.turnOnTorchWithStrengthLevel(
                    it,
                    cameraOptions[it]!!.flashPower
                )
            } else {
                cameraManager.setTorchMode(it, true)
            }
        }
    }
}

相关问题