|
| 1 | +/* |
| 2 | + * Copyright 2026 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.android.camerax.takeaphoto |
| 17 | + |
| 18 | +import android.content.Context |
| 19 | +import android.util.Log |
| 20 | +import androidx.camera.core.CameraControl |
| 21 | +import androidx.camera.core.CameraSelector |
| 22 | +import androidx.camera.core.FocusMeteringAction |
| 23 | +import androidx.camera.core.ImageCapture |
| 24 | +import androidx.camera.core.ImageCaptureException |
| 25 | +import androidx.camera.core.ImageProxy |
| 26 | +import androidx.camera.core.Preview |
| 27 | +import androidx.camera.core.SurfaceOrientedMeteringPointFactory |
| 28 | +import androidx.camera.core.SurfaceRequest |
| 29 | +import androidx.camera.lifecycle.ProcessCameraProvider |
| 30 | +import androidx.compose.runtime.Composable |
| 31 | +import androidx.compose.runtime.Stable |
| 32 | +import androidx.compose.runtime.getValue |
| 33 | +import androidx.compose.runtime.mutableStateOf |
| 34 | +import androidx.compose.runtime.remember |
| 35 | +import androidx.compose.runtime.rememberCoroutineScope |
| 36 | +import androidx.compose.runtime.setValue |
| 37 | +import androidx.compose.ui.geometry.Offset |
| 38 | +import androidx.core.content.ContextCompat |
| 39 | +import androidx.lifecycle.LifecycleOwner |
| 40 | +import kotlinx.coroutines.CoroutineScope |
| 41 | +import kotlinx.coroutines.launch |
| 42 | +import java.util.concurrent.ExecutorService |
| 43 | +import java.util.concurrent.Executors |
| 44 | + |
| 45 | +private const val TAG = "CameraXController" |
| 46 | + |
| 47 | +@Composable |
| 48 | +fun rememberCameraXTakeAPhotoController( |
| 49 | + context: Context, |
| 50 | + lifecycleOwner: LifecycleOwner, |
| 51 | + isFrontCamera: Boolean, |
| 52 | + onPhotoCaptured: (ImageProxy) -> Unit |
| 53 | +): CameraXTakeAPhotoController { |
| 54 | + val coroutineScope = rememberCoroutineScope() |
| 55 | + return remember(context, isFrontCamera, onPhotoCaptured) { |
| 56 | + CameraXTakeAPhotoController(context, lifecycleOwner, isFrontCamera, onPhotoCaptured, coroutineScope) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +@Stable |
| 61 | +class CameraXTakeAPhotoController( |
| 62 | + private val context: Context, |
| 63 | + private val lifecycleOwner: LifecycleOwner, |
| 64 | + val isFrontCamera: Boolean, |
| 65 | + private val onPhotoCaptured: (ImageProxy) -> Unit, |
| 66 | + private val coroutineScope: CoroutineScope |
| 67 | +) { |
| 68 | + var surfaceRequest: SurfaceRequest? by mutableStateOf(null) |
| 69 | + private set |
| 70 | + |
| 71 | + private var cameraControl: CameraControl? = null |
| 72 | + private var cameraProvider: ProcessCameraProvider? = null |
| 73 | + |
| 74 | + private val cameraExecutor: ExecutorService = Executors.newSingleThreadExecutor() |
| 75 | + |
| 76 | + private val imageCapture = ImageCapture.Builder() |
| 77 | + .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) |
| 78 | + .build() |
| 79 | + |
| 80 | + private val preview = Preview.Builder().build().apply { |
| 81 | + setSurfaceProvider { request -> |
| 82 | + surfaceRequest = request |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + fun openCamera() { |
| 87 | + val cameraProviderFuture = ProcessCameraProvider.getInstance(context) |
| 88 | + cameraProviderFuture.addListener({ |
| 89 | + val provider = cameraProviderFuture.get() |
| 90 | + cameraProvider = provider |
| 91 | + |
| 92 | + val lensFacing = if (isFrontCamera) { |
| 93 | + CameraSelector.LENS_FACING_FRONT |
| 94 | + } else { |
| 95 | + CameraSelector.LENS_FACING_BACK |
| 96 | + } |
| 97 | + |
| 98 | + val cameraSelector = CameraSelector.Builder() |
| 99 | + .requireLensFacing(lensFacing) |
| 100 | + .build() |
| 101 | + |
| 102 | + try { |
| 103 | + provider.unbindAll() |
| 104 | + val camera = provider.bindToLifecycle( |
| 105 | + lifecycleOwner, cameraSelector, preview, imageCapture |
| 106 | + ) |
| 107 | + cameraControl = camera.cameraControl |
| 108 | + } catch (exc: Exception) { |
| 109 | + Log.e(TAG, "Use case binding failed", exc) |
| 110 | + } |
| 111 | + }, ContextCompat.getMainExecutor(context)) |
| 112 | + } |
| 113 | + |
| 114 | + fun updateTargetRotation(rotation: Int) { |
| 115 | + imageCapture.targetRotation = rotation |
| 116 | + preview.targetRotation = rotation |
| 117 | + } |
| 118 | + |
| 119 | + fun takePicture() { |
| 120 | + imageCapture.takePicture( |
| 121 | + cameraExecutor, |
| 122 | + object : ImageCapture.OnImageCapturedCallback() { |
| 123 | + override fun onCaptureSuccess(image: ImageProxy) { |
| 124 | + coroutineScope.launch { |
| 125 | + onPhotoCaptured(image) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + override fun onError(exception: ImageCaptureException) { |
| 130 | + Log.e(TAG, "Photo capture failed: \${exception.message}", exception) |
| 131 | + } |
| 132 | + } |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + fun focus(surfaceCoords: Offset, width: Float, height: Float) { |
| 137 | + val factory = SurfaceOrientedMeteringPointFactory(width, height) |
| 138 | + val point = factory.createPoint(surfaceCoords.x, surfaceCoords.y) |
| 139 | + val action = FocusMeteringAction.Builder(point, FocusMeteringAction.FLAG_AF).build() |
| 140 | + cameraControl?.startFocusAndMetering(action) |
| 141 | + } |
| 142 | + |
| 143 | + fun closeCamera() { |
| 144 | + cameraProvider?.unbindAll() |
| 145 | + } |
| 146 | + |
| 147 | + fun release() { |
| 148 | + closeCamera() |
| 149 | + cameraExecutor.shutdown() |
| 150 | + } |
| 151 | +} |
0 commit comments