fix: Fix CodeScanner stalling on Android (#2009)

This commit is contained in:
Marc Rousavy 2023-10-16 16:28:51 +02:00 committed by GitHub
parent 8884a1c3e4
commit e8ae11e30b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,10 +13,10 @@ import java.io.Closeable
class CodeScannerPipeline(val size: Size, val format: Int, val output: CameraOutputs.CodeScannerOutput) : Closeable { class CodeScannerPipeline(val size: Size, val format: Int, val output: CameraOutputs.CodeScannerOutput) : Closeable {
companion object { companion object {
// We want to have a buffer of 3 images, but we always only acquire one. // We want to have a buffer of 2 images, but we always only acquire one.
// That way the pipeline is free to stream up to two frames into the unused buffer, // That way the pipeline is free to stream one frame into the unused buffer,
// while the other buffer is being used for code scanning. // while the other buffer is being used for code scanning.
private const val MAX_IMAGES = 3 private const val MAX_IMAGES = 2
} }
private val imageReader: ImageReader private val imageReader: ImageReader
@ -35,12 +35,14 @@ class CodeScannerPipeline(val size: Size, val format: Int, val output: CameraOut
var isBusy = false var isBusy = false
imageReader = ImageReader.newInstance(size.width, size.height, format, MAX_IMAGES) imageReader = ImageReader.newInstance(size.width, size.height, format, MAX_IMAGES)
imageReader.setOnImageAvailableListener({ reader -> imageReader.setOnImageAvailableListener({ reader ->
val image = reader.acquireNextImage() ?: return@setOnImageAvailableListener
if (isBusy) { if (isBusy) {
// We're currently executing on a previous Frame, so we skip this one. // We're currently executing on a previous Frame, so we skip this one.
// We don't try to acquire a new one, so that the Camera is not blocked/stalling. // Close it and free it again, so that the Camera does not stall.
image.close()
return@setOnImageAvailableListener return@setOnImageAvailableListener
} }
val image = reader.acquireNextImage() ?: return@setOnImageAvailableListener
isBusy = true isBusy = true
// TODO: Get correct orientation // TODO: Get correct orientation