How to Read a Barcode from an Image

How to read a barcode from an image

This project might be what you're looking for: ZXing

How to implement scanning of barcode from gallery in android

This is possible now with the new Barcode Scanning Apis available from Google Play Services 7.8 version. It has method to detect barcode passed as a bitmap.
Get path of image from gallery and convert it to bitmap and pass it like below:

     Frame frame = new Frame.Builder().setBitmap(bitmap).build();
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
.build();
if(barcode.isOperational()){
SparseArray<Barcode> sparseArray = barcodeDetector.detect(frame);
if(sparseArray != null && sparseArray.size() > 0){
for (int i = 0; i < sparseArray.size(); i++){
Log.d(LOG_TAG, "Value: " + sparseArray.valueAt(i).rawValue + "----" + sparseArray.valueAt(i).displayValue);
Toast.makeText(LOG_TAG, sparseArray.valueAt(i).rawValue, Toast.LENGTH_SHORT).show();

}
}else {
Log.e(LOG_TAG,"SparseArray null or empty");
}

}else{
Log.e(LOG_TAG, "Detector dependencies are not yet downloaded");
}

In your build.gradle file, include the following under dependencies section:
compile 'com.google.android.gms:play-services:7.8.+'

Following Manifest permissions are must:

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Meta data for google play services:

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

Meta data for first time install/run time dependencies to be downloaded for getting barcode detector operational.

<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="barcode" />

For detailed usage of this api, Refer Github Sample, follow Code Lab, Documentation.

Barcode reading from scanned image

If you can preset a region of interest that contains the code and nothing else, then detection is pretty easy. Scan a few rays across this region and find the white/black and black/white transitions. Then, knowing where the "cells" should be, you known their polarity.

For this to work, you need to frame your cells with two black ones on both ends to make sure to know where it starts/stops (if the scale is fixed, you can do with just a start cell, but I would not recommend this).



Related Topics



Leave a reply



Submit