“This is my fourth day of participating in the First Challenge 2022. For more details: First Challenge 2022.”

This article introduces the generation of two-dimensional code through Java text or URL, using a browser or wechat scan to get text or URL content, super simple method, two steps copy and paste can be used.

Note: The content is text will be displayed directly. If the content is A URL address, it will directly access the corresponding address. Wechat may be blocked when scanning access, such as blocked to check whether the URL address is safe or illegal.

Here generated two-dimensional code output provides two ways: 1, generated two-dimensional code to base64 2, direct output two-dimensional code picture

The two-dimensional code is generated through Google’s TWO-DIMENSIONAL code toolkit. First, the corresponding JAR package is introduced through Maven

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.0</version>
</dependency>
Copy the code

Concrete code implementation

The specific implementation code is as follows. Maven can be used by directly copying and pasting the jar package after importing it correctly. The usage method and generation process are written in the code comments, which are very detailed.

// The color that the qr code needs to use
private int BLACK = 0xFF000000;
private int WHITE = 0xFFFFFFFF;

@Test
public void createQrCodeImg(){
    // The text and address of the QR code to be generated
    String QrCodeStr = "Happy New Year, happy Year of the Tiger.";
    // The generated file path, so write is generated on the desktop, if you need to replace the file path directly in another path can be.
    String imgPath = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator;
    // The name of the generated QR code
    String imgName = "QrCodeStr.jpg";
    // Create a qr code
    try {
        Map<EncodeHintType, String> charcter = new HashMap<>();
        // Set the character set
        charcter.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // Set the type, width, height, and character string to be generated to set the four parameters of the TWO-DIMENSIONAL code
        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(QrCodeStr, BarcodeFormat.QR_CODE, 500.500, charcter);
        // Create a file object
        File file = new File(imgPath, imgName);
        // Qr code pixel, namely set above 500
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        // Create a QR code object
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                // Generate a QR code according to the above defined qr code color codingimage.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE); }}// 1
        // The generated TWO-DIMENSIONAL code image object turns to Base64
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        // Set the image format
        ImageIO.write(image, "png", stream);
        String base64 = Base64.encode(stream.toByteArray());
        // Output base64 encoding after successful conversion
        System.out.println(base64);
        // 2
        // Output the QR code file directly
        ImageIO.write(image, "jpg", file);
        System.out.println("Qr code created successfully file address:" + file);
    } catch(Exception e) { e.printStackTrace(); }}Copy the code

Note: 1. After the TWO-DIMENSIONAL code is generated and transferred to Base64, if you want to use the IMG label to display the content in the front end, you need to add the prefix of the image before the current string, and the image format is set as PNG. 2, output two-dimensional code file, because the address and file name here is written dead, so multiple generation will cover the last file, if you need to generate multiple files to change the generated file name or file generation address.