Home > Java > javaTutorial > Java development skills revealed: implementing QR code generation and parsing functions

Java development skills revealed: implementing QR code generation and parsing functions

WBOY
Release: 2023-11-20 16:18:42
Original
818 people have browsed it

Java development skills revealed: implementing QR code generation and parsing functions

Java development skills revealed: Implementing QR code generation and parsing functions

With the rapid development of the mobile Internet, QR codes have become a very common Method of information transmission. We can see QR codes in various scenarios, such as on product labels in shopping malls, billboards on buses, and even on personal business cards. The convenience and legibility of QR codes make them a very popular way of encoding information.

In Java development, generating and parsing QR codes is a very common requirement. This article will reveal some Java development techniques for realizing QR code generation and parsing functions to help developers complete this task more efficiently.

1. Generate QR code

To generate QR code, we can use a Java class library-ZXing (Zebra Crossing), which is a powerful and easy-to-use open source 2D code. QR code generation and parsing library. The following is a simple code example that shows how to use ZXing to generate a QR code containing text information:

import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
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;

public class QRCodeGenerator {

    public static void main(String[] args) {
        String text = "Hello, QR Code!";
        int width = 300;
        int height = 300;
        String format = "png";
        String filePath = "qrcode.png";

        try {
            Map<EncodeHintType, Object> hintMap = new HashMap<>();
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, com.google.zxing.BarcodeFormat.QR_CODE, width, height, hintMap);
            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, bitMatrix.get(x, y) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
                }
            }
            ImageIO.write(image, format, new File(filePath));
            System.out.println("二维码生成成功!");
        } catch (Exception e) {
            System.err.println("二维码生成失败:" + e.getMessage());
        }
    }

}
Copy after login

By running the above code, we can generate a QR code named "qrcode.png" picture. In the code, we can control the size, fault tolerance level, text content, etc. of the QR code by setting parameters.

2. Parsing QR codes

In addition to generating QR codes, we often need to parse existing QR codes. Similarly, we can use the ZXing library to implement QR code parsing. The following is a simple code example that shows how to use ZXing to parse a QR code image and obtain the text information:

import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class QRCodeReader {

    public static void main(String[] args) {
        String filePath = "qrcode.png";

        try {
            BufferedImage image = ImageIO.read(new File(filePath));
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            Result result = new MultiFormatReader().decode(binaryBitmap);
            
            System.out.println("解析结果:" + result.getText());
        } catch (Exception e) {
            System.err.println("二维码解析失败:" + e.getMessage());
        }
    }
}
Copy after login

With the above code, we can convert the QR code image of "qrcode.png" Parse and output the text information to the console.

Summary:

This article introduces how to use the ZXing library to realize the function of generating and parsing QR codes in Java. Through the above code examples, we can see that ZXing provides a very simple and easy-to-use interface to help us quickly generate and parse QR codes. In actual development, we can flexibly adjust the parameters of the QR code according to specific needs, and optimize the generation and parsing efficiency of the QR code according to the actual scenario.

I hope that the content of this article can help developers who need to implement QR code generation and parsing functions in Java development, and also hope that it can provide some help for everyone's technical learning and project development. thanks for reading!

The above is the detailed content of Java development skills revealed: implementing QR code generation and parsing functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template