我正在开发一个应用程序,其中需要在摄像头工作时保持手电筒的开启状态,同时还需要在不关闭手电筒的情况下拍照。我需要这个功能来提高用户体验。
但是每次拍照时,手电筒都会关闭,我的问题是如何防止这种情况发生?
但是每次拍照时,手电筒都会关闭,我的问题是如何防止这种情况发生?
class CameraFragment : Fragment() {
private lateinit var cameraExecutor: ExecutorService
private var cameraProvider: ProcessCameraProvider? = null
private var preview: Preview? = null
private var imageCapture: ImageCapture? = null
private var imageAnalysis: ImageAnalysis? = null
private var camera: Camera? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
cameraExecutor = Executors.newSingleThreadExecutor()
if (allPermissionsGranted()) {
startCamera()
} else {
ActivityCompat.requestPermissions(requireActivity(),
REQUIRED_PERMISSIONS,
REQUEST_CODE_PERMISSIONS
)
}
}
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
bindCameraUseCases()
}, ContextCompat.getMainExecutor(requireContext()))
}
private fun bindCameraUseCases() {
val cameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
val rotation = binding.viewFinder.display.rotation
preview = Preview.Builder()
.setTargetRotation(rotation)
.build()
imageCapture = ImageCapture.Builder()
.setTargetRotation(rotation)
.setCaptureMode(CAPTURE_MODE_MAXIMIZE_QUALITY)
.build()
imageAnalysis = ImageAnalysis.Builder()
.setTargetRotation(rotation)
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
.build()
imageAnalysis?.setAnalyzer(cameraExecutor) {
// Some analysis
}
cameraProvider?.unbindAll()
try {
camera = cameraProvider?.bindToLifecycle(
this@CameraFragment,
cameraSelector,
preview,
imageCapture,
imageAnalysis
)
// OPEN FLASH
openFlash()
preview?.setSurfaceProvider(binding.viewFinder.surfaceProvider)
} catch(exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}
private fun takePhoto() {
val imageCapture = imageCapture ?: return
imageCapture.takePicture(cameraExecutor, object : ImageCapture.OnImageCapturedCallback() {
override fun onCaptureSuccess(image: ImageProxy) {
super.onCaptureSuccess(image)
// image process
}
override fun onError(exception: ImageCaptureException) {
super.onError(exception)
println("ERROR")
}
})
}
private fun openFlash() {
if (camera?.cameraInfo?.hasFlashUnit() == true) {
camera?.cameraControl?.enableTorch(true)
}
}
private fun closeFlash() {
if (camera?.cameraInfo?.hasFlashUnit() == true) {
camera?.cameraControl?.enableTorch(false)
}
}
override fun onDestroy() {
super.onDestroy()
cameraExecutor.shutdown()
}
override fun onResume() {
super.onResume()
openFlash()
}
override fun onPause() {
super.onPause()
closeFlash()
}
companion object {
private const val TAG = "Camera"
// private const val FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS"
private const val REQUEST_CODE_PERMISSIONS = 10
private val REQUIRED_PERMISSIONS = mutableListOf (
Manifest.permission.CAMERA
).toTypedArray()
}
}