Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement scanning of barcode from gallery in android

Here is what i have tried.

I have implemented Zbar Scanner in android application in which I can scan barocde and get result.

I have implemented this in my android project. now I want to implement scanner which scans images(of course Bar code images) from gallery. I know this can be possible anyhow. check this link. It has barcode image scanning.

I have tried to search it out but failed. Please Help me out.

like image 443
Hitesh Kamani Avatar asked Aug 22 '15 10:08

Hitesh Kamani


People also ask

Can you scan a QR code from gallery?

Scan with the Gallery app Open the Gallery app on Huawei phone, tap on the image to reveal the bottom menu. Select “More” then wait for a second until you see the 'Scan QR code in image' option at the bottom of the menu. Tap on the option to allow the phone to scan the QR code from the image.

Can barcodes be scanned through pictures?

The simple answer is yes - if the barcode scanner that you have has what is known as a 2D (two dimensional) imager as its scan engine. Barcode scanners come with two different types of scan engines.


1 Answers

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.

like image 79
Namrata Bagerwal Avatar answered Oct 01 '22 03:10

Namrata Bagerwal