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 architecture which helps in separating web pages designer fro

Scheduler(Task Executor) In Spring Boot

WHAT IS A SCHEDULER?       Firstly know about what is scheduling. Scheduling is a process that executing a task in a certain time period. A scheduler is a tool, the main objective is to implement the scheduling process. Mainly scheduler is used to perform the batch task for the repeated manner and perform the action of task without any outer other events. IMPLEMENTATION OF SCHEDULER IN SPRING BOOT:        In spring boot we can implement scheduler in multiple ways but here we use the annotation to implement the scheduler. In spring boot we can implement scheduler using @Scheduled annotation. The @Scheduled annotation internally uses the TaskScheduler interface to schedule a task. To enable the @Scheduled annotation we must use @EnableScheduling annotation in the Configuration file.        Let's start with an example of task scheduler. Her we use STS(spring tool suite) to build the spring boot project. First, build a project with web configuration. PROJECT S