androidkotlinbarcode-scannergoogle-mlkit

ML Kit Analyzer returns incorrect boundingBox coordinates in Landscape mode with LifecycleCameraController


I’m trying to implement a simple barcode scanner using ML Kit Analyzer together with LifecycleCameraController.

The idea is to draw a bounding rectangle around a detected barcode directly on the preview. I’m using COORDINATE_SYSTEM_VIEW_REFERENCED, so ML Kit Analyzer should automatically handle the coordinate transformation between the camera and the preview according to the documentation.

This works perfectly fine in portrait mode. But in landscape mode, the Y coordinates of the bounding box are always off, either too far up or too far down but by roughly the same amount (see screenshots). At first, I thought this is a bug in my code, but I found this example implementation (basically doing exactly what I want): https://github.com/DUMA042/BarsandQ and the issue is reproducible there as well.

In the emulator the offset is very strong. On a real device it’s more subtle, but still clearly visible.

So, is there something I’m missing here? Or might this be a bug in the ML Kit Analyzer?

private fun setupBarcodeScanner(controller: LifecycleCameraController) {
    // ...
    controller.setImageAnalysisAnalyzer(
        mainExecutor,
        MlKitAnalyzer(
            listOf(barcodeScanner),
            ImageAnalysis.COORDINATE_SYSTEM_VIEW_REFERENCED,
            mainExecutor
        ) { result: MlKitAnalyzer.Result? ->
            processBarcodeResults(result, barcodeScanner, previewView)
        }
    )
    // ...
}

// ...

private fun processBarcodeResults(
    result: MlKitAnalyzer.Result?,
    barcodeScanner: BarcodeScanner,
    previewView: PreviewView,
) {
    val barcodes = result?.getValue(barcodeScanner) ?: return
    if (barcodes.isEmpty()) return

    val barcode = barcodes.firstOrNull() ?: return

    Log.d(TAG, "BoundingBox: ${barcode.boundingBox}") // <-- Wrong in Landscape mode
}

Landscape 1 Landscape 2

Portrait works fine

Edit:

This definitely seems to be a bug or regression between cameraX and androidx.camera:camera-mlkit-vision in the dependency. I checked the official CameraX- MlKit example app by Android which is working fine until I start upgrading the dependencies to the latest version ending up with the same result as posted above. The behavior can be noticed from androidx.camera:camera-mlkit-vision:1.4.0-alpha05 on. Version 1.4.0-alpha04 still seems to work perfectly fine in portrait and landscape mode.


Solution

  • Apparently this issue did only occur on Samsung devices and the emulator: https://github.com/android/camera-samples/tree/main/CameraX-MLKit