QR-Code Scanning Using Android Studio

Introduction

 
Android is one of the most popular operating systems for mobile. In this article, we will learn how to scan QR code without camera options using Android Studio. Android is a kernel-based operating system that allows the users to modify the GUI component and source code.
 
Requirement
  • XML and Java knowledge
  • Android Studio
  • Android Emulator or Android mobile
Step to be followed
 
Carefully follow the below steps to create a QR code scan application using Android Studio. The source code is also attached.
 
Step 1
 
Open Android Studio and create a new project. Give an application name, company domain, check Include C++ option if you wish to use C++ for coding the project, and click Next.
 
image001
 
Step 2
 
Select the Android minimum SDK. It will show the high percentage of people using that SDK. Now, click "Next".
 
image002
Step 3
 
After the process completes, extract the app. Double-clicking on the manifests will show the Android manifest.XML page.
 
image003
 
Step 4
 
We can add some code here. I have given the source code already so you can use that code or write your own code.
 
image004
 
XML Code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ravir.qrcode">  
  3.     <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">  
  4.         <activity android:name=".MainActivity">  
  5.             <intent-filter>  
  6.                 <action android:name="android.intent.action.MAIN" />  
  7.                 <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>  
  8.             <meta-data android:name="com.google.gms.vision.DEPENDENCIES" android:value="barcode" /> </activity>  
  9.         <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application>  
  10. </manifest>  
Step 5
 
In this step, the QR Code is generated. We can go to browse https://www.qrcode-monkey.com/ to open QR code page. Select the Text you wish to write. Just click "Create QR code" and a few seconds later, click on Download PNG.
 
image005
 
Step 6
 
Then, extract the res file, click on Layout down arrow, click to select Activity_Main.xml.
 
image006
 
Write a simple XML code. I have given the code in an attachment but you can use your own code too.
 
XML code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.ravir.qrcode.MainActivity">  
  5.     <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">  
  6.         <ImageView android:id="@+id/imageview" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center_horizontal" />  
  7.         <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:text="Click to Scan" android:id="@+id/btnscan" />  
  8.         <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="20dp" android:text="" android:layout_gravity="center_horizontal" />  
  9.     </LinearLayout>  
  10. </RelativeLayout>  
Step 7
 
Go to the Download page, right click on qr_code.PNG image. Just click on "Copy" to copy this image.
 
image007
 
Step 8
 
Click on res content. There appear four types of pages. Select the drawable page and paste the image copied from the previous step.
 
image008
 
Don’t change any name or location in this process.
 
image009
 
Step 9
 
After selecting Gradle Scripts >>build. gradle (Module.App) add the following small code.
 
image010
 
Script code
  1. compile 'com.android.support:appcompat-v7:26.+'  
  2.  testCompile 'junit:junit:4.12'  
  3. compile 'com.google.android.gms:play-services:9.8.0'  
Step 10
 
In this step, click on Start and select the Select Devices page.
 
image011
 
Step 11
 
Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permission.
 
image012
 
Output
 
image013
 

Summary

 
In this article, you learned how to create QR Scanner using Android Studio including Java Standard libraries.
 
If you have any questions/ feedback/ issues, please write in the comment box.


Similar Articles