Skip to content

Commit

Permalink
Merge pull request #3 from UbiqueInnovation/bugfix/zxing-parallel-dec…
Browse files Browse the repository at this point in the history
…oding

Read-Only ByteBuffer & Error Codes
  • Loading branch information
gallmann-ubique authored Feb 28, 2022
2 parents df3300a + e8132ad commit c6bef79
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material.Button
import androidx.compose.material.Slider
import androidx.compose.material.Text
Expand All @@ -16,6 +22,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import ch.ubique.qrscanner.compose.QrScanner
import ch.ubique.qrscanner.example.databinding.ActivityComposeBinding
import ch.ubique.qrscanner.mlkit.decoder.MLKitImageDecoder
import ch.ubique.qrscanner.scanner.QrScannerCallback
import ch.ubique.qrscanner.state.DecodingState
import ch.ubique.qrscanner.zxing.decoder.GlobalHistogramImageDecoder
Expand Down Expand Up @@ -44,7 +51,7 @@ class ComposeActivity : AppCompatActivity() {
binding.composeView.setContent {
Box(Modifier.fillMaxSize()) {
QrScanner(
imageDecoders = listOf(GlobalHistogramImageDecoder(), HybridImageDecoder()),
imageDecoders = listOf(MLKitImageDecoder(), GlobalHistogramImageDecoder(), HybridImageDecoder()),
scannerCallback = scannerCallback,
modifier = Modifier.fillMaxSize(),
isFlashEnabled = isFlashEnabled.collectAsState(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package ch.ubique.qrscanner.scanner

object ErrorCodes {
/**
* Happens when the image proxy input has the wrong format (YUV format is expected)
*/
const val INPUT_WRONG_FORMAT = 1

/**
* The image decoder failed to read the image proxy input
*/
const val INPUT_READ_FAILED = 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ import androidx.camera.core.ImageProxy
import ch.ubique.qrscanner.scanner.ErrorCodes
import ch.ubique.qrscanner.scanner.ImageDecoder
import ch.ubique.qrscanner.state.DecodingState
import com.google.zxing.*
import com.google.zxing.BarcodeFormat
import com.google.zxing.Binarizer
import com.google.zxing.BinaryBitmap
import com.google.zxing.ChecksumException
import com.google.zxing.DecodeHintType
import com.google.zxing.FormatException
import com.google.zxing.LuminanceSource
import com.google.zxing.MultiFormatReader
import com.google.zxing.NotFoundException
import com.google.zxing.PlanarYUVLuminanceSource
import com.google.zxing.RGBLuminanceSource
import com.google.zxing.common.GlobalHistogramBinarizer
import com.google.zxing.common.HybridBinarizer
import kotlinx.coroutines.Dispatchers
Expand All @@ -31,18 +41,22 @@ abstract class ZXingImageDecoder(

override suspend fun decodeFrame(image: ImageProxy): DecodingState = withContext(Dispatchers.IO) {
if (image.format in yuvFormats && image.planes.size == 3) {
val buffer = image.planes[0].buffer
val data = buffer.toByteArray()
val source = PlanarYUVLuminanceSource(
data,
image.planes[0].rowStride,
image.height,
0,
0,
image.width,
image.height,
false
)
val source = try {
val buffer = image.planes[0].buffer.asReadOnlyBuffer()
val data = buffer.toByteArray()
PlanarYUVLuminanceSource(
data,
image.planes[0].rowStride,
image.height,
0,
0,
image.width,
image.height,
false
)
} catch (e: Exception) {
return@withContext DecodingState.Error(ErrorCodes.INPUT_READ_FAILED)
}

return@withContext decodeLuminanceSource(source)
} else {
Expand Down

0 comments on commit c6bef79

Please sign in to comment.