Introduction

Today, you are going to learn about the Google Vision API using Android Studio and we are going to build a barcode reader app to scan the barcodes. Please follow the below steps carefully to build the Barcode Reader App.

Google API

  • Google Vision API is used to find objects like images, faces in photos, and videos, and barcodes.
  • It recognizes the texts and other things that are digitally captured; thus, it is very useful to build our barcode reader app.

Barcode API Overview

  • This app will let us detect the information from a barcode with the help of Vision API.
  • A barcode is classified into 1d Barcode and 2d Barcode.
  • To know more about it, you can visit the Barcode API information page from the Google Developers portal.
Requirements
  • Android Studio
  • How to code
  • Understanding basic functionalities of how API works
  • Free to learn
Steps to follow
Add the below code in "build Gradle" to add library files to our Barcode reader.
  1. compile 'com.google.android.gms:play-services-vision:11.0.2'
Now, we are going to include the Barcode Reader library to work correctly. First, we need to build the Gradle codes and Android Hive. Then, add Barcode Reader code to it.
  1. build.gradle
  2. dependencies {
  3. implementation 'info.androidhive:barcode-reader:1.1.5'
  4. implementation 'com.google.android.gms:play-services-vision:11.0.2'
  5. }
It is time to add Activity fragment to which we can invoke the Camera fragment.
  1. <fragment
  2. android:id="@+id/barcode_scanner"
  3. android:name="info.androidhive.barcode.BarcodeReader"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. app:auto_focus="true"
  7. app:use_flash="false" />
  8. This is code for Camera Actvity
Now, let's add the following code to work our project correctly and name it as BarcodeReader.BarcodeReader.java in class library.
  1. import android.support.v7.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.util.SparseArray;
  4. import com.google.android.gms.vision.barcode.Barcode;
  5. import java.util.List;
  6. import info.androidhive.barcode.BarcodeReader;
  7. public class MainActivity extends AppCompatActivity implements BarcodeReader.BarcodeReaderListener {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_scan);
  12. }
  13. @Override
  14. public void onScanned(Barcode barcode) {
  15. // single barcode scanned
  16. }
  17. @Override
  18. public void onScannedMultiple(List<Barcode> list) {
  19. // multiple barcodes scanned
  20. }
  21. @Override
  22. public void onBitmapScanned(SparseArray<Barcode> sparseArray) {
  23. }
  24. @Override
  25. public void onScanError(String s) {
  26. // now scan error
  27. }
  28. @Override
  29. public void onCameraPermissionDenied() {
  30. // camera permission actually denied
  31. }
  32. }
Add this following Java Code MyClass java file in iy.
  1. import android.app.Application;
  2. import android.text.TextUtils;
  3. import com.android.volley.Request;
  4. public class MyApplication extends Application {
  5. public static final String TAG = MyApplication.class
  6. .getSimpleName();
  7. private RequestQueue mRequestQueue;
  8. private static MyApplication mInstance;
  9. @Override
  10. public void onCreate() {
  11. super.onCreate();
  12. mInstance = this;
  13. }
  14. public static synchronized MyApplication getInstance() {
  15. return mInstance;
  16. }
  17. public RequestQueue getRequestQueue() {
  18. if (mRequestQueue == null) {
  19. mRequestQueue = Volley.newRequestQueue(getApplicationContext());
  20. }
  21. return mRequestQueue;
  22. }
  23. public <T> void addToRequestQueue(Request<T> req, String tag) {
  24. // set the default tag if tag is empty
  25. req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
  26. getRequestQueue().add(req);
  27. }
  28. public <T> void addToRequestQueue(Request<T> req) {
  29. req.setTag(TAG);
  30. getRequestQueue().add(req);
  31. }
  32. public void cancelPendingRequests(Object tag) {
  33. if (mRequestQueue != null) {
  34. mRequestQueue.cancelAll(tag);
  35. }
  36. }
  37. }
Now, add the following XAML code to add the user interface in activity.main.xaml page.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@drawable/bg_gradient"
  8. tools:context="info.androidhive.movietickets.MainActivity">
  9. //akshayrao code
  10. <LinearLayout
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_centerHorizontal="true"
  14. android:layout_centerInParent="true"
  15. android:gravity="center"
  16. android:orientation="vertical"
  17. android:paddingLeft="40dp"
  18. android:paddingRight="40dp">
  19. <ImageView
  20. android:id="@+id/icon"
  21. android:layout_width="100dp"
  22. android:layout_height="100dp"
  23. android:layout_centerHorizontal="true"
  24. android:clickable="true"
  25. android:foreground="?attr/selectableItemBackground"
  26. android:src="@drawable/qrcode"
  27. android:tint="@android:color/white" />
  28. <TextView
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_marginTop="30dp"
  32. android:fontFamily="sans-serif-light"
  33. android:gravity="center"
  34. android:text="Scan the QR code on the poster and book your movie tickets"
  35. android:textColor="@android:color/white"
  36. android:textSize="16dp" />
  37. </LinearLayout>
  38. <Button
  39. android:id="@+id/btn_scan"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_alignParentBottom="true"
  43. android:layout_centerHorizontal="true"
  44. android:layout_marginBottom="40dp"
  45. android:background="@android:color/transparent"
  46. android:foreground="?attr/selectableItemBackground"
  47. android:paddingLeft="20dp"
  48. android:paddingRight="20dp"
  49. android:fontFamily="sans-serif-medium"
  50. android:text="Scan QR Code"
  51. android:textColor="@android:color/white"
  52. android:textSize="18sp" />
  53. </RelativeLayout>
Now, the application has been successfully created. Debug it to get a certain output.
Comment below in case you have any queries. Please like and share if you love it.