Skip to content

[Bug report] [Android] Kotlin Delegation is not working with MlKitAnalyzer #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AlexTrotsenko opened this issue Dec 9, 2024 · 1 comment
Assignees

Comments

@AlexTrotsenko
Copy link

Describe the bug
When Kotlin delegation is used as following:

class BarcodeScanningAnalyzer internal constructor(
    mlKitAnalyzer: MlKitAnalyzer, val executor: Executor, barcodeScanner: BarcodeScanner
) : ImageAnalysis.Analyzer by mlKitAnalyzer, LifecycleObserver by barcodeScanner {
}

and later scanning started via:

cameraController.setImageAnalysisAnalyzer(analyzer.executor, analyzer)

No scanning is happening (consumer is never called back).

However when I change the delegation to the direct inheritance of the MlKitAnalyzer - then scanning works as expected:

class BarcodeScanningAnalyzer internal constructor(
    barcodeScanner: BarcodeScanner, val executor: Executor, consumer: Consumer<Result>
) : MlKitAnalyzer(
    listOf<BarcodeScanner>(barcodeScanner),
    /* provides ability drawing easily a overlay/box around the QR Code in real time */
    COORDINATE_SYSTEM_VIEW_REFERENCED,
    executor,
    consumer
), LifecycleObserver by barcodeScanner

SDK Info:

  • SDK Name & Version: com.google.mlkit:barcode-scanning:17.0.2

Smartphone:

  • Device/Simulator: Pixel 9 Pro
  • Device/Simulator OS: Android 15

Development Environment:
IDE: Android Studio Meerkat | 2024.3.1 Canary 4

@xizhang
Copy link

xizhang commented Feb 7, 2025

Hi Alex,

The problem is that when using Kotlin delegation (by), interface methods with default implementations are not automatically forwarded to the delegate object. In ImageAnalysis.Analyzer, updateTransform and getTargetCoordinateSystem have default implementations. Because your BarcodeScanningAnalyzer doesn't explicitly override these methods, the calls from CameraX are not reaching your MlKitAnalyzer instance. This means the necessary transformation matrix isn't being set, causing the barcode scanning to fail.

Solution:

Even with delegation, you must override updateTransform and getTargetCoordinateSystem in your BarcodeScanningAnalyzer and manually forward those calls to the MlKitAnalyzer instance.


class BarcodeScanningAnalyzer internal constructor(
    private val mlKitAnalyzer: MlKitAnalyzer, // Make mlKitAnalyzer private
    val executor: Executor,
    barcodeScanner: BarcodeScanner
) : ImageAnalysis.Analyzer by mlKitAnalyzer, LifecycleObserver by barcodeScanner {

    override fun updateTransform(matrix: Matrix?) {
        mlKitAnalyzer.updateTransform(matrix)
    }

    override fun getTargetCoordinateSystem(): Int {
        return mlKitAnalyzer.targetCoordinateSystem
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants