Hello,
Based on examples found in the B4X forum that demonstrate MLKit’s Selfie Segmentation and Text Recognition features, I have been experimenting with integrating MLKit’s BarcodeScanner into a B4i project.
In my implementation, I use the low-level camera (LLCamera), which raises a Preview event that provides each frame as a Bitmap. Each bitmap is passed to MLKit’s BarcodeScanner for QR code detection.
The current implementation generally works and correctly detects QR codes, but under certain runtime conditions the app terminates randomly and without any visible exception or error. There is no fixed pattern or reproducible frame count. Sometimes it runs for a while, sometimes it crashes after only a few frames. Logs show no diagnostic entry at all.
This suggests that either a memory leak or a race condition occurs somewhere inside MLKit’s asynchronous processing chain. To mitigate possible concurrency issues, I have already tried several synchronization strategies:
I would appreciate any insight on how to correctly manage memory and threading when repeatedly calling processImage:completion: from the preview stream, or whether MLKit requires a different approach to safely reuse the same BarcodeScanner instance for continuous frame analysis.
Thank you in advance for any analysis or advice!
Update 2025/11/03:
Below is the current version of my test class (QRCodeScanner.bas). The complete example project is attached.
Please find the last version of QRCodeScanner.bas and my example projekt in the post below.
I am aware that there already exists a working QR code scanning implementation for B4i. However, my goal here is different: I am not looking for another ready-made solution, but rather to understand how to make this specific MLKit-based integration run reliably and correctly. I am interested in understanding the internal cause of this random termination and in achieving a stable implementation using MLKit.
Based on examples found in the B4X forum that demonstrate MLKit’s Selfie Segmentation and Text Recognition features, I have been experimenting with integrating MLKit’s BarcodeScanner into a B4i project.
In my implementation, I use the low-level camera (LLCamera), which raises a Preview event that provides each frame as a Bitmap. Each bitmap is passed to MLKit’s BarcodeScanner for QR code detection.
The current implementation generally works and correctly detects QR codes, but under certain runtime conditions the app terminates randomly and without any visible exception or error. There is no fixed pattern or reproducible frame count. Sometimes it runs for a while, sometimes it crashes after only a few frames. Logs show no diagnostic entry at all.
This suggests that either a memory leak or a race condition occurs somewhere inside MLKit’s asynchronous processing chain. To mitigate possible concurrency issues, I have already tried several synchronization strategies:
- serializing image processing calls with semaphores,
- adding short delays (Sleep) between frames,
- ensuring that only one recognition task runs at a time, and
- explicitly releasing temporary Bitmaps and invoking garbage collection after each scan.
I would appreciate any insight on how to correctly manage memory and threading when repeatedly calling processImage:completion: from the preview stream, or whether MLKit requires a different approach to safely reuse the same BarcodeScanner instance for continuous frame analysis.
Thank you in advance for any analysis or advice!
Update 2025/11/03:
Please find the last version of QRCodeScanner.bas and my example projekt in the post below.
I am aware that there already exists a working QR code scanning implementation for B4i. However, my goal here is different: I am not looking for another ready-made solution, but rather to understand how to make this specific MLKit-based integration run reliably and correctly. I am interested in understanding the internal cause of this random termination and in achieving a stable implementation using MLKit.
Last edited: