- Make sure you have added module reference into your project. 1) New - Import new Module - Select your zxing library. Let the gradle build. Then, Go to File - Project Structure - Select app under.
- Barcode Scanning in Android Apps Barcode scanning is one of the most potentially useful resources you will come across on the Android platform. The ZXing (Zebra Crossing) project is bar far the easiest and most effective way to implement barcode scanning for most development purposes.
In this tutorial I will describe an intent integration, which means that I will ask the system if there is a Barcode Scan app installed, if yes – the scan will be performed, if not – I will be re-directed to the Play Store where I could easily download Barcode Scan app and after the installation, – perform the scan.
It is easy to use Simple barcode scanner it required internet Reads multiple barcodes. Blurred & damaged barcodes. Any orientation & rotation angle. Integrate barcode scanner into Android, that allows an Android device with imaging hardware to scan barcodes or 2-D 2D graphical barcodes and retrieve the data encoded. Scan barcodes on products, or Data Matrix and QR Codes containing URLs. Use the mobile device's native camera to scan a barcode on a book. The barcode's information appears in the barcode field, and an additional section appears with the book's information that was received from our call to the Open Library Books API: Notable Implementation Details. This example shows how to call a specific API using and Integration.
You can use ML Kit to recognize and decode barcodes.
See the ML Kit Material Design showcase app and the ML Kit quickstart sample on GitHub forexamples of this API in use.
There are two ways to integrate barcode scanning: by bundling the model as partof your app, or by using an unbundled model that depends on Google PlayServices. If you select the unbundled model, your app will be smaller, howeverthe bundled model has performance advantages over the unbundled model. See thetable below for details.
Feature | Unbundled | Bundled |
---|---|---|
Implementation | Model is dynamically downloaded via Google Play Services. | Model is statically linked to your app at build time. |
App size | No impact. | About 2.2 MB model size. |
Initialization time | Might have to wait for model to download before first use. | Model is available immediately. |
Performance | V1 model. | V2 model is faster and more accurate. Adds support for broken PDF417 start/stop pattern detection, improving recall by 10.5% |
Before you begin
- In your project-level
build.gradle
file, make sure to include Google's Maven repository in both yourbuildscript
andallprojects
sections. - Add the dependencies for the ML Kit Android libraries to your module's app-level gradle file, which is usually
app/build.gradle
. Choose one of the following dependencies based on your needs:For bundling the model with your app:
For using the model in Google Play Services:
If you choose to use the model in Google Play Services, you can configure your app to automatically download the model to the device after your app is installed from the Play Store. To do so, add the following declaration to your app's
If you don't enable install-time model downloads, the model is downloaded the first time you run the scanner. Requests you make before the download has completed produce no results.AndroidManifest.xml
file:
Input image guidelines
For ML Kit to accurately read barcodes, input images must contain barcodes that are represented by sufficient pixel data.
The specific pixel data requirements are dependent on both the type of barcode and the amount of data that's encoded in it, since many barcodes support a variable size payload. In general, the smallest meaningful unit of the barcode should be at least 2 pixels wide, and for 2-dimensional codes, 2 pixels tall.
For example, EAN-13 barcodes are made up of bars and spaces that are 1, 2, 3, or 4 units wide, so an EAN-13 barcode image ideally has bars and spaces that are at least 2, 4, 6, and 8 pixels wide. Because an EAN-13 barcode is 95 units wide in total, the barcode should be at least 190 pixels wide.
Denser formats, such as PDF417, need greater pixel dimensions for ML Kit to reliably read them. For example, a PDF417 code can have up to 34 17-unit wide 'words' in a single row, which would ideally be at least 1156 pixels wide.
Poor image focus can impact scanning accuracy. If your app isn't getting acceptable results, ask the user to recapture the image.
For typical applications, it's recommended to provide a higher resolution image, such as 1280x720 or 1920x1080, which makes barcodes scannable from a larger distance away from the camera.
However, in applications where latency is critical, you can improve performance by capturing images at a lower resolution, but requiring that the barcode make up the majority of the input image. Also see Tips to improve real-time performance.
1. Configure the barcode scanner
If you know which barcode formats you expect to read, you can improve the speedof the barcode detector by configuring it to only detect those formats.For example, to detect only Aztec code and QR codes, build aBarcodeScannerOptions
object as in the following example:
The following formats are supported:
- Code 128 (
FORMAT_CODE_128
) - Code 39 (
FORMAT_CODE_39
) - Code 93 (
FORMAT_CODE_93
) - Codabar (
FORMAT_CODABAR
) - EAN-13 (
FORMAT_EAN_13
) - EAN-8 (
FORMAT_EAN_8
) - ITF (
FORMAT_ITF
) - UPC-A (
FORMAT_UPC_A
) - UPC-E (
FORMAT_UPC_E
) - QR Code (
FORMAT_QR_CODE
) - PDF417 (
FORMAT_PDF417
) - Aztec (
FORMAT_AZTEC
) - Data Matrix (
FORMAT_DATA_MATRIX
)
2. Prepare the input image
To recognize barcodes in an image, create anInputImage
objectfrom either a Bitmap
, media.Image
, ByteBuffer
, byte array, or a file onthe device. Then, pass the InputImage
object to theBarcodeScanner
's process
method.You can create an InputImage
from different sources, each is explained below.
Using a media.Image
To create an InputImage
object from a media.Image
object, such as when you capture an image from a device's camera, pass the media.Image
object and the image's rotation to InputImage.fromMediaImage()
.
If you use the CameraX library, the OnImageCapturedListener
and ImageAnalysis.Analyzer
classes calculate the rotation value for you.
If you don't use a camera library that gives you the image's rotation degree, you can calculate it from the device's rotation degree and the orientation of camera sensor in the device:
Then, pass the media.Image
object and the rotation degree value to InputImage.fromMediaImage()
:
Using a file URI
To create an InputImage
object from a file URI, pass the app context and file URI to InputImage.fromFilePath()
. This is useful when you use an ACTION_GET_CONTENT
intent to prompt the user to select an image from their gallery app.
Using a ByteBuffer
or ByteArray
To create an InputImage
object from a ByteBuffer
or a ByteArray
, first calculate the image rotation degree as previously described for media.Image
input. Then, create the InputImage
object with the buffer or array, together with image's height, width, color encoding format, and rotation degree:
Using a Bitmap
To create an InputImage
object from a Bitmap
object, make the following declaration:
The image is represented by a Bitmap
object together with rotation degrees.
3. Get an instance of BarcodeScanner
4. Process the image
Pass the image to theprocess
method:Note: If you are using the CameraX
API, make sure to close the ImageProxy
when finish using it,e.g., by adding an OnCompleteListener
to the Task
returned from theprocess
Best Barcode Scanner App
method. See theVisionProcessorBase
class in the quickstart sample app for an example.5. Get information from barcodes
Integrate Barcode Scanner Into Android App Download
If the barcode recognition operation succeeds, a list ofBarcode
objects will be passed to the success listener. EachBarcode
object represents a barcode that was detected in theimage. For each barcode, you can get its bounding coordinates in the inputimage, as well as the raw data encoded by the barcode. Also, if the barcodescanner was able to determine the type of data encoded by the barcode, you canget an object containing parsed data.For example:
Tips to improve real-time performance
If you want to scan barcodes in a real-time application, follow these guidelines to achieve the best framerates:
Don't capture input at the camera’s native resolution. On some devices, capturing input at the native resolution produces extremely large (10+ megapixels) images, which results in very poor latency with no benefit to accuracy. Instead, only request the size from the camera that's required for barcode detection, which is usually no more than 2 megapixels.
If scanning speed is important, you can further lower the image capture resolution. However, bear in mind the minimum barcode size requirements outlined above.
- If you use the
Camera
orcamera2
API, throttle calls to the detector. If a new video frame becomes available while the detector is running, drop the frame. See theVisionProcessorBase
class in the quickstart sample app for an example. - If you use the
CameraX
API, be sure that backpressure strategy is set to its default valueImageAnalysis.STRATEGY_KEEP_ONLY_LATEST
. This guarantees only one image will be delivered for analysis at a time. If more images are produced when the analyzer is busy, they will be dropped automatically and not queued for delivery. Once the image being analyzed is closed by calling ImageProxy.close(), the next latest image will be delivered. - If you use the output of the detector to overlay graphics on the input image, first get the result from ML Kit, then render the image and overlay in a single step. This renders to the display surface only once for each input frame. See the
CameraSourcePreview
andGraphicOverlay
classes in the quickstart sample app for an example. - If you use the Camera2 API, capture images in
ImageFormat.YUV_420_888
format. If you use the older Camera API, capture images inImageFormat.NV21
format.
Next steps
See the ML Kit Material Design showcase app and the ML Kit quickstart sample on GitHub forexamples of this API in use.