3

Generate QR code in android using Zxing library in Android Studio example tutorial

How to make QR code in android using EditText entered string or integer value at application run time on button click.

QR code also known as Quick Response Code obtain an array of white and black color small blocks mostly used to store or convert any kind of URL , string name and integer value into blocks mode. QR code is now very much popular in android development because with the use of it android developers can easily determine any android device and connect them with each other without typing their username or password. So here is the complete step by step tutorial for Generate QR code in android using Zxing library in Android Studio example tutorial.

What we are doing in this project : We are creating an android application with the use of Zxing library and inside the app user can just simply enter any kind of value ( URL, NAME, NUMBER ) and click on button, After click it will automatically convert the entered value to QR code.

android-project-download-code-button

Follow all the below steps very carefully to add Zxing library in your project.

1. Open your Project’s build.gradle ( Module : app ) and  add com.android.support:appcompat-v7:23.4.0  library file.

build-gradle-app

2. Here is the code to add appcompat library inside build.gradle file.

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 testCompile 'junit:junit:4.12'
 compile 'com.android.support:appcompat-v7:24.0.0'

 compile 'com.google.zxing:core:3.2.1'

}

Screenshot after add this code into build.gradle file.

zxing-library-android

Here you go now our library is successfully imported next step is to start coding.

How to Generate QR code in android using Zxing library in Android Studio example tutorial.

Code for MainActivity.java file.

package com.android_examples.generateqrcode_android_examplescom;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    Button button;
    EditText editText;
    String EditTextValue ;
    Thread thread ;
    public final static int QRcodeWidth = 500 ;
    Bitmap bitmap ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView)findViewById(R.id.imageView);
        editText = (EditText)findViewById(R.id.editText);
        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EditTextValue = editText.getText().toString();

                try {
                    bitmap = TextToImageEncode(EditTextValue);

                    imageView.setImageBitmap(bitmap);

                } catch (WriterException e) {
                    e.printStackTrace();
                }

            }
        });
    }


    Bitmap TextToImageEncode(String Value) throws WriterException {
        BitMatrix bitMatrix;
        try {
            bitMatrix = new MultiFormatWriter().encode(
                    Value,
                    BarcodeFormat.DATA_MATRIX.QR_CODE,
                    QRcodeWidth, QRcodeWidth, null
            );

        } catch (IllegalArgumentException Illegalargumentexception) {

            return null;
        }
        int bitMatrixWidth = bitMatrix.getWidth();

        int bitMatrixHeight = bitMatrix.getHeight();

        int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

        for (int y = 0; y < bitMatrixHeight; y++) {
            int offset = y * bitMatrixWidth;

            for (int x = 0; x < bitMatrixWidth; x++) {

                pixels[offset + x] = bitMatrix.get(x, y) ?
                        getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

        bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);
        return bitmap;
    }
    }

Code for activity_main.xml layout file.

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.generateqrcode_android_examplescom.MainActivity"
    android:background="#FFF8E1">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:hint="Enter Text Here"
        android:gravity="center"
        />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="CLICK HERE TO generate qr code"
        android:id="@+id/button"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/button"
        />

</RelativeLayout>

Screenshots:

QR-code-1

 

Generate QR code in android using Zxing library in Android Studio example tutorial

Click here to download Generate QR code in android using Zxing library in Android Studio example tutorial project with source code.

3 Comments

  1. Hi,
    I have seen your two programs.
    1. Generate QR code
    2. Scan QR code.
    Now, i want to install two application in two different devices. In one device, QR code will be generated and the other will scan the generated QR code. After this i want to perform some function. But this functions can be done only after pairing the devices via QR code only.
    Is it possible to do. If yes, then please help me with the source code.

  2. The getColor is already deprecated, you may change this line:
    pixels[offset + x] = bitMatrix.get(x, y) ? getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);

    to this:
    pixels[offset + x] = bitMatrix.get(x, y) ? getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);

    and dont forget to import contextcompat like below:
    import android.support.v4.content.ContextCompat;

Leave a Reply

Your email address will not be published. Required fields are marked *