Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

[BUG] Detection stops working when toggling flash #50

Closed
Hulcsa opened this issue Oct 15, 2022 · 3 comments
Closed

[BUG] Detection stops working when toggling flash #50

Hulcsa opened this issue Oct 15, 2022 · 3 comments
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@Hulcsa
Copy link

Hulcsa commented Oct 15, 2022

Describe the bug
The barcode detection stops working when i toggle the flash via setCameraFlashMode(). This is what i see in the error logs:

2022-10-15 12:20:57.008 23489-23489/hu.egis.greta E/BarcodeImageProcessor: Barcode processing error
    com.google.mlkit.common.MlKitException: This detector is already closed!
        at com.google.mlkit.vision.common.internal.MobileVisionBase.processBase(com.google.mlkit:vision-common@@16.3.0:3)
        at com.google.mlkit.vision.barcode.internal.BarcodeScannerImpl.process(com.google.android.gms:play-services-mlkit-barcode-scanning@@16.1.5:1)
        at uk.co.brightec.kbarcode.processor.BarcodeImageProcessor.detectInImage$kbarcode_release(BarcodeImageProcessor.kt:1)
        at uk.co.brightec.kbarcode.processor.base.ImageProcessorBase.startDetection$kbarcode_release(ImageProcessorBase.kt:30)
        at uk.co.brightec.kbarcode.processor.base.ImageProcessorBase$startDetection$1.invokeSuspend(Unknown Source:12)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

I went through your code and i saw that the updateCameraFeature closes the BarcodeScanner, and it is not recreated. I saw that if i call setBarcodeFormats() then it will trigger a createScanner() so the barcode detection continues, but with this solution sometimes i get a crash:

2022-10-15 12:32:53.847 3210-26680/? E/DropBoxUtil: [AppErrors] null InputStream [CONTEXT service_id=254 ]
    java.io.IOException: null InputStream
        at bupm.c(:com.google.android.gms@223915045@22.39.15 (190408-480654083):23)
        at buno.apply(:com.google.android.gms@223915045@22.39.15 (190408-480654083):5)
        at jxy.apply(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at jym.apply(:com.google.android.gms@223915045@22.39.15 (190408-480654083):5)
        at clsc.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at ctwm.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at cztz.d(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at czub.run(:com.google.android.gms@223915045@22.39.15 (190408-480654083):9)
        at czvl.execute(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at cztx.q(:com.google.android.gms@223915045@22.39.15 (190408-480654083):1)
        at cztx.gn(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at czub.g(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at clro.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at ctwk.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at czvr.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at czxt.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at czwu.run(:com.google.android.gms@223915045@22.39.15 (190408-480654083):3)
        at czxv.run(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at czvt.run(:com.google.android.gms@223915045@22.39.15 (190408-480654083):7)
        at czvl.execute(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at czvt.execute(:com.google.android.gms@223915045@22.39.15 (190408-480654083):9)
        at cztx.q(:com.google.android.gms@223915045@22.39.15 (190408-480654083):1)
        at cztx.gn(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at czvv.c(:com.google.android.gms@223915045@22.39.15 (190408-480654083):5)
        at clrt.h(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at clsd.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at ctwm.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at cztz.d(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at czub.run(:com.google.android.gms@223915045@22.39.15 (190408-480654083):9)
        at czvl.execute(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at cztx.q(:com.google.android.gms@223915045@22.39.15 (190408-480654083):1)
        at cztx.gn(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at czub.g(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at clsa.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at ctwk.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at czvr.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at czxt.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at czwu.run(:com.google.android.gms@223915045@22.39.15 (190408-480654083):3)
        at czxv.run(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at czvt.run(:com.google.android.gms@223915045@22.39.15 (190408-480654083):7)
        at czvl.execute(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at czvt.execute(:com.google.android.gms@223915045@22.39.15 (190408-480654083):9)
        at cztx.q(:com.google.android.gms@223915045@22.39.15 (190408-480654083):1)
        at cztx.gn(:com.google.android.gms@223915045@22.39.15 (190408-480654083):4)
        at czvv.c(:com.google.android.gms@223915045@22.39.15 (190408-480654083):5)
        at clsi.c(:com.google.android.gms@223915045@22.39.15 (190408-480654083):5)
        at clsi.b(:com.google.android.gms@223915045@22.39.15 (190408-480654083):2)
        at com.google.android.gms.stats.service.DropBoxEntryAddedChimeraService.a(:com.google.android.gms@223915045@22.39.15 (190408-480654083):32)
2022-10-15 12:32:53.847 3210-26680/? E/DropBoxUtil:     at com.google.android.gms.framework.tracing.wrapper.TracingIntentService.onHandleIntent(:com.google.android.gms@223915045@22.39.15 (190408-480654083):1)
        at gjl.handleMessage(:com.google.android.gms@223915045@22.39.15 (190408-480654083):0)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.os.HandlerThread.run(HandlerThread.java:67)
@Hulcsa Hulcsa added the bug Something isn't working label Oct 15, 2022
@iTruff
Copy link

iTruff commented Nov 22, 2022

Hi @Hulcsa I was able to solve this issue in my project, still not tested in production but seems like working.

Add these two methods in BarcodeView.kt

    fun turnFlashOn() {
        barcodeScanner.turnFlashOn()
    }

    fun turnFlashOff() {
        barcodeScanner.turnFlashOff()
    }

Add these two methods in BarcodeScanner.kt

    fun turnFlashOn() {
        cameraSource.turnFlashOn()
    }

    fun turnFlashOff() {
        cameraSource.turnFlashOff()
    }

Add these two methods in Camera2Source.kt

    fun turnFlashOn() {
        currentSession?.stopRepeating()

        val captureRequest = cameraDevice
            ?.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
            ?.apply { currentSurfaces?.forEach { addTarget(it) } } ?: return

        captureRequest.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH)
        currentSession?.setRepeatingRequest(captureRequest.build(), null, null)
    }

    fun turnFlashOff() {
        currentSession?.stopRepeating()

        val captureRequest = cameraDevice
            ?.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW)
            ?.apply { currentSurfaces?.forEach { addTarget(it) } } ?: return

        captureRequest.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF)
        currentSession?.setRepeatingRequest(captureRequest.build(), null, null)
    }

@Hulcsa
Copy link
Author

Hulcsa commented Nov 23, 2022

Thanks, it seems to be working! It would be nice if the original lib would implement this.

@alistairsykes
Copy link
Contributor

Thanks for issue and for the community support in providing a solution.

FYI: This library is no longer maintained. Closing and marking wontfix.

@alistairsykes alistairsykes added the wontfix This will not be worked on label Feb 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants