|
1 | 1 | package com.darkrockstudios.app.securecamera.camera |
2 | 2 |
|
| 3 | +import androidx.camera.compose.CameraXViewfinder |
3 | 4 | import androidx.camera.core.CameraSelector |
| 5 | +import androidx.camera.core.ImageCapture |
4 | 6 | import androidx.camera.lifecycle.ProcessCameraProvider |
5 | | -import androidx.camera.view.PreviewView |
6 | 7 | import androidx.compose.animation.core.animateFloatAsState |
7 | 8 | import androidx.compose.animation.core.tween |
8 | 9 | import androidx.compose.foundation.Canvas |
9 | 10 | import androidx.compose.foundation.gestures.detectTapGestures |
10 | 11 | import androidx.compose.foundation.gestures.rememberTransformableState |
11 | 12 | import androidx.compose.foundation.gestures.transformable |
12 | 13 | import androidx.compose.foundation.layout.Box |
| 14 | +import androidx.compose.foundation.layout.BoxWithConstraints |
13 | 15 | import androidx.compose.foundation.layout.fillMaxSize |
14 | 16 | import androidx.compose.runtime.* |
15 | 17 | import androidx.compose.ui.Modifier |
16 | 18 | import androidx.compose.ui.graphics.Color |
17 | 19 | import androidx.compose.ui.input.pointer.pointerInput |
18 | | -import androidx.compose.ui.platform.LocalContext |
19 | | -import androidx.compose.ui.viewinterop.AndroidView |
20 | | -import androidx.lifecycle.compose.LocalLifecycleOwner |
21 | 20 | import kotlinx.coroutines.delay |
22 | 21 | import kotlinx.coroutines.launch |
23 | 22 |
|
24 | | -/** Create and remember a [CameraState] inside composition. */ |
25 | | -@Composable |
26 | | -fun rememberCameraState(initialLensFacing: Int = CameraSelector.LENS_FACING_BACK): CameraState { |
27 | | - val context = LocalContext.current |
28 | | - val lifecycleOwner = LocalLifecycleOwner.current |
29 | 23 |
|
30 | | - val provider = remember { ProcessCameraProvider.getInstance(context).get() } |
31 | | - val previewView = remember { |
32 | | - PreviewView(context).apply { scaleType = PreviewView.ScaleType.FIT_CENTER } |
33 | | - } |
| 24 | +@Composable |
| 25 | +fun rememberCameraState( |
| 26 | + initialLensFacing: Int = CameraSelector.LENS_FACING_BACK, |
| 27 | + initialFlashMode: Int = ImageCapture.FLASH_MODE_OFF, |
| 28 | +): CameraState { |
| 29 | + val context = androidx.compose.ui.platform.LocalContext.current |
| 30 | + val lifecycleOwner = androidx.compose.ui.platform.LocalLifecycleOwner.current |
34 | 31 |
|
35 | | - val state = remember { |
| 32 | + return remember { |
| 33 | + val providerFuture = ProcessCameraProvider.getInstance(context).get() |
36 | 34 | CameraState( |
37 | | - previewView = previewView, |
38 | 35 | lifecycleOwner = lifecycleOwner, |
39 | | - providerFuture = provider, |
40 | | - initialLensFacing = initialLensFacing |
41 | | - ) |
42 | | - } |
43 | | - |
44 | | - // (Re)bind camera whenever lens changes. |
45 | | - DisposableEffect(state.lensFacing) { |
46 | | - state.bindCamera() |
47 | | - onDispose { provider.unbindAll() } |
48 | | - } |
49 | | - |
50 | | - DisposableEffect(Unit) { |
51 | | - onDispose { state.shutdown() } |
| 36 | + providerFuture = providerFuture, |
| 37 | + initialLensFacing = initialLensFacing, |
| 38 | + initialFlashMode = initialFlashMode |
| 39 | + ).also { cameraState -> |
| 40 | + cameraState.bindCamera() |
| 41 | + } |
| 42 | + }.also { cameraState -> |
| 43 | + DisposableEffect(cameraState) { |
| 44 | + onDispose { |
| 45 | + cameraState.cleanup() |
| 46 | + } |
| 47 | + } |
52 | 48 | } |
53 | | - |
54 | | - return state |
55 | 49 | } |
56 | 50 |
|
57 | | -/** Composable that renders the camera preview and UI using the provided [CameraState]. */ |
58 | 51 | @Composable |
59 | 52 | fun CameraPreview( |
60 | 53 | state: CameraState, |
61 | | - modifier: Modifier = Modifier.fillMaxSize() |
| 54 | + modifier: Modifier = Modifier |
62 | 55 | ) { |
63 | 56 | val scope = rememberCoroutineScope() |
64 | | - // Pinch‑to‑zoom transformable |
65 | | - val zoomState = rememberTransformableState { zoomChange, _, _ -> |
66 | | - state.camera?.let { cam -> |
67 | | - val current = cam.cameraInfo.zoomState.value?.zoomRatio ?: 1f |
68 | | - val newZoom = (current * zoomChange).coerceIn(state.minZoom, state.maxZoom) |
69 | | - state.setZoomRatio(newZoom) |
| 57 | + |
| 58 | + // Track the display size for focus calculations |
| 59 | + BoxWithConstraints( |
| 60 | + modifier = modifier |
| 61 | + ) { |
| 62 | + val displaySize = androidx.compose.ui.unit.IntSize( |
| 63 | + width = constraints.maxWidth, |
| 64 | + height = constraints.maxHeight |
| 65 | + ) |
| 66 | + |
| 67 | + // Update the camera state with current display size |
| 68 | + LaunchedEffect(displaySize) { |
| 69 | + state.displaySize = displaySize |
70 | 70 | } |
71 | | - } |
72 | 71 |
|
73 | | - // focus indicator fade animation |
74 | | - val indicatorAlpha by animateFloatAsState( |
75 | | - targetValue = if (state.focusOffset != null) 1f else 0f, |
76 | | - animationSpec = tween(durationMillis = 300) |
77 | | - ) |
| 72 | + // Pinch‑to‑zoom transformable |
| 73 | + val zoomState = rememberTransformableState { zoomChange, _, _ -> |
| 74 | + state.camera?.let { cam -> |
| 75 | + val current = cam.cameraInfo.zoomState.value?.zoomRatio ?: 1f |
| 76 | + val newZoom = (current * zoomChange).coerceIn(state.minZoom, state.maxZoom) |
| 77 | + state.setZoomRatio(newZoom) |
| 78 | + } |
| 79 | + } |
78 | 80 |
|
79 | | - Box( |
80 | | - modifier = modifier |
81 | | - .transformable(zoomState) |
82 | | - .pointerInput(Unit) { |
83 | | - detectTapGestures { offset -> |
84 | | - state.focusAt(offset) |
| 81 | + // focus indicator fade animation |
| 82 | + val indicatorAlpha by animateFloatAsState( |
| 83 | + targetValue = if (state.focusOffset != null) 1f else 0f, |
| 84 | + animationSpec = tween(durationMillis = 300), |
| 85 | + label = "focus_alpha" |
| 86 | + ) |
| 87 | + |
| 88 | + Box( |
| 89 | + modifier = Modifier |
| 90 | + .fillMaxSize() |
| 91 | + .transformable(zoomState) |
| 92 | + .pointerInput(Unit) { |
| 93 | + detectTapGestures { offset -> |
| 94 | + state.focusAt(offset) |
85 | 95 |
|
86 | | - scope.launch { |
87 | | - delay(800) |
88 | | - state.clearFocusOffset() |
| 96 | + scope.launch { |
| 97 | + delay(800) |
| 98 | + state.clearFocusOffset() |
| 99 | + } |
89 | 100 | } |
90 | 101 | } |
| 102 | + ) { |
| 103 | + state.surfaceRequest?.let { request -> |
| 104 | + CameraXViewfinder( |
| 105 | + surfaceRequest = request, |
| 106 | + modifier = Modifier.fillMaxSize() |
| 107 | + ) |
91 | 108 | } |
92 | | - ) { |
93 | | - // Camera preview |
94 | | - AndroidView( |
95 | | - factory = { state.previewView }, |
96 | | - modifier = Modifier.fillMaxSize() |
97 | | - ) |
98 | 109 |
|
99 | | - // Draw focus ring |
100 | | - state.focusOffset?.let { pos -> |
101 | | - Canvas( |
102 | | - modifier = Modifier |
103 | | - .fillMaxSize() |
104 | | - .pointerInput(Unit) {} // intercept taps |
105 | | - ) { |
106 | | - drawCircle( |
107 | | - color = Color.White.copy(alpha = indicatorAlpha), |
108 | | - radius = 40f, |
109 | | - center = pos |
110 | | - ) |
| 110 | + // Draw focus ring |
| 111 | + state.focusOffset?.let { pos -> |
| 112 | + Canvas( |
| 113 | + modifier = Modifier |
| 114 | + .fillMaxSize() |
| 115 | + .pointerInput(Unit) {} // intercept taps |
| 116 | + ) { |
| 117 | + drawCircle( |
| 118 | + color = Color.White.copy(alpha = indicatorAlpha), |
| 119 | + radius = 40f, |
| 120 | + center = pos, |
| 121 | + style = androidx.compose.ui.graphics.drawscope.Stroke(width = 4f) |
| 122 | + ) |
| 123 | + } |
111 | 124 | } |
112 | 125 | } |
113 | 126 | } |
|
0 commit comments