Make image capture happen on separate thread executor
This commit is contained in:
@@ -75,6 +75,9 @@ fun CameraScreen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val captureController = remember { CameraCaptureController() }
|
val captureController = remember { CameraCaptureController() }
|
||||||
|
DisposableEffect(Unit) {
|
||||||
|
onDispose { captureController.shutdown() }
|
||||||
|
}
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
val camera = android.Manifest.permission.CAMERA
|
val camera = android.Manifest.permission.CAMERA
|
||||||
@@ -100,7 +103,6 @@ fun CameraScreen(
|
|||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
captureController.takePicture(
|
captureController.takePicture(
|
||||||
context = context,
|
|
||||||
onImageCaptured = { imageProxy ->
|
onImageCaptured = { imageProxy ->
|
||||||
if (imageProxy != null) {
|
if (imageProxy != null) {
|
||||||
viewModel.processCapturedImageAndNavigate(imageProxy)
|
viewModel.processCapturedImageAndNavigate(imageProxy)
|
||||||
@@ -235,11 +237,15 @@ fun Point.toOffset() = Offset(x.toFloat(), y.toFloat())
|
|||||||
|
|
||||||
class CameraCaptureController {
|
class CameraCaptureController {
|
||||||
var imageCapture: ImageCapture? = null
|
var imageCapture: ImageCapture? = null
|
||||||
|
private val executor = Executors.newSingleThreadExecutor()
|
||||||
|
|
||||||
fun takePicture(context: Context, onImageCaptured: (ImageProxy?) -> Unit) {
|
fun shutdown() {
|
||||||
|
executor.shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun takePicture(onImageCaptured: (ImageProxy?) -> Unit) {
|
||||||
imageCapture?.takePicture(
|
imageCapture?.takePicture(
|
||||||
// TODO is it a good idea to use this executor?
|
executor,
|
||||||
ContextCompat.getMainExecutor(context),
|
|
||||||
object : ImageCapture.OnImageCapturedCallback() {
|
object : ImageCapture.OnImageCapturedCallback() {
|
||||||
override fun onCaptureSuccess(imageProxy: ImageProxy) {
|
override fun onCaptureSuccess(imageProxy: ImageProxy) {
|
||||||
onImageCaptured(imageProxy)
|
onImageCaptured(imageProxy)
|
||||||
|
|||||||
Reference in New Issue
Block a user