Skip to main content

QR Code Generate using zxing in spring boot





WHAT IS QR CODE AND HOW TO GENERATE QR CODE USING JAVA:

         QR Code stands for Quick Response Code first design in Japan in 1994. The QR code is the trademark for a type of matrix barcode. The main purpose is to store information and is easily read by the advanced mobile system. The QR code is the two-dimensional bar code which store the piece of information.in the QR code information are encoded in 4 standardized encoding mode (numeric, alphanumeric, byte/binary, and kanji)    to store data efficiently. In Java, we can generate a QR code using zxing API provided by Google .zxing API support 4000 characters to encode. So without wasting any time let begin with an example for a better preference.

PROJECT STRUCTURE:





















MAVEN DEPENDENCIES:

       Put the dependencies on pom.xml

pom.xml:

<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
     <groupId>com.google.zxing</groupId>
     <artifactId>core</artifactId>
     <version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
     <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.0</version>
</dependency>


DevelpoerVillagerApplication.java

package com.dv;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = { "com.dv"})
public class DevelpoerVillagerApplication {

public static void main(String[] args) {
SpringApplication.run(DevelpoerVillagerApplication.class, args);
}

}

         This is the main class,it auto generated class when we build the spring boot application.

DVController.java

package com.dv;

import java.nio.file.FileSystems;
import java.nio.file.Path;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

@RestController
public class DVController {

private static Logger logger = LoggerFactory.getLogger(DVController.class);

@RequestMapping(value = "generate/qr", method = RequestMethod.GET)
public void generateQRCode() {

try {
logger.info("In generateQRCode() called in DVController.");

String dataString = "WHAT IS QR CODE AND HOW TO GENERATE QR CODE USING JAVA";

/* Initialize the com.google.zxing.qrcode.QRCodeWriter class */
QRCodeWriter qrCodeWriter = new QRCodeWriter();

/* QRCodeWriter encode method convert the data string into BitMatrix */
BitMatrix bitMatrix = qrCodeWriter.encode(dataString, BarcodeFormat.QR_CODE, 300, 300);

/* Initialize the path where QR image write */
Path path = FileSystems.getDefault().getPath("D:\\DV_QR.png");
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
System.out.println("Successfully QR code generated in path.");

} catch (Exception e) {
logger.error("Exception in generateQRCode() in DVController{}", e);
e.printStackTrace();
}
}

}


              This is the controller class in which we generate the QR code in png format using zxing API.First we initialize the QRCodeWriter to generate the QR code.QRCodeWriter class encode the dataString into QR bitmatrix image. Using the MatrixToImageWrite class use this bitmatrix and graphically print the image system path.

         Firstly through the scanner class input the information,which we want to store in QR Code.

         QRCodeWriter is a class which has to render a QR Code as a bit matrix 2D array of greyscale values.
QRCodeWriter has a encode method which has to take the data, bar code format, height and width as the parameter and return the response as Bit Matrix.

         Bitmatrix represents the 2D matrix in the format of bits. In bit matrix columns and rows are stored in x and y format of the 1D array.x represents the column and y represent the row bits. The origin is at the top-left. Internally the bits are represented in a 1-D array of 32-bit ints.

  FileSystems.getDefault().getPath("D:\\DV_QR.png"); return the system path variable.in which QR Code image is to be written. 

       MatrixToImageWriter class has the different method, that can write the bit matrix data into a different format.for example :BufferedImage,file system,stream etc.

       Here we want the QRCode image store in system path so we use the writeToPath() method,which has take bit matrix ,image format(jpeg or png etc) and the path variable where you want to store the image as parameter and in response it return void.


OUTPUT:



Comments

Popular posts from this blog

Send FTL In Email Using Spring Boot

                  In this topic, we discuss mail protocol and how to send email using mail library and how can we sent an email with more dynamic and with some more graphical interface and it's really looking good than the traditional email content. Here we use FTL to make email a more graphical experience. We try to show the example of this topic as possible as an easy way. so without wasting any time let's start the topic. WHAT IS FTL AND WHAT'S THE BENEFITS?          FTL(Free marker template) is a Java-based template language which is used to make the more dynamic and graphical experience on screen. FTL discovered by Apache foundation. It focuses on java based dynamic template design. Free marker template file extension is .ftl which is approx. as same as HTML code but the major difference is, we can send the data from Java source classes to show dynamic content. It's the main focus on MVC architec...

Interceptor In Spring boot

WHAT IS INTERCEPTOR?        Interceptor is work as a middle object. Interceptor handles the HTTP request before it executes by the controller's method. If you want to perform some task before the controller start executes the HTTP request then we use to the handler adapter and use by dispatcher servlet. Handler Adapter uses handler interceptor to perform the action. Handler interceptor has various methods which are used to perform the action before handling, after handling and after completion . Using interceptor we can perform the operation before request handled by the controller and after the controller send the request to the client. WHAT IS SPRING BOOT INTERCEPTOR?                 In spring boot to implement interceptor, we must implement the Handler Interceptor interface. Handler Interceptor interface intercepts the client request before the request process in the controller. Handler Interceptor interface has 3 methods ...