-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds QR Code Scanner UI component (#11)
* 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
1 parent
2313231
commit 1249a65
Showing
4 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
WalletSdk/src/main/java/com/spruceid/wallet/sdk/ui/QRCodeAnalyzer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.