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();
}
}
}
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.
Comments
Post a Comment