Skip to content

Commit d9a03c7

Browse files
committed
CameraScreen: set up a Preview to make it easier to work on the layout
1 parent 271f047 commit d9a03c7

File tree

1 file changed

+77
-32
lines changed
  • app/src/main/java/org/mydomain/myscan/view

1 file changed

+77
-32
lines changed

app/src/main/java/org/mydomain/myscan/view/Camera.kt

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ import androidx.compose.ui.graphics.asImageBitmap
6969
import androidx.compose.ui.graphics.toArgb
7070
import androidx.compose.ui.platform.LocalConfiguration
7171
import androidx.compose.ui.platform.LocalContext
72-
import androidx.compose.ui.semantics.semantics
7372
import androidx.compose.ui.unit.dp
7473
import androidx.compose.ui.viewinterop.AndroidView
7574
import androidx.core.content.ContextCompat
@@ -82,6 +81,7 @@ import org.mydomain.myscan.LiveAnalysisState
8281
import org.mydomain.myscan.MainViewModel
8382
import org.mydomain.myscan.Point
8483
import org.mydomain.myscan.scaledTo
84+
import org.mydomain.myscan.ui.theme.MyScanTheme
8585
import java.util.concurrent.ExecutorService
8686
import java.util.concurrent.Executors
8787

@@ -99,7 +99,6 @@ fun CameraScreen(
9999
val showPageDialog = rememberSaveable { mutableStateOf(false) }
100100
val isProcessing = rememberSaveable { mutableStateOf(false) }
101101
val pageToValidate by viewModel.pageToValidate.collectAsStateWithLifecycle()
102-
val pageCount = viewModel.pageCount()
103102

104103
val context = LocalContext.current
105104
val requestPermissionLauncher = rememberLauncherForActivityResult(
@@ -122,32 +121,32 @@ fun CameraScreen(
122121
}
123122
}
124123

125-
Box(modifier = modifier.fillMaxSize()) {
126-
CameraPreviewWithOverlay(onImageAnalyzed, captureController, liveAnalysisState)
127-
MessageBox(liveAnalysisState.inferenceTime)
128-
CaptureButton(
129-
onClick = {
130-
showPageDialog.value = true
131-
isProcessing.value = true
132-
captureController.takePicture(
133-
onImageCaptured = { imageProxy ->
134-
if (imageProxy != null) {
135-
viewModel.processCapturedImageThen(imageProxy) {
136-
isProcessing.value = false
137-
}
138-
} else {
139-
Log.e("MyScan", "Error during image capture")
124+
CameraScreenContent(
125+
modifier,
126+
cameraPreview = {
127+
CameraPreview(
128+
onImageAnalyzed = onImageAnalyzed,
129+
captureController = captureController
130+
) },
131+
pageCount = viewModel.pageCount(),
132+
liveAnalysisState,
133+
onCapture = {
134+
showPageDialog.value = true
135+
isProcessing.value = true
136+
captureController.takePicture(
137+
onImageCaptured = { imageProxy ->
138+
if (imageProxy != null) {
139+
viewModel.processCapturedImageThen(imageProxy) {
140+
isProcessing.value = false
140141
}
142+
} else {
143+
Log.e("MyScan", "Error during image capture")
141144
}
142-
)
143-
},
144-
modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 96.dp)
145-
)
146-
CameraScreenFooter(
147-
pageCount = pageCount,
148-
onFinalizePressed = onFinalizePressed,
149-
modifier = Modifier.align(Alignment.BottomCenter))
150-
}
145+
}
146+
)
147+
},
148+
onFinalizePressed = onFinalizePressed
149+
)
151150

152151
if (showPageDialog.value) {
153152
PageValidationDialog(
@@ -167,6 +166,33 @@ fun CameraScreen(
167166
}
168167
}
169168

169+
@Composable
170+
private fun CameraScreenContent(
171+
modifier: Modifier,
172+
cameraPreview: @Composable () -> Unit,
173+
pageCount: Int,
174+
liveAnalysisState: LiveAnalysisState,
175+
onCapture: () -> Unit,
176+
onFinalizePressed: () -> Unit
177+
) {
178+
Box(modifier = modifier.fillMaxSize()) {
179+
CameraPreviewWithOverlay(cameraPreview, liveAnalysisState)
180+
MessageBox(liveAnalysisState.inferenceTime)
181+
182+
CaptureButton(
183+
onClick = onCapture,
184+
modifier = Modifier
185+
.align(Alignment.BottomCenter)
186+
.padding(bottom = 96.dp)
187+
)
188+
CameraScreenFooter(
189+
pageCount = pageCount,
190+
onFinalizePressed = onFinalizePressed,
191+
modifier = Modifier.align(Alignment.BottomCenter)
192+
)
193+
}
194+
}
195+
170196
@Composable
171197
fun CaptureButton(onClick: () -> Unit, modifier: Modifier) {
172198
val color = MaterialTheme.colorScheme.primary
@@ -199,8 +225,7 @@ fun CaptureButton(onClick: () -> Unit, modifier: Modifier) {
199225

200226
@Composable
201227
private fun CameraPreviewWithOverlay(
202-
onImageAnalyzed: (ImageProxy) -> Unit,
203-
captureController: CameraCaptureController,
228+
cameraPreview: @Composable () -> Unit,
204229
liveAnalysisState: LiveAnalysisState
205230
) {
206231
val width = LocalConfiguration.current.screenWidthDp
@@ -210,10 +235,7 @@ private fun CameraPreviewWithOverlay(
210235
.width(width.dp)
211236
.height(height.dp)
212237
) {
213-
CameraPreview(
214-
onImageAnalyzed = onImageAnalyzed,
215-
captureController = captureController
216-
)
238+
cameraPreview()
217239
AnalysisOverlay(liveAnalysisState)
218240
}
219241
}
@@ -405,3 +427,26 @@ fun CameraScreenFooter(
405427
}
406428
}
407429
}
430+
431+
@androidx.compose.ui.tooling.preview.Preview(showBackground = true)
432+
@Composable
433+
fun CameraScreenPreview() {
434+
MyScanTheme {
435+
CameraScreenContent(
436+
modifier = Modifier,
437+
cameraPreview = {
438+
Box(
439+
modifier = Modifier
440+
.fillMaxSize()
441+
.background(Color.DarkGray),
442+
contentAlignment = Alignment.Center
443+
) {
444+
Text("Camera Preview", color = Color.White)
445+
}
446+
},
447+
pageCount = 3,
448+
liveAnalysisState = LiveAnalysisState(),
449+
onCapture = {},
450+
onFinalizePressed = {})
451+
}
452+
}

0 commit comments

Comments
 (0)