【QR code of JAVA】

QR Code, also known as QR Code, QR stands for Quick Response, is a super popular coding method on mobile devices in recent years. It can store more information and represent more data than traditional Bar Code barcodes. Types of.

2-dimensional bar code (2-dimensional bar code) is a black-and-white graphic that is distributed on a plane (two-dimensional direction) with a specific geometric figure according to a certain rule to record data symbol information; it is ingenious in code compilation. It makes use of the concept of "0" and "1" bit streams that constitute the internal logic basis of the computer, and uses several geometric shapes corresponding to binary to represent text and numerical information. Automatic information processing: It has some commonalities of barcode technology: each code system has its specific character set; each character occupies a certain width; has a certain verification function, etc. At the same time, it also has the function of automatically identifying the information of different lines, and processing the rotation change points of the graphics.

 

 

advantage

1. High-density coding, large information capacity.

2. Wide range of codes.

3. It has strong fault tolerance and error correction function.

4. The decoding reliability is high.

5. Encryption measures can be introduced.

6. Low cost, easy to make and durable.

 

 

Features

1. High-density coding, large information capacity: it can accommodate up to 1850 uppercase letters or 2710 numbers or 1108 bytes, or more than 500 Chinese characters, which is about dozens of times higher than the information capacity of ordinary barcodes.

2. Wide range of coding: The barcode can encode digitized information such as pictures, voices, texts, signatures, fingerprints, etc., and represent them with barcodes; it can represent multiple languages; it can represent image data.

3. Strong fault tolerance and error correction function: This makes it possible to correctly read the 2D barcode when it is partially damaged due to perforation, contamination, etc., and the information can still be recovered when the damaged area reaches 50%.

4. High decoding reliability: It is much lower than the ordinary barcode decoding error rate of two millionths, and the bit error rate does not exceed one millionth.

5. Encryption measures can be introduced: good confidentiality and anti-counterfeiting.

6. Low cost, easy to make and durable.

7. The shape and size of the barcode symbol are variable.

8. 2D barcodes can be read with a laser or CCD reader.



 

 

Steps to generate QR code using JAVA

1) Introduce Maven coordinates

<dependency>

  <groupId>com.google.zxing</groupId>

  <artifactId>core</artifactId>

  <version>3.0.0</version>

  </dependency>

  <dependency>

  <groupId>com.google.zxing</groupId>

  <artifactId>javase</artifactId>

  <version>3.0.0</version>  

  </dependency>

2) Write core tool classes and Servlet

import java.io.IOException;

import java.util.Hashtable;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

 

import com.google.zxing.BarcodeFormat;

import com.google.zxing.EncodeHintType;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.WriterException;

import com.google.zxing.common.BitMatrix;

  

public class MatrixToImageServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {  

    static final long serialVersionUID = 1L;  

  

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  

        response.setHeader("Pragma", "No-cache");  

        response.setHeader("Cache-Control", "no-cache");  

        response.setDateHeader("Expires", 0);  

        response.setContentType("image/jpeg");  

          

        //save session session  

       // HttpSession session = request.getSession(true);  

     

        String text = "QR code content: http://gaojingsong.iteye.com/"; // QR code content  

        int width = 300; // QR code image width  

        int height = 300; // QR code image height  

        String format = "gif";// QR code image format  

          

        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();  

        hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // The character set encoding used by the content  

          

        BitMatrix bitMatrix = null;

try {

bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);

} catch (WriterException e) {

// TODO Auto-generated catch block

e.printStackTrace ();

}  

        // generate QR code  

        MatrixToImageWriterUtils.writeToStream(bitMatrix, format, response.getOutputStream());  

    }  

}  

 

3) Configure Servlet

<servlet>  

   <servlet-name>MyMatrixToImage</servlet-name>  

   <servlet-class>MatrixToImageServlet</servlet-class>  

    </servlet>  

    <servlet-mapping>  

   <servlet-name>MyMatrixToImage</servlet-name>  

   <url-pattern>/qrCode</url-pattern>  

    </servlet-mapping> 

4) Verify



 

 

 

 

Original is not easy, welcome to reward, please look for the correct address, beware of counterfeiting



 

 


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939043&siteId=291194637
Recommended