Build an Android barcode scanner with CameraX
Creating a Barcode Scanner in Android Using CameraX combines a live camera preview, reliable permission handling, and fast barcode recognition. CameraX provides the image pipeline, while Google ML Kit identifies values such as EAN-13, Code 128, and QR codes. Used correctly, the scanner can read a label in a busy warehouse or a suburban takeaway while keeping image processing on the device.
A focused implementation starts with one screen: show the preview, request camera access, analyse only the frames that matter, and return a validated result to the rest of the app. This keeps the sample approachable for Android developers while leaving room for favourites, scan history, enterprise workflows, and the Australian market’s strong reliance on Google Play for Android distribution.
Plan the scanning flow
Before coding, decide what counts as a successful scan. A retail scanner may need only EAN-13 and UPC, while a courier or inventory app may also handle Code 39, Code 128, and QR. Restricting the accepted formats reduces false positives and makes duplicate suppression easier. It also lets you explain clearly why a particular label was rejected.
Think through the physical context as well. In a Coles or Woolworths warehouse, hands may be gloved, cartons may be poorly lit, and a trolley can move quickly. A café in Melbourne might use QR codes for loyalty cards, whereas a small business in Sydney may print Code 128 labels. These situations favour a short scan window, audible feedback, and clear recovery when the camera cannot focus.
Set up CameraX and permissions
Add the CameraX modules for camera-core, camera-camera2, camera-lifecycle, and camera-view. A PreviewView displays the camera feed, while ProcessCameraProvider connects preview and analysis to the activity or fragment lifecycle. Configure ImageAnalysis with STRATEGY_KEEP_ONLY_LATEST so the scanner processes new frames instead of building a backlog.
Declare android.permission.CAMERA in the manifest and request it at runtime through the Activity Result API. Explain why access is needed before showing the system prompt, and offer a settings route if the user permanently denies permission. If the app can provide non-scanning features without a camera, mark the camera feature as optional. Camera frames can contain personal information, so align retention and disclosure practices with the Australian Privacy Act 1988 and applicable privacy rules.
Detect and validate barcodes
ML Kit provides two practical packaging options for recognition. Both decode images on the device, but they differ in how the model reaches the user. This makes the right choice depend on offline requirements and the app’s download size.
| ML Kit option | Model delivery | Practical effect | Choose it when |
|---|---|---|---|
| Bundled | Included in the app package | Works immediately and fully offline, but increases the download size | Scanning must be reliable during the first launch or without connectivity |
| Unbundled | Delivered through Google Play services | Keeps the initial package smaller, with possible first-use download time | The app supports many barcode formats and dependable Play services access |
Configure the scanner for the formats required by your product. During analysis, convert each ImageProxy into an ML Kit InputImage, preserving the rotation supplied by imageInfo.rotationDegrees. Pass the media image and rotation into the recogniser, then close the image proxy after processing finishes. Keeping this work on a background executor prevents dropped frames and an unresponsive preview.
Treat a detected barcode as raw input rather than an immediate success. Validate the expected length and format, reject an empty value, and confirm checksums where the format supports them. Debounce repeated readings of the same label, and clear the current result after a short interval so one physical barcode cannot be added to a cart several times.
Handle results and failure states
Design the result feedback for speed. A green outline, haptic pulse, and short confirmation sound can acknowledge a valid code without interrupting the user. Display the decoded value in plain language, such as an item name, SKU, or tracking number. If the workflow records money, keep the Australian dollar amount and any 10% GST treatment separate from the barcode itself; the printed label normally identifies a product rather than its final checkout price.
Camera access, lighting, focus, distance, and motion all create different failure modes. A permission denial needs a settings action, a dark scene needs an instruction to move towards light, and a crowded shelf may require the user to isolate one label. Keep the preview active while recovery is possible, and provide a manual entry field for damaged stock. For collaboration or record transfer, the share scanned results pattern connects the camera workflow to Android’s Sharesheet while keeping the barcode value under the app’s control.
Test and release the scanner
Test on physical Android devices because camera behaviour varies significantly between manufacturers and price tiers. Cover a low-resolution entry phone, a current flagship, and a tablet used for inventory work. Exercise portrait and landscape rotation, pause and resume, incoming calls, low-light conditions, partial obstruction, and rapid movement. An emulator can verify the interface and permission states, but it cannot reproduce the focus and exposure behaviour of a real lens.
Measure performance rather than relying on a subjective impression. Track analysis latency, successful detections per second, battery use during a long shift, and the number of duplicate results. Keep the camera resolution balanced against recognition accuracy, close every image proxy, and release the analyser when the screen leaves the foreground. Avoid storing camera images unless the feature genuinely requires it, and make the retention period clear to users.
Prepare the Google Play release with a privacy policy, camera permission rationale, and an explanation of any data collected after decoding. Australian businesses may want product names shown in English while preserving GST-inclusive prices in A$. Local wording can also feel more natural: a takeaway workflow might offer “takeaway order”, while an event staff app could reference an “arvo pickup”. Test those strings with Australian users before publishing the production build.
Create the smallest end-to-end version today: add the dependencies, request camera access, render the preview, analyse frames, validate one barcode type, and display the result. Run it against real labels, tighten the recognition settings, and then expand the workflow with history, sharing, or inventory features.
Publish the tested build to Google Play, monitor crash reports and scanning feedback after release, and keep each update focused on measured reliability improvements.