Skip to content

Instantly share code, notes, and snippets.

@koolfreak
Last active June 10, 2022 13:49
Show Gist options
  • Save koolfreak/0f6c3b26ba75c5e39608600e62c6bbed to your computer and use it in GitHub Desktop.
Save koolfreak/0f6c3b26ba75c5e39608600e62c6bbed to your computer and use it in GitHub Desktop.
@Composable
fun TimeEntry() {
val currentContext = LocalContext.current
val barcodeLauncher = rememberLauncherForActivityResult(ScanContract(), onResult = { result ->
if(result.contents != null) {
Toast.makeText(currentContext, result.contents, Toast.SHORT).show()
}
}
val scanOptions = ScanOptions().apply {
setDesiredBarcodeFormats(ScanOptions.QR_CODE)
setPrompt("Scan a barcode")
setBeepEnabled(true)
setBarcodeImageEnabled(true)
setOrientationLocked(false)
}
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center
){
Button(
modifier = Modifier.align(Alignment.CenterHorizontally),
shape = RoundedCornerShape(10.dp),
onClick = {
barcodeLauncher.launch(scanOptions)
}
) {
Text(text = "Start Scan")
}
}
// Note: add these dependencies to the build.gradle
// qr code reader & generator - https://github.com/journeyapps/zxing-android-embedded
// implementation('com.journeyapps:zxing-android-embedded:4.3.0') { transitive = false }
// implementation 'com.google.zxing:core:3.4.1'
// in the Androidmanifest.xml add this line
// <activity
// android:name="com.journeyapps.barcodescanner.CaptureActivity"
// android:screenOrientation="portrait"
// tools:replace="screenOrientation" />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment