Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Google Vision API Demo - BarCode Reader Using Android Studio
WhatsApp
Akshayrao V
5y
51.8k
0
1
100
Article
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.
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.
build.gradle
dependencies {
implementation 'info.androidhive:barcode-reader:1.1.5'
implementation 'com.google.android.gms:play-services-vision:11.0.2'
}
It is time to add Activity fragment to which we can invoke the Camera fragment.
<
fragment
android:id
=
"@+id/barcode_scanner"
android:name
=
"info.androidhive.barcode.BarcodeReader"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
app:auto_focus
=
"true"
app:use_flash
=
"false"
/>
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.
import android.support.v
7
.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import com.google.android.gms.vision.barcode.Barcode;
import java.util.List;
import info.androidhive.barcode.BarcodeReader;
public class MainActivity extends AppCompatActivity implements BarcodeReader.BarcodeReaderListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
}
@Override
public void onScanned(Barcode barcode) {
// single barcode scanned
}
@Override
public void onScannedMultiple(List<Barcode> list) {
// multiple barcodes scanned
}
@Override
public void onBitmapScanned(SparseArray<Barcode> sparseArray) {
}
@Override
public void onScanError(String s) {
// now scan error
}
@Override
public void onCameraPermissionDenied() {
// camera permission actually denied
}
}
Add this following Java Code MyClass java file in iy.
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
public class MyApplication extends Application {
public
static
final String TAG = MyApplication.class
.getSimpleName();
private RequestQueue mRequestQueue;
private
static
MyApplication mInstance;
@Override
public void onCreate() {
super
.onCreate();
mInstance = this;
}
public
static
synchronized MyApplication getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the
default
tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
Now, add the following XAML code to add the user interface in activity.main.xaml page.
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:app=
"http://schemas.android.com/apk/res-auto"
xmlns:tools=
"http://schemas.android.com/tools"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:background=
"@drawable/bg_gradient"
tools:context=
"info.androidhive.movietickets.MainActivity"
>
//akshayrao
code
<LinearLayout
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_centerHorizontal=
"true"
android:layout_centerInParent=
"true"
android:gravity=
"center"
android:orientation=
"vertical"
android:paddingLeft=
"40dp"
android:paddingRight=
"40dp"
>
<ImageView
android:id=
"@+id/icon"
android:layout_width=
"100dp"
android:layout_height=
"100dp"
android:layout_centerHorizontal=
"true"
android:clickable=
"true"
android:foreground=
"?attr/selectableItemBackground"
android:src=
"@drawable/qrcode"
android:tint=
"@android:color/white"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginTop=
"30dp"
android:fontFamily=
"sans-serif-light"
android:gravity=
"center"
android:text=
"Scan the QR code on the poster and book your movie tickets"
android:textColor=
"@android:color/white"
android:textSize=
"16dp"
/>
</LinearLayout>
<Button
android:id=
"@+id/btn_scan"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_alignParentBottom=
"true"
android:layout_centerHorizontal=
"true"
android:layout_marginBottom=
"40dp"
android:background=
"@android:color/transparent"
android:foreground=
"?attr/selectableItemBackground"
android:paddingLeft=
"20dp"
android:paddingRight=
"20dp"
android:fontFamily=
"sans-serif-medium"
android:text=
"Scan QR Code"
android:textColor=
"@android:color/white"
android:textSize=
"18sp"
/>
</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.
Android Studio
BarCode Reader
Google Vision API Demo
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.4k people
Download Now!
Learn
View all
Membership not found