How To Generate QR Code In Android

Android 
 

Introduction

 
In this article, we will learn how to generate QR Code in Android. Quick Response Code (QR Code in short) is a 2-Dimensional matrix type barcode used to store data. It is more popular because of its storage capacity and fast readability.
 

Zxing & QR Generator Library

 
Zebra Crossing (Zxing) is an awesome library used to generate and read QR codes in mobile apps. But it is bigger in size. So, we have to go for another one, QR Generator which is a tiny, open-source library. In this article, we will learn how to create & save the QR Code in Android programmatically.
 
Steps
 
I have split this part into 4 steps as in the following.
  • Step 1: Creating a New Project with Empty Activity.
  • Step 2: Setting up the library and manifest.
  • Step 3: Generating a QR Code.
  • Step 4: Saving the QR Code 
Step 1 - Creating a New Project with Android Studio
  1. Open Android Studio and select "Create New Project".
  2. Name the project as per your wish and select your activity template.
     
    Android
     
  3. Click the Finish button to create a new project in Android Studio.
Step 2 - Setting up the library and manifest
  1. Open the App-level Gradle file and import the library.
    1. implementation 'androidmads.library.qrgenearator:QRGenearator:1.0.3'  
  1. Then, click “Sync Now”.
  2. Then, open your Manifest file and add the following permissions. It is used to save QR Code to file storage.
    1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  1. We need to handle runtime permissions from Android Version 6.0.
Step 3 - Generating QR Code
 
In this step, we will learn how to generate QR Code in Android. Open your MainActivity.java file and add the following lines.
  1. QRGEncoder qrgEncoder = new QRGEncoder(inputValue, null, QRGContents.Type.TEXT, smallerDimension);  
  • Here, inputValue is an input to be converted to QR Code.
  • Input Type also can be specified while initializing the library.
  • We can specify the dimensions also.
Then, add the following lines to create a QR Code and encode that into Bitmap Format.
  1. try {  
  2.   // Getting QR-Code as Bitmap  
  3.   bitmap = qrgEncoder.encodeAsBitmap();  
  4.   // Setting Bitmap to ImageView  
  5.   qrImage.setImageBitmap(bitmap);  
  6. catch (WriterException e) {  
  7.   Log.v(TAG, e.toString());  
  8. }  
qrImage is an ImageView used to preview the generated QR code bitmap.
Step 4 - Saving the QR Code
 
QR Generator has an option to save the generated QR Code Bitmap to storage using the following lines.
  1. // Save with location, value, bitmap returned and type of Image(JPG/PNG).  
  2. QRGSaver.save(savePath, edtValue.getText().toString().trim(), bitmap, QRGContents.ImageType.IMAGE_JPEG);  
We can save QR Code in PNG & JPG format also. We have to handle runtime permissions from Android version 6.0.
 
Full Code
 
You can find the full code implementation here.
  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     String TAG = "GenerateQRCode";  
  4.     EditText edtValue;  
  5.     ImageView qrImage;  
  6.     Button start, save;  
  7.     String inputValue;  
  8.     String savePath = Environment.getExternalStorageDirectory().getPath() + "/QRCode/";  
  9.     Bitmap bitmap;  
  10.     QRGEncoder qrgEncoder;  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         qrImage = (ImageView) findViewById(R.id.QR_Image);  
  18.         edtValue = (EditText) findViewById(R.id.edt_value);  
  19.         start = (Button) findViewById(R.id.start);  
  20.         save = (Button) findViewById(R.id.save);  
  21.   
  22.         start.setOnClickListener(new View.OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View view) {  
  25.                 inputValue = edtValue.getText().toString().trim();  
  26.                 if (inputValue.length() > 0) {  
  27.                     WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);  
  28.                     Display display = manager.getDefaultDisplay();  
  29.                     Point point = new Point();  
  30.                     display.getSize(point);  
  31.                     int width = point.x;  
  32.                     int height = point.y;  
  33.                     int smallerDimension = width < height ? width : height;  
  34.                     smallerDimension = smallerDimension * 3 / 4;  
  35.   
  36.                     qrgEncoder = new QRGEncoder(  
  37.                             inputValue, null,  
  38.                             QRGContents.Type.TEXT,  
  39.                             smallerDimension);  
  40.                     try {  
  41.                         bitmap = qrgEncoder.encodeAsBitmap();  
  42.                         qrImage.setImageBitmap(bitmap);  
  43.                     } catch (WriterException e) {  
  44.                         Log.v(TAG, e.toString());  
  45.                     }  
  46.                 } else {  
  47.                     edtValue.setError("Required");  
  48.                 }  
  49.             }  
  50.         });  
  51.   
  52.         save.setOnClickListener(new View.OnClickListener() {  
  53.             @Override  
  54.             public void onClick(View v) {  
  55.                 boolean save;  
  56.                 String result;  
  57.                 try {  
  58.                     save = QRGSaver.save(savePath, edtValue.getText().toString().trim(), bitmap, QRGContents.ImageType.IMAGE_JPEG);  
  59.                     result = save ? "Image Saved" : "Image Not Saved";  
  60.                     Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();  
  61.                 } catch (Exception e) {  
  62.                     e.printStackTrace();  
  63.                 }  
  64.             }  
  65.         });  
  66.   
  67.     }  
Download Code
 
You can download the full source code of the article in GitHub. If you like this article, do star the repo in GitHub.