Android - How to Generate QR Code in Android Studio free Source Code

Sovary May 8, 2022 4.39K
2 minutes read

We will create a very simple QR Code generator app that will allow us to input text from the text box and generate a QR Code that will show as image. The example below will give you a good idea of what we'll be doing in this tutorial. We are using Java programming language to create the project.

Pre-requisite

Implementation

Step 1: Create New Project

Create a new project in Android Studio, this example we will choose sdk Android 5 and Java as language.

Navigate to File -> New -> Project -> Select Empty Activty -> Name project QRGen -> Finish

Step 2: Add Library in Gradle File

Insert Zxing-Android dependency to android build.gradle file (Module: app).

Navigate to app -> build.gradle(Module: app)

implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

Click sync on right top to download the library

Step 3: Design Layout

For design very sample layout, we will add a button to click generate QR Code, Edit Text to input any text after that we will insert Image View to display back the QR code on screen.

Open the following to edit the layout app -> res -> layout -> activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_gravity="center_horizontal"
    tools:context=".MainActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit_input"
        android:padding="15dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt_generate"
        android:text="Generate"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/iv_qr"
        android:scaleType="fitCenter"/>
</LinearLayout>

Step 4: Implement on MainActivity Java File.

We will implement on Java class. Navigate to app -> java -> package -> MainActivity.java 

package com.cambotutorial.sovary.qrgen;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.os.Bundle;
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;
import com.journeyapps.barcodescanner.BarcodeEncoder;

public class MainActivity extends AppCompatActivity
{
    EditText edit_input;
    Button bt_generate;
    ImageView iv_qr;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
	//initialize variable from view
        edit_input = findViewById(R.id.edit_input);
        bt_generate = findViewById(R.id.bt_generate);
        iv_qr = findViewById(R.id.iv_qr);
        bt_generate.setOnClickListener(v->{
            generateQR();
        });
    }

    private void generateQR()
    {
        String text = edit_input.getText().toString().trim();
        MultiFormatWriter writer = new MultiFormatWriter();
        try
        {
            BitMatrix matrix = writer.encode(text, BarcodeFormat.QR_CODE,600,600);
            BarcodeEncoder encoder = new BarcodeEncoder();
            Bitmap bitmap = encoder.createBitmap(matrix);
	    //set data image to imageview
            iv_qr.setImageBitmap(bitmap);

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

Source code: In description

You might Also Like:

 

Android  Java  Video 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him    

Search