Android Bar Code/ QR-Code Generator Tutorial Using ZXING Library

This tutorial explains how to generate Bar Code or QR-Code in Android device using ZXING Library.

Introduction

Quick Response(QR) code is a two dimensional barcode(matrix codes) that allows content to be decoded at a high speed. The use of QR code is license-free. This tutorial explains how to implement ZXING library to generate dynamic barcodes into Android applications. We will generate Bar Code into a Image Format.
Barcode

 

Create a new project

To generate Bar Code, we are going to create a new android project. Go to File ⇒ New ⇒ New Projects in Android studio. 

Add ZXING library

We need to add ZXING library in our project, so open build.gradle(Module:app) file and add following code:

repositories{
  maven{
    url "https://jitpack.io"
  }
}

dependencies{
   ..... 
  compile 'com.github.kenglxn.QRGen:android:2.2.0' 
  .....
}

 

Create Layout

We need to add Imageview tag in the layout. We will generate Bar Code as Image format and Imgaeview is used to display bar code.

 
<ImageView android:layout_width="match_parent" 
           android:layout_height="match_parent" 
           android:id="@+id/imageView"/>

 

Create a simple Bar Code/QR-Code

To get QR-Code from text use the following code:

Bitmap myBitmap=QRCode.from("www.studytutorial.in").bitmap();

 

Display Bar Code

To display bar code, initialize the Imageview and Pass bitmap into setImageBitmap() As a parameter.

ImageView myImage=(ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);

barcodes 

Generate Bar code with set of details

If we want to generate Bar Code with some details then we need to use VCard object to set details.

VCard abhay=new VCard("Abhay")
                   .setEmail("anand.abhay1910@gmail.com") 
                   .setAddress("India") 
                   .setTitle("Tutorial") 
                   .setCompany("studytutorial") 
                   .setPhoneNumber("258999") 
                   .setWebsite("www.studytutorial.in");
Bitmap myBitmap=QRCode.from(abhay).bitmap();

 

Change Color of Bar code

To change color, we need to pass background color and code color into .withColor()

Bitmap myBitmap=QRCode.from(abhay)
     .withColor(0xFFFF0000, 0xFFFFFFAA).bitmap();

colorfulbarcode



 


About Abhay Anand

Abhay Anand has experience in the field of Software Development. He can program Android, PHP, JavaScript, Html, Css. He develops web and Android applications. You can reach him at anand.abhay1910@gmail.com
  • vishnu kumar

    Great tutorial, thanks