Skip to content

Commit

Permalink
Improve QR Code scanning (#12)
Browse files Browse the repository at this point in the history
* Improve QR Code scanning

* Update Zxing
  • Loading branch information
w4ll3 authored Jun 10, 2024
1 parent 4c4e6c9 commit 970cf58
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
2 changes: 1 addition & 1 deletion WalletSdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ dependencies {
implementation("androidx.camera:camera-camera2:1.3.2")
implementation("androidx.camera:camera-lifecycle:1.3.2")
implementation("androidx.camera:camera-view:1.3.2")
implementation("com.google.zxing:core:3.3.3")
implementation("com.google.zxing:core:3.5.1")
implementation("com.google.accompanist:accompanist-permissions:0.34.0")
implementation("androidx.test.ext:junit-ktx:1.1.5")
/* End UI dependencies */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import android.graphics.ImageFormat
import android.os.Build
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy
import com.google.zxing.BarcodeFormat
import com.google.zxing.BinaryBitmap
import com.google.zxing.DecodeHintType
import com.google.zxing.MultiFormatReader
import com.google.zxing.PlanarYUVLuminanceSource
import com.google.zxing.common.HybridBinarizer
import com.google.zxing.qrcode.QRCodeReader
import java.nio.ByteBuffer

class QrCodeAnalyzer(
Expand All @@ -27,7 +25,7 @@ class QrCodeAnalyzer(

override fun analyze(image: ImageProxy) {
if (image.format in supportedImageFormats) {
val bytes = image.planes.first().buffer.toByteArray()
val bytes = image.planes[0].buffer.toByteArray()
val source =
PlanarYUVLuminanceSource(
bytes,
Expand All @@ -41,17 +39,7 @@ class QrCodeAnalyzer(
)
val binaryBmp = BinaryBitmap(HybridBinarizer(source))
try {
val result =
MultiFormatReader().apply {
setHints(
mapOf(
DecodeHintType.POSSIBLE_FORMATS to
arrayListOf(
BarcodeFormat.QR_CODE,
),
),
)
}.decode(binaryBmp)
val result = QRCodeReader().decode(binaryBmp)
if (isMatch(result.text)) {
onQrCodeScanned(result.text)
}
Expand Down
41 changes: 22 additions & 19 deletions WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeScanner.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.spruceid.wallet.sdk.ui

import android.content.res.Resources
import android.util.Range
import android.view.Surface
import androidx.camera.core.CameraControl
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST
import androidx.camera.core.ImageCapture
import androidx.camera.core.Preview
import androidx.camera.core.resolutionselector.ResolutionSelector
import androidx.camera.core.resolutionselector.ResolutionStrategy
Expand Down Expand Up @@ -49,6 +53,7 @@ import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.content.ContextCompat


@Composable
fun QRCodeScanner(
title: String = "Scan QR Code",
Expand Down Expand Up @@ -88,6 +93,7 @@ fun QRCodeScanner(
"QR code line animation",
)


Column(
modifier = Modifier.fillMaxSize(),
) {
Expand All @@ -96,22 +102,11 @@ fun QRCodeScanner(
) {
AndroidView(
factory = { context ->
val screenSize = android.util.Size(1920, 1080)
val resolutionSelector =
ResolutionSelector
.Builder()
.setResolutionStrategy(
ResolutionStrategy(
screenSize,
ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER
)
)
.build()
val previewView = PreviewView(context)
val preview =
Preview.Builder()
.setTargetFrameRate(Range(20, 45))
.setResolutionSelector(resolutionSelector)
.setTargetRotation(Surface.ROTATION_0)
.build()
val selector =
CameraSelector.Builder()
Expand All @@ -121,8 +116,8 @@ fun QRCodeScanner(
val imageAnalysis =
ImageAnalysis.Builder()
.setBackpressureStrategy(STRATEGY_KEEP_ONLY_LATEST)
.setResolutionSelector(resolutionSelector)
.build()

imageAnalysis.setAnalyzer(
ContextCompat.getMainExecutor(context),
QrCodeAnalyzer(
Expand All @@ -132,13 +127,21 @@ fun QRCodeScanner(
code = result
}),
)
var cameraControl: CameraControl? = null
try {
cameraProviderFuture.get().bindToLifecycle(
lifecycleOwner,
selector,
preview,
imageAnalysis,
)
cameraControl = cameraProviderFuture
.get()
.bindToLifecycle(
lifecycleOwner,
selector,
preview,
imageAnalysis,
).cameraControl
} catch (e: Exception) {
e.printStackTrace()
}
try {
cameraControl?.setZoomRatio(2f)
} catch (e: Exception) {
e.printStackTrace()
}
Expand Down

0 comments on commit 970cf58

Please sign in to comment.