QR code generator is easy to use

insert image description here

Builder Utilities

The following is a simple example of QRCodeUtil, which uses the zxing library to generate QR code images:

import com.google.zxing.BarcodeFormat;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class QRCodeUtil {
    
    
    private static final String DEFAULT_IMAGE_FORMAT = "PNG";
    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 300;

    public static BufferedImage createQRCode(String content) throws Exception {
    
    
        return createQRCode(content, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    public static BufferedImage createQRCode(String content, int width, int height) throws Exception {
    
    
        BitMatrix matrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
    
    
            for (int y = 0; y < height; y++) {
    
    
                image.setRGB(x, y, matrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
            }
        }
        return image;
    }

    public static byte[] toByteArray(BufferedImage image, String format) throws IOException {
    
    
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, format, os);
        return os.toByteArray();
    }

    public static String toBase64String(BufferedImage image, String format) throws IOException {
    
    
        byte[] bytes = toByteArray(image, format);
        return java.util.Base64.getEncoder().encodeToString(bytes);
    }
}

The QRCodeUtilUtilities class contains the following methods:

  • createQRCode(String content): Generate a QR code image with default width and height, and return BufferedImagean object.
  • createQRCode(String content, int width, int height): Generate a QR code image according to the specified width and height, and return BufferedImagea object.
  • toByteArray(BufferedImage image, String format): BufferedImageConvert the object to a byte array, and specify the image format.
  • toBase64String(BufferedImage image, String format): BufferedImageConvert the object to a Base64 encoded string and specify the image format.

You can use these methods to generate QR code images and convert them into byte arrays or Base64 strings as needed. Make sure BufferedImageto close related resources after using the object to avoid resource leaks.

test

The following is a QRCodeUtilcase of using to show how to generate a QR code and save it as an image file and convert it to a Base64 string:

public class QRCodeExample {
    
    
    public static void main(String[] args) {
    
    
        String content = "https://example.com"; // 要生成二维码的内容

        try {
    
    
            // 生成二维码图片
            BufferedImage qrCodeImage = QRCodeUtil.createQRCode(content);
            
            // 将二维码保存为图片文件
            String imagePath = "path/to/save/image.png";  // 设置保存路径和文件名
            File outputFile = new File(imagePath);
            ImageIO.write(qrCodeImage, "PNG", outputFile);
            System.out.println("二维码已保存为图片:" + imagePath);
            
            // 将二维码转换为 Base64 字符串
            String base64Image = QRCodeUtil.toBase64String(qrCodeImage, "PNG");
            System.out.println("Base64 图片数据:" + base64Image);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

In this case, we first specify the content of the QR code to be generated as https://example.com. Then, we call createQRCode(content)the method to generate a QR code image and save it to the specified path.

Next, we call toBase64String(qrCodeImage, "PNG")the method to convert the QR code image into a Base64 string, where the second parameter indicates the image format (PNG format is used here).

Finally, we print the output to show the path saved as an image file and the converted Base64 image data.

Please make sure to modify the save path and file name according to the actual situation before running the sample code. In addition, related dependent packages (such as zxing library) need to be added to enable the code to compile and run smoothly.

Guess you like

Origin blog.csdn.net/weixin_53742691/article/details/131717459