diff --git a/WalletSdk/build.gradle.kts b/WalletSdk/build.gradle.kts index af1dc82..b627da1 100644 --- a/WalletSdk/build.gradle.kts +++ b/WalletSdk/build.gradle.kts @@ -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 */ diff --git a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeAnalyzer.kt b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeAnalyzer.kt index d835eda..8bcf56f 100644 --- a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeAnalyzer.kt +++ b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeAnalyzer.kt @@ -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( @@ -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, @@ -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) } diff --git a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeScanner.kt b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeScanner.kt index 9601f54..2cf3146 100644 --- a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeScanner.kt +++ b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeScanner.kt @@ -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 @@ -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", @@ -88,6 +93,7 @@ fun QRCodeScanner( "QR code line animation", ) + Column( modifier = Modifier.fillMaxSize(), ) { @@ -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() @@ -121,8 +116,8 @@ fun QRCodeScanner( val imageAnalysis = ImageAnalysis.Builder() .setBackpressureStrategy(STRATEGY_KEEP_ONLY_LATEST) - .setResolutionSelector(resolutionSelector) .build() + imageAnalysis.setAnalyzer( ContextCompat.getMainExecutor(context), QrCodeAnalyzer( @@ -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() }