Skip to content

Commit

Permalink
Adds QR Code Scanner UI component (#11)
Browse files Browse the repository at this point in the history
* Add QR Code scanner UI component

* Update WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeAnalyzer.kt

Co-authored-by: Gregório Granado Magalhães <[email protected]>

* Update WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeAnalyzer.kt

Co-authored-by: Gregório Granado Magalhães <[email protected]>

* Fix lint

* Add isMatch method

* Remove image crop when analyzing QR codes

* Update QR code camera resolution

---------

Co-authored-by: Gregório Granado Magalhães <[email protected]>
  • Loading branch information
Juliano1612 and w4ll3 authored May 15, 2024
1 parent 2313231 commit 1249a65
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 0 deletions.
15 changes: 15 additions & 0 deletions WalletSdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ android {
jvmTarget = "1.8"
}

buildFeatures {
compose = true
viewBinding = true
}

composeOptions { kotlinCompilerExtensionVersion = "1.5.11" }

publishing {
singleVariant("release") {
withSourcesJar()
Expand All @@ -114,6 +121,14 @@ dependencies {
api("com.spruceid.wallet.sdk.rs:walletsdkrs:0.0.25")
//noinspection GradleCompatible
implementation("com.android.support:appcompat-v7:28.0.0")
/* Begin UI dependencies */
implementation("androidx.compose.material3:material3:1.2.1")
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.accompanist:accompanist-permissions:0.34.0")
/* End UI dependencies */
testImplementation("junit:junit:4.13.2")
androidTestImplementation("com.android.support.test:runner:1.0.2")
androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2")
Expand Down
1 change: 1 addition & 0 deletions WalletSdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CAMERA" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.spruceid.wallet.sdk.ui

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 java.nio.ByteBuffer

class QrCodeAnalyzer(
private val onQrCodeScanned: (String) -> Unit,
private val isMatch: (content: String) -> Boolean = {_ -> true},
) : ImageAnalysis.Analyzer {

private val supportedImageFormats = mutableListOf(ImageFormat.YUV_420_888)

init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
supportedImageFormats.addAll(listOf(ImageFormat.YUV_422_888, ImageFormat.YUV_444_888))
}
}

override fun analyze(image: ImageProxy) {
if (image.format in supportedImageFormats) {
val bytes = image.planes.first().buffer.toByteArray()
val source =
PlanarYUVLuminanceSource(
bytes,
image.width,
image.height,
0,
0,
image.width,
image.height,
false,
)
val binaryBmp = BinaryBitmap(HybridBinarizer(source))
try {
val result =
MultiFormatReader().apply {
setHints(
mapOf(
DecodeHintType.POSSIBLE_FORMATS to
arrayListOf(
BarcodeFormat.QR_CODE,
),
),
)
}.decode(binaryBmp)
if (isMatch(result.text)) {
onQrCodeScanned(result.text)
}
} catch (e: Exception) {
e.printStackTrace()
} finally {
image.close()
}
}
}

private fun ByteBuffer.toByteArray(): ByteArray {
rewind()
return ByteArray(remaining()).also {
get(it)
}
}
}
Loading

0 comments on commit 1249a65

Please sign in to comment.