KScan

repository·main·Indexed 43 days ago

https://github.com/ismai117/kscan

A 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.

Tokens
1.5K
Snippets
7
Records
7
Agent score
6%

What's inside KScan

  1. Configure Camera Permissions

    main

    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>
  2. Control Torch and Zoom with ScannerController

    main

    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.

    • Torch: Use scannerController.setTorch(Boolean) and check scannerController.torchEnabled.
    • Zoom: Use 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
    )
  3. Use ScannerView without default UI

    main

    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
    }
  4. Use ScannerView for basic barcode scanning

    main

    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")
            }
        }
    }
  5. Scan barcodes from static images

    main

    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
            }
        }
    }
  6. Reference: Supported Barcode Formats

    main

    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