-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from myofficework000/barcode_scanner
added barcode scanner implementation
- Loading branch information
Showing
6 changed files
with
171 additions
and
2 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
99 changes: 99 additions & 0 deletions
99
...c/main/java/com/example/jetpack_compose_all_in_one/demos/barcode_scanner/CameraPreview.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,99 @@ | ||
package com.example.jetpack_compose_all_in_one.demos.barcode_scanner | ||
|
||
import android.util.Size | ||
import android.view.ViewGroup | ||
import androidx.camera.core.CameraSelector | ||
import androidx.camera.core.ImageAnalysis | ||
import androidx.camera.core.Preview | ||
import androidx.camera.lifecycle.ProcessCameraProvider | ||
import androidx.camera.view.PreviewView | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.core.content.ContextCompat | ||
import androidx.lifecycle.LifecycleOwner | ||
import com.google.mlkit.vision.barcode.BarcodeScannerOptions | ||
import com.google.mlkit.vision.barcode.BarcodeScanning | ||
import com.google.mlkit.vision.barcode.common.Barcode | ||
import com.google.mlkit.vision.common.InputImage | ||
import java.util.concurrent.Executors | ||
|
||
enum class ScanMode { | ||
QR_CODE, | ||
BARCODE, | ||
BOTH | ||
} | ||
@Composable | ||
@androidx.annotation.OptIn(androidx.camera.core.ExperimentalGetImage::class) | ||
@androidx.camera.core.ExperimentalGetImage | ||
fun CameraPreview( | ||
modifier: Modifier, | ||
onBarcodeDetected: (Barcode) -> Unit, | ||
scanMode: ScanMode = ScanMode.BOTH | ||
) { | ||
val context = LocalContext.current | ||
val lifecycleOwner = LocalContext.current as LifecycleOwner | ||
val cameraExecutor = Executors.newSingleThreadExecutor() | ||
val options = when (scanMode) { | ||
ScanMode.QR_CODE -> BarcodeScannerOptions.Builder() | ||
.setBarcodeFormats(Barcode.FORMAT_QR_CODE) | ||
.build() | ||
ScanMode.BARCODE -> BarcodeScannerOptions.Builder() | ||
.setBarcodeFormats(Barcode.FORMAT_CODE_128, Barcode.FORMAT_CODE_39) | ||
.build() | ||
ScanMode.BOTH -> BarcodeScannerOptions.Builder() | ||
.setBarcodeFormats(Barcode.FORMAT_ALL_FORMATS) | ||
.build() | ||
} | ||
val barcodeScanner = BarcodeScanning.getClient(options) | ||
|
||
AndroidView( | ||
modifier = modifier, | ||
factory = { context -> | ||
val previewView = PreviewView(context).apply { | ||
layoutParams = ViewGroup.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.MATCH_PARENT | ||
) | ||
} | ||
|
||
val cameraProviderFuture = ProcessCameraProvider.getInstance(context) | ||
cameraProviderFuture.addListener({ | ||
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get() | ||
val preview = Preview.Builder().build().also { | ||
it.setSurfaceProvider(previewView.surfaceProvider) | ||
} | ||
|
||
val imageAnalysis = ImageAnalysis.Builder() | ||
.setTargetResolution(Size(1280, 720)) | ||
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) | ||
.build() | ||
.also { | ||
it.setAnalyzer(cameraExecutor, { imageProxy -> | ||
val mediaImage = imageProxy.image | ||
if (mediaImage != null) { | ||
val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees) | ||
barcodeScanner.process(image) | ||
.addOnSuccessListener { barcodes -> | ||
for (barcode in barcodes) { | ||
onBarcodeDetected(barcode) | ||
} | ||
} | ||
.addOnCompleteListener { | ||
imageProxy.close() | ||
} | ||
} | ||
}) | ||
} | ||
try { | ||
cameraProvider.unbindAll() | ||
cameraProvider.bindToLifecycle(lifecycleOwner, CameraSelector.DEFAULT_BACK_CAMERA, preview, imageAnalysis) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
}, ContextCompat.getMainExecutor(context)) | ||
previewView | ||
} | ||
) | ||
} |
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/example/jetpack_compose_all_in_one/demos/barcode_scanner/ScannerApp.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,57 @@ | ||
package com.example.jetpack_compose_all_in_one.demos.barcode_scanner | ||
|
||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import android.Manifest | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.RadioButton | ||
import androidx.compose.material3.Text | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
@androidx.annotation.OptIn(androidx.camera.core.ExperimentalGetImage::class) | ||
fun ScannerApp() { | ||
var scanMode by remember { mutableStateOf(ScanMode.BOTH) } | ||
var lastScannedCode by remember { mutableStateOf("") } | ||
var hasCameraPermission by remember { mutableStateOf(false) } | ||
|
||
val permissionResult = rememberLauncherForActivityResult( | ||
contract = ActivityResultContracts.RequestPermission(), | ||
onResult = { hasCameraPermission = it } | ||
) | ||
|
||
LaunchedEffect(key1 = true) { | ||
permissionResult.launch(Manifest.permission.CAMERA) | ||
} | ||
|
||
Column { | ||
Row { | ||
Text(text = "QR Code") | ||
RadioButton(selected = scanMode == ScanMode.QR_CODE, onClick = { scanMode = ScanMode.QR_CODE }) | ||
Text(text = "Barcode") | ||
RadioButton(selected = scanMode == ScanMode.BARCODE, onClick = { scanMode = ScanMode.BARCODE }) | ||
Text(text = "Both") | ||
RadioButton(selected = scanMode == ScanMode.BOTH, onClick = { scanMode = ScanMode.BOTH }) | ||
} | ||
if (hasCameraPermission) { | ||
CameraPreview( | ||
modifier = Modifier.weight(1f), | ||
onBarcodeDetected = { barcode -> | ||
lastScannedCode = barcode.rawValue ?: "No barcode data" | ||
}, | ||
scanMode = scanMode) | ||
Text(text = "Last Scanned Code: $lastScannedCode") | ||
} else { | ||
Text(text = "Camera permission not granted") | ||
} | ||
} | ||
} |
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
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