Hey guys in this tutorial we will learn creating Android QR Code Scanner. So if you need something like a Barcode or QR Code Scanner in your android application, then you are at the right place. So lets start creating our Android QR Code Scanner.
Table of Contents
What is a QR Code?
You may already seen QR Code many times. Whatsapp web also uses QR Code for login. QR Code stands for Quick Response Code. QR Code is actually a 2 dimensional bar code. A QR Code consist of black squares over a white background, and it can store some data, which can be read by camera or other imaging devices. Below you can see a sample QR Code.
Generating a QR Code
To scan a QR Code the first thing needed is itself a QR Code. Â So for this tutorial we will generate a Simple QR Code consisting of Name and Address. Â Below is the QR Code I already generated a free service.
To generate your own QR Code you can go to this link. I have given JSON string as QR Data you can see in the below screenshot.
Creating Android QR Code Scanner
The following project will work for Barcode as well.
Creating a new Project
- As always create a new Android Studio project.
- I just created AndroidQRCodeScanner.
Adding Zxing Library
- For reading QR Code we will be using Zxing Library.
- So once your project is loaded go to your app level build.gradle file and add the following dependency.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.1' testCompile 'junit:junit:4.12' //add this dependency compile 'com.journeyapps:zxing-android-embedded:3.4.0' } |
- Now sync your project with gradle.
Creating User Interface
- We have two values (Name, Address) in the QR Code that we generated. So I designed the following layout that will display Name and Address. We also have a button in the layout that will open up camera for scanning the QR Code.
- For designing the above layout you can use the following xml code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<?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="net.simplifiedcoding.androidqrcodescanner.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name" /> <TextView android:id="@+id/textViewName" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="xyz" android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Address" /> <TextView android:id="@+id/textViewAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="xyz" android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> </LinearLayout> <Button android:id="@+id/buttonScan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Scan QR Code" /> </RelativeLayout> |
Building Android QR Code Scanner
Defining View Objects
- Now come inside MainActivity.java and first define the view objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class MainActivity extends AppCompatActivity { //View Objects private Button buttonScan; private TextView textViewName, textViewAddress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //View objects buttonScan = (Button) findViewById(R.id.buttonScan); textViewName = (TextView) findViewById(R.id.textViewName); textViewAddress = (TextView) findViewById(R.id.textViewAddress); } } |
- Now we will attach OnClickListener to button.
Attaching OnClickListener
- Now we will attach OnClickListener to our button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
//implementing onclicklistener public class MainActivity extends AppCompatActivity implements View.OnClickListener { //View Objects private Button buttonScan; private TextView textViewName, textViewAddress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //View objects buttonScan = (Button) findViewById(R.id.buttonScan); textViewName = (TextView) findViewById(R.id.textViewName); textViewAddress = (TextView) findViewById(R.id.textViewAddress); //attaching onclick listener buttonScan.setOnClickListener(this); } @Override public void onClick(View view) { } } |
Android QR Code Scanner
- Now its time to scan the QR Code.
- For scanning modify the code of MainActivity.java as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package net.simplifiedcoding.androidqrcodescanner; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; import org.json.JSONException; import org.json.JSONObject; //implementing onclicklistener public class MainActivity extends AppCompatActivity implements View.OnClickListener { //View Objects private Button buttonScan; private TextView textViewName, textViewAddress; //qr code scanner object private IntentIntegrator qrScan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //View objects buttonScan = (Button) findViewById(R.id.buttonScan); textViewName = (TextView) findViewById(R.id.textViewName); textViewAddress = (TextView) findViewById(R.id.textViewAddress); //intializing scan object qrScan = new IntentIntegrator(this); //attaching onclick listener buttonScan.setOnClickListener(this); } //Getting the scan results @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null) { //if qrcode has nothing in it if (result.getContents() == null) { Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show(); } else { //if qr contains data try { //converting the data to json JSONObject obj = new JSONObject(result.getContents()); //setting values to textviews textViewName.setText(obj.getString("name")); textViewAddress.setText(obj.getString("address")); } catch (JSONException e) { e.printStackTrace(); //if control comes here //that means the encoded format not matches //in this case you can display whatever data is available on the qrcode //to a toast Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show(); } } } else { super.onActivityResult(requestCode, resultCode, data); } } @Override public void onClick(View view) { //initiating the qr code scan qrScan.initiateScan(); } } |
- Now you can try running your application.
- Bingo! Our Android QR Code Scanner is working absolutely fine.
So thats all for this Android QR Code Scanner Tutorial friends. Feel free to leave out your comments if having any confusions or queries. Thank You 🙂