Scanning QR code using Android's Mobile Vision API

折月煮酒 提交于 2019-12-03 23:05:16

Finally I got it working in my side. I wanted to share the process and the code that I have right now to implement the QR code scanning in my application. I am not actually answering your question. However, I did not find any good source of help from StackOverflow regarding how can I implement QR code scanning using Google Vision API. I looked into the tutorial that you pointed in your question. However, the tutorial was not very helpful to me either. Hence I am putting down the classes and the steps to implement the QR code scanning in my application.

First of all, you will need some gradle dependencies. So in your build.gradle file, add the following dependencies.

dependencies {
    compile 'com.android.support:design:25.3.1'
    compile 'com.google.android.gms:play-services-vision:10.2.1'
}

Then, you need to have the following five classes in your project. I am adding the classes here. Please import the missing classes if necessary.

  1. CameraSource.java
  2. CameraSourcePreview.java
  3. BarcodeCaptureActivity.java
  4. BarcodeGraphicTracker.java
  5. BarcodeTrackerFactory.java

Now BarcodeCaptureActivity has a layout which you need to put in your layout folder as well.

Here's the barcode_capture.xml layout that you need.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:orientation="vertical">

    <!-- Do not forget to replace with your package name where the class is located -->
    <com.example.yourpackage.camera.CameraSourcePreview
        android:id="@+id/preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

You will need some strings and an activity entry in manifest for the BarcodeCaptureActivity. Here are the strings that you need to put in your strings.xml.

<!-- QR Code related strings -->
<string name="permission_camera_rationale">Access to the camera is needed for detection</string>
<string name="no_camera_permission">This application cannot run because it does not have the camera permission.  The application will now exit.</string>
<string name="low_storage_error">Face detector dependencies cannot be downloaded due to low device storage</string>
<string name="ok">OK</string>

And the AndroidManifest.xml should have a new entry for the BarcodeCaptureActivity like the following.

<activity
    android:name=".util.scanner.BarcodeCaptureActivity"
    android:theme="@style/Theme.AppCompat" />

Now your setup is done and you are ready to open your camera for scanning a barcode or QR code. Just call the following initiateScan function where necessary.

public static final int RC_BARCODE_CAPTURE = 9001;

public void initiateScan() {
    Intent intent = new Intent(YourActivity.this, BarcodeCaptureActivity.class);
    startActivityForResult(intent, RC_BARCODE_CAPTURE);
}

Please note that, you need to ask for camera permission to the user before you call initiateScan function. On granting the permission of the camera you will call the initiateScan function.

The initiateScan function will open the scanner and then after a successful scan, it will return to the calling Activity or Fragment. So you need to have a onActivityResult function in the calling Activity or Fragment.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == CommonStatusCodes.SUCCESS && requestCode == RC_BARCODE_CAPTURE) {
        if (data == null) return;
        Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
        final String scanResult = barcode.displayValue;
        if (scanResult == null) return;

        doSomethingWithTheScanResult(scanResult);
    }
}

Do not forget to add the CAMERA permission in your AndroidManifest.xml file as well.

<uses-permission android:name="android.permission.CAMERA" />

Hope that helps for easier integration of QR code scanner using Google Vision API. I have added a github project for a sample QR code scanner application. Please have a look.

I'm new to Android development but I followed the tutorial using play services 8.1. Worked.

Code is very similar to yours. Only differences are that I have the meta tag under the application level and I removed .setBarcodeFormats(Barcode.QR_CODE) since it limits it specifically to QR type codes.

Also used the app in landscape since portrait wasn't working for me. Even in landscape for QR codes I sometimes had to slowly move the QR code away from the camera until it was able to recognize it.

Did you enabled internet connection in the device? In order to download data via Google Play services; it requires internet connection over the device however you need not to provide any permission.

Try move the following line

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

to your activity tag in yout manifest right after <intent-filter/>

so it would look like this

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

Lemme know if it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!