Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve QR Code scanning #12

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
/* End UI dependencies */
testImplementation("junit:junit:4.13.2")
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
Loading