Android Barcode Detection from Fast Moving Objects
Assume you apply barcode technology to the logistics conveyor belt for scanning parcels. A problem you may face is how to recognize barcodes from blurred images. Although we can use advanced algorithms to deal with this complicated case, we’d better improve the image quality as possible as we can. A simple way is to adjust the camera shutter speed which is also known as exposure time. Faster shutter speed can avoid motion blur. In this post, I will share how to invoke Android Camera2 APIs to change the shutter speed, as well as how to build a simple Android barcode reader to decode barcodes from fast-moving objects
Android Barcode Reader with Camera2 API
To build a basic Android Camera2 app, we don’t need to reinvent the wheel. Just download or fork the source code of android-Camera2Basic provided by Google.
Get Xiao Ling’s stories in your inbox
Join Medium for free to get updates from this writer.
Create an AlertDialog with a list view for selecting shutter speed:
Get a list of sizes compatible with the requested image format YUV_420_888 and instantiate ImageReader with the opted image size:
In the ImageReader callback function onImageAvailable(), get the image buffer and call the decodeBuffer() function for barcode detection:
Because the listener is invoked on a thread handler, we can not update the UI directly. We can send an update request via the main thread handler:
Add the ImageReader surface to the CaptureRequest.Builder:
mPreviewRequestBuilder.addTarget(mImageReader.getSurface());Start the preview when the camera capture session is ready. To control the shutter speed, turn off the auto-exposure mode:
Now build and run the simple Android barcode reader. If the barcodes cannot be read from the fast-moving objects, you just need to change the shutter speed:
Note: due to the hardware specs, you may get low-quality preview images on some cheap phones when changing the shutter speed to 1/500 seconds or faster.
Source Code
Originally published at https://www.codepool.biz on May 27, 2019.









