Skip to content

Commit dead58e

Browse files
bug: fix pause not working correctly on android
1 parent 50fdca8 commit dead58e

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class MobileScanner(
4949
/// Internal variables
5050
private var cameraProvider: ProcessCameraProvider? = null
5151
private var camera: Camera? = null
52+
private var cameraSelector: CameraSelector? = null
5253
private var preview: Preview? = null
5354
private var textureEntry: TextureRegistry.SurfaceTextureEntry? = null
5455
private var scanner: BarcodeScanner? = null
@@ -61,6 +62,7 @@ class MobileScanner(
6162
private var detectionSpeed: DetectionSpeed = DetectionSpeed.NO_DUPLICATES
6263
private var detectionTimeout: Long = 250
6364
private var returnImage = false
65+
private var isPaused = false
6466

6567
companion object {
6668
/**
@@ -250,7 +252,22 @@ class MobileScanner(
250252
this.detectionTimeout = detectionTimeout
251253
this.returnImage = returnImage
252254

253-
if (camera?.cameraInfo != null && preview != null && textureEntry != null) {
255+
if (camera?.cameraInfo != null && preview != null && textureEntry != null && !isPaused) {
256+
257+
// TODO: resume here for seamless transition
258+
// if (isPaused) {
259+
// resumeCamera()
260+
// mobileScannerStartedCallback(
261+
// MobileScannerStartParameters(
262+
// if (portrait) width else height,
263+
// if (portrait) height else width,
264+
// currentTorchState,
265+
// textureEntry!!.id(),
266+
// numberOfCameras ?: 0
267+
// )
268+
// )
269+
// return
270+
// }
254271
mobileScannerErrorCallback(AlreadyStarted())
255272

256273
return
@@ -353,6 +370,7 @@ class MobileScanner(
353370
preview,
354371
analysis
355372
)
373+
cameraSelector = cameraPosition
356374
} catch(exception: Exception) {
357375
mobileScannerErrorCallback(NoCamera())
358376

@@ -410,25 +428,39 @@ class MobileScanner(
410428
* Pause barcode scanning.
411429
*/
412430
fun pause() {
413-
if (isPaused()) {
431+
if (isPaused) {
414432
throw AlreadyPaused()
415433
} else if (isStopped()) {
416434
throw AlreadyStopped()
417435
}
418436

419-
releaseCamera()
437+
pauseCamera()
420438
}
421439

422440
/**
423441
* Stop barcode scanning.
424442
*/
425443
fun stop() {
426-
if (!isPaused() && isStopped()) {
444+
if (!isPaused && isStopped()) {
427445
throw AlreadyStopped()
428446
}
429447

430448
releaseCamera()
431-
releaseTexture()
449+
}
450+
451+
private fun pauseCamera() {
452+
// Pause camera by unbinding all use cases
453+
cameraProvider?.unbindAll()
454+
isPaused = true
455+
}
456+
457+
private fun resumeCamera() {
458+
// Resume camera by rebinding use cases
459+
cameraProvider?.let { provider ->
460+
val owner = activity as LifecycleOwner
461+
cameraSelector?.let { provider.bindToLifecycle(owner, it, preview) }
462+
}
463+
isPaused = false
432464
}
433465

434466
private fun releaseCamera() {
@@ -446,11 +478,13 @@ class MobileScanner(
446478
it.zoomState.removeObservers(owner)
447479
it.cameraState.removeObservers(owner)
448480
}
481+
textureEntry?.release()
482+
textureEntry = null
483+
449484
// Unbind the camera use cases, the preview is a use case.
450485
// The camera will be closed when the last use case is unbound.
451486
cameraProvider?.unbindAll()
452487

453-
// Release the texture for the preview.
454488
textureEntry?.release()
455489
textureEntry = null
456490

@@ -460,13 +494,7 @@ class MobileScanner(
460494
lastScanned = null
461495
}
462496

463-
private fun releaseTexture() {
464-
textureEntry?.release()
465-
textureEntry = null
466-
}
467-
468497
private fun isStopped() = camera == null && preview == null
469-
private fun isPaused() = isStopped() && textureEntry != null
470498

471499
/**
472500
* Toggles the flash light on or off.

0 commit comments

Comments
 (0)