Install KScan via Gradle
mainTo use KScan in your Compose Multiplatform project, add the dependency to your commonMain source set in your build.gradle.kts file.
implementation("io.github.ismai117:KScan:$version")repository·main·Indexed 43 days ago
https://github.com/ismai117/kscanA Compose Multiplatform library for barcode scanning across Android, iOS, and Desktop. It provides a ScannerView composable for live camera scanning with torch and zoom control via ScannerController, as well as a scanImage function for processing static images.
To use KScan in your Compose Multiplatform project, add the dependency to your commonMain source set in your build.gradle.kts file.
implementation("io.github.ismai117:KScan:$version")For Android, iOS, and macOS, you must request and grant camera permissions before displaying the ScannerView.
On iOS and macOS, you must add the NSCameraUsageDescription key to your Info.plist file to explain why the app needs camera access.
<key>NSCameraUsageDescription</key>
<string>Camera access is required for barcode scanning</string>To programmatically control the camera's torch (flashlight) and zoom level, use a ScannerController. You can pass this controller to ScannerView via the scannerController parameter.
scannerController.setTorch(Boolean) and check scannerController.torchEnabled.scannerController.setZoom(Float) with a value between 1f and scannerController.maxZoomRatio.val scannerController = remember { ScannerController() }
ScannerView(
codeTypes = listOf(BarcodeFormat.FORMAT_ALL_FORMATS),
scannerUiOptions = null,
scannerController = scannerController
) { result ->
// handle result
}
// Torch control
Button(onClick = { scannerController.setTorch(!scannerController.torchEnabled) }) {
Text("Toggle Torch")
}
// Zoom control
Slider(
value = scannerController.zoomRatio,
onValueChange = scannerController::setZoom,
valueRange = 1f..scannerController.maxZoomRatio
)If you want to implement your own user interface instead of using the library's default overlay, set scannerUiOptions to null in the ScannerView composable.
ScannerView(
codeTypes = listOf(BarcodeFormat.FORMAT_QR_CODE),
scannerUiOptions = null
) { result ->
// handle result
}The ScannerView composable provides a live camera feed for scanning barcodes. You can specify which BarcodeFormat types to look for using the codeTypes parameter. The trailing lambda provides a BarcodeResult which you should handle using a when expression to account for OnSuccess, OnFailed, and OnCanceled states.
ScannerView(
codeTypes = listOf(BarcodeFormat.FORMAT_QR_CODE, BarcodeFormat.FORMAT_EAN_13)
) { result ->
when (result) {
is BarcodeResult.OnSuccess -> {
println("Barcode: ${result.barcode.data}")
}
is BarcodeResult.OnFailed -> {
println("Error: ${result.exception.message}")
}
BarcodeResult.OnCanceled -> {
println("Canceled")
}
}
}Use the scanImage function to process static images (like those from a gallery or screenshot) instead of using a live camera feed. This function requires a ByteArray of the image (PNG or JPEG) and a list of codeTypes. You can optionally provide a filter lambda to decide which detected barcodes to return.
scanImage(
imageBytes = imageBytes, // ByteArray of the image (PNG, JPEG)
codeTypes = listOf(BarcodeFormat.FORMAT_ALL_FORMATS),
filter = { barcode -> true }, // Optional: filter detected barcodes
) { result ->
when (result) {
is BarcodeResult.OnSuccess -> {
println("Barcode: ${result.barcode.data}")
println("Format: ${result.barcode.format}")
}
is BarcodeResult.OnFailed -> {
println("Error: ${result.exception.message}")
}
BarcodeResult.OnCanceled -> {
// Not applicable for image scanning
}
}
}KScan supports various 1D and 2D barcode formats. You can scan all supported types by using BarcodeFormat.FORMAT_ALL_FORMATS.
1D Barcodes:
- CODE_128
- CODE_39
- CODE_93
- CODABAR
- EAN_13
- EAN_8
- ITF
- UPC_A
- UPC_E
2D Barcodes:
- QR_CODE
- AZTEC
- DATA_MATRIX
- PDF417