August 16, 2020

Implementation of QR code generator in Android

By Mohit Agrawal

Let’s get started with QR code generator in android

qr coder generate android 2

Introduction

In one of my recent projects, I was working on a wallet management app and the requirements were to show user’s details in a QR code. So I found a very easy way of integrating a QR code generator in the android project.

Today’s tutorial is going to be very simple but useful. In this tutorial, I will show you how to generate QR code in your android app using a QRGen which is a 3rd party library.

QRGen: a simple QRCode generation api for java built on top ZXING

What we will build?

Let me show you first that what we are going to build in this QR code generator tutorial. This will give you a better understanding.

We build an android app with the simple UI as shown below.

qr code generatoe android
  • Text Field: The user will give the input text.
  • Button: On the click of this button, we will take input text from the text field and pass it to the QRGen library.
  • Image View: It will display the QR image.

Implementation

1. Install QRGen library

Firstly we need to install the QRGen library to the android project.

Open you app level gradle file and and add this dependency.

dependencies {
    implementation 'com.github.kenglxn.QRGen:android:2.6.0'
}

Now open the project level gradle file and the below maven.

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

2. Creation of UI

Let’s move to the UI part. To make it simple I have added just three UI elements like button, text field, and image view.

Code of activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/editCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter text to create a QR code"/>

    <Button
        android:id="@+id/buttonCreate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Create QR"/>

    <ImageView
        android:id="@+id/imagePreview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>
</LinearLayout>

3. Generation of the QR

This is an important step because here we will see how to use the QRGen library. Let me explain this in detail.

val bitmap = QRCode.from("Your input text").withSize(1000, 1000).bitmap()
  • .from("Your input text"): Pass your text which you want to convert in the QR code.
  • .withSize(1000, 1000): The dimension of the required bitmap image.
  • .bitmap(): Returns the QR in bitmap format.

If your input text is not English then you can set the character set as well. Let’s take an example. Suppose I have to convert Hindi text in the QR code then I will pass the UTF-8 charset like this.

val bitmap = QRCode.from(text).withCharset("UTF-8").withSize(1000, 1000).bitmap()

Code for MainActivity.kt.

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import net.glxn.qrgen.android.QRCode

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        buttonCreate.setOnClickListener {
            val text = editCode.text.toString()

            if (text.isEmpty()) {
                Toast.makeText(this, "Enter Text",
                        Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            /*
            * Generate bitmap from the text provided,
            * The QR code can be saved using other methods such as stream(), file(), to() etc.
            * */
            val bitmap = QRCode.from(text).withCharset("UTF-8").withSize(1000, 1000).bitmap()
            imagePreview.setImageBitmap(bitmap)
            hideKeyboard()
        }
    }

    fun hideKeyboard() {
        val view = this.currentFocus
        if (view != null) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }
}

Run your code and try to generate the QR code. If you facing any issue then please let me know it in the comment section.

Video Tutorial

Conclusion

I have shown you the best way of generating the QR code in the android. If you liked this tutorial then please show some love in the comment section.

Suggested post: Learn how to implement Lottie progress bar animation.

Subscribe YouTube: More tutorials like this

I hope this blog post is useful for you, do let me know your opinion in the comment section below.
I will be happy to see your comments down below 👏.
Thanks for reading!!!

FAQs

  1. What is QR code?

    A QR code is a type of matrix barcode first designed in 1994 for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached. (source: Wikipedia)qr coder generate android 2

  2. How to generate QR code in android?

    There are many ways to generate QR code in the android. But the most easiest way which I found is QRGen library. You just need pass the string value to this library and it will return you the QR image.
    It can be customised according to the requirements as well.

  3. How to generate QR code for Hindi text?

    The simplest way of generating the QR code for Hindi text is to use QRGen library. You can set any charset value for your input text. To use Hindi text you need to use UTF-8 charset.