How to use JAVA code to generate a simple QR code

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right

Article directory


foreword

Everyone should be familiar with the QR code, because we usually use it, today the blogger will teach you how to generate a simple QR code

Come and learn with me,


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is a QR code?

Two-dimensional code is also called two-dimensional barcode. The common two-dimensional code is QR Code. The full name of QR is Quick Response, which is a coding method. It can store more information and represent more data types than the traditional Bar Code.

Two-dimensional bar code/two-dimensional bar code (2-dimensional bar code) is a graphic that uses a specific geometric figure to be distributed on a plane (two-dimensional direction) according to certain rules, black and white, and records data symbol information; It cleverly uses 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, which can be automatically read by image input equipment or photoelectric scanning equipment. To realize automatic processing of information: it has some common features of barcode technology: each code system has its specific character set; each character occupies a certain width; it has a certain verification function, etc. At the same time, it also has the function of automatic identification of information in different rows, and the processing of graphic rotation change points

2. Use steps

1. Introduce dependencies

The code is as follows (example):

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.3</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.10</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.3</version>
</dependency>

2. Start operation

The code is as follows (example):

package com.tm.controller;

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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaoBai
 * @create 2022-10-21 2:51
 */
public class QRCode {

    public static void main(String[] args) throws WriterException, IOException { 


            // create a writer 
            MultiFormatWriter mfw = new MultiFormatWriter(); 
            // set: content 
            String content = "The stars are love letters from the Milky Way to the moon"; 
            // setting: type 
            BarcodeFormat type = BarcodeFormat.QR_CODE; 
            // setting: width 
            int width = 600; 
            // setting: height 
            int height = 600; 
            // setting: additional information map 
            Map map=new HashMap(); 
            // setting: encoding utf-8 
            map.put(EncodeHintType.CHARACTER_SET,"UTF-8");  
            // Setting: QR code error tolerance mechanism l minimum
            map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 
            // Setting: the size of the blank area on both sides of the QR code 
            map.put(EncodeHintType.MARGIN,2); 
            // Draw a virtual QR code to set the parameters just now Put in 
            BitMatrix matrix = mfw.encode(content, type, width, height, map); 
            // Write the information of the stream virtual QR code object into the file 
            // Use black and white. Generally, QR codes are black and white, and other colors can be set Color 
            int black = Color.BLACK.getRGB(); 
            int white = Color.WHITE.getRGB(); 
            // Construct a virtual image object to set the color of width and height fonts 
            BufferedImage image =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB ); 
            // loop to describe four corners 
            for (int x = 0 ; x<width ; x++ ){

                for (int y = 0 ; y<height ; y++){ 

                    image.setRGB(x,y,matrix.get(x,y)?black:white); } 
                } 

            // 
            Set the QR code position 
            File file = new File ("D://image//QRCode.jpg"); 
            // execute 
            ImageIO.write(image, "jpg", file); 


    } 

}

 3. QR code error tolerance rate

 

 

 4. Results presentation


 

Summarize

Each step of the above code is explained String content = '"The stars are the love letters from the Milky Way to the moon" Here you can define what you want to say, there are not many codes and dry goods, hurry up and make one to send to your girlfriend

Guess you like

Origin blog.csdn.net/m0_73093747/article/details/127438381