QR Code Generator in JAVA

Short Description...

QR (abbreviated as Quick Response) is the trademark for a type of matrix barcode (or two-dimensional or 2D barcode) first designed 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.

A QR code consists of black modules (square dots) arranged in a square grid on a white background, which can be read by an imaging device (such as a camera, scanner, etc.) and processed using Reed-Solomon error correction until the image can be appropriately interpreted. The required data are then extracted from patterns that are present in both horizontal and vertical components of the image.

Output...
Scan this code and Comment the secret written in it.
Lets Code...

Lets do some magic in JAVA to create own program to generate QR Code. If you are familiar with JAVA then it ok otherwise you must first follow some good tutorial of Java. They can be found at www.javatpoint.com, www.tutorialspoint.com etc

In JAVA creating a whole program to create QR Code is a little bit tricky to do but don't worry I am not going to teach you whole programming. Yes, we are going to use some pre-build libraries by ZXing (get on github).

Prerequisite...

  • Netbeans (IDE) with JAVA latest SDK
  • ZXing QR Code generator libraries (Download)

Libraries...

  • qrgen-1.3.jar
  • zxing-core-2.0.jar
  • zxing-1.7.jar
Get them from official ZXing github repository or download them directly from HERE.

Lets Do it...

Step 1 :  

Create a new JAVA project in Netbeans IDE and name it QRCodeGenerator.

Step 2

Expand the project folder on left of Netbeans IDE and right click on Libraries and select Add JAVR/Folder

Step 3

Move to the folder where you have downloaded jar files on the left and select all three file on the right box and click OK

Step 4

Write the following java code in main java file

/*
 * 
 * QRCodeGenerator.java
 * 
 */

package qrcodegenerator;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;

/**
 * @author anujsharma
 **/
public class QRCodeGenerator {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        
        Scanner sc = new Scanner(System.in);
        
        System.out.println("QR CODE Generator....");
        System.out.println("********************************");
        System.out.println("Enter The String to create QR : ");
        String details = sc.nextLine();
        System.out.println("Enter the path to export QR File : ");
        String path = sc.nextLine();
        
        ByteArrayOutputStream out = QRCode.from(details).to(ImageType.PNG).withSize(300, 300).stream();
        
        File f = new File(path+"qr.png");
        
        FileOutputStream fos = new FileOutputStream(f);
        
        fos.write(out.toByteArray());
        fos.flush();
        
        System.out.println("QR Code Created Successfully.");
    }   
}



Step 5 

Thats it... Now run your file and get your QR Code generated

NOTE:::
User Inputs....
PATH : to produce QR Code at the location given.

Share on Google Plus

4 comments:

  1. I am continually amazed by the amount of information available on this subject. What you presented was well researched and well worded in order to get your stand on this across to all your readers.
    qr code generator free

    ReplyDelete
  2. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. zcode discount

    ReplyDelete
  3. Those strange-looking shape-filled squares you've been seeing lately may not look like much, but they are very likely the next big thing in marketing and branding for U.S. businesses. With a simple snap of a smartphone, that unassuming graphic sends valuable information instantly to potential customers, which for some businesses can mean the difference between a sale or a walk-away. QR Code Creator

    ReplyDelete
  4. I was looking for something like that and found it here. I'm really grateful for your blog post. You will find a lot of approaches after visiting your post.
    Asset Management Software
    Asset Management Software UAE
    Asset Management Software Abu Dhabi
    Asset Tracking Software
    IT Asset Management Software
    Fixed Asset Management Software

    ReplyDelete