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.
Step 2
Select the Android minimum SDK. It will show the high percentage of people using that SDK. Now, click "Next".
Step 3
After the process completes, extract the app. Double-clicking on the manifests will show the Android manifest.XML page.
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.
XML Code
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ravir.qrcode">
- <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
- <meta-data android:name="com.google.gms.vision.DEPENDENCIES" android:value="barcode" /> </activity>
- <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application>
- </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.
Step 6
Then, extract the res file, click on Layout down arrow, click to select Activity_Main.xml.
Write a simple XML code. I have given the code in an attachment but you can use your own code too.
XML code
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- 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">
- <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
- <ImageView android:id="@+id/imageview" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center_horizontal" />
- <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" />
- <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" />
- </LinearLayout>
- </RelativeLayout>
Step 7
Go to the Download page, right click on qr_code.PNG image. Just click on "Copy" to copy this image.
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.
Don’t change any name or location in this process.
Step 9
After selecting Gradle Scripts >>build. gradle (Module.App) add the following small code.
Script code
- compile 'com.android.support:appcompat-v7:26.+'
- testCompile 'junit:junit:4.12'
- compile 'com.google.android.gms:play-services:9.8.0'
Step 10
In this step, click on Start and select the Select Devices page.
Step 11
Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permission.
Output
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.