Skip to content

Instantly share code, notes, and snippets.

@DUMA042
Created January 10, 2025 23:08
Show Gist options
  • Save DUMA042/b2758ab82a60bbe3a3c3d9e6cbbb1b0f to your computer and use it in GitHub Desktop.
Save DUMA042/b2758ab82a60bbe3a3c3d9e6cbbb1b0f to your computer and use it in GitHub Desktop.
// Configure barcode scanning options for supported formats
val options = BarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_QR_CODE,
Barcode.FORMAT_CODABAR,
Barcode.FORMAT_CODE_93,
Barcode.FORMAT_CODE_39,
Barcode.FORMAT_CODE_128,
Barcode.FORMAT_EAN_8,
Barcode.FORMAT_EAN_13,
Barcode.FORMAT_AZTEC
)
.build()
// Initialize the barcode scanner client with the configured options
val barcodeScanner = BarcodeScanning.getClient(options)
// Set up the image analysis analyzer for barcode detection
cameraController.setImageAnalysisAnalyzer(
ContextCompat.getMainExecutor(ctx), // Use the main executor
MlKitAnalyzer(
listOf(barcodeScanner), // Pass the barcode scanner
ImageAnalysis.COORDINATE_SYSTEM_VIEW_REFERENCED, // Use view-referenced coordinates
ContextCompat.getMainExecutor(ctx) // Use the main executor
) { result: MlKitAnalyzer.Result? ->
// Process the barcode scanning results
val barcodeResults = result?.getValue(barcodeScanner)
if (!barcodeResults.isNullOrEmpty()) {
// Update the barcode state with the first detected barcode
barcode = barcodeResults.first().rawValue
// Update the state to indicate a barcode has been detected
qrCodeDetected = true
// Update the bounding rectangle of the detected barcode
boundingRect = barcodeResults.first().boundingBox
// Log the bounding box for debugging purposes
Log.d("Looking for Barcode ", barcodeResults.first().boundingBox.toString())
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment