QRCode (Quick Response Code) là một loại mã vạch hai chiều.
Bước 1: Nhập thư viện JAR
Đường dẫn nguồn: https://github.com/zxing/zxing/releases
Nếu không có tệp JAR sẵn sàng, bạn có thể tạo dự án Java từ mã nguồn, sao chép mã nguồn vào dự án và xuất tệp JAR. Khi xuất tệp JAR, có thể gặp lỗi, hãy bỏ qua chúng. Bước 2: Viết mã để tạo mã QR
Đường dẫn nguồn: https://github.com/zxing/zxing/releases
Nếu không có tệp JAR sẵn sàng, bạn có thể tạo dự án Java từ mã nguồn, sao chép mã nguồn vào dự án và xuất tệp JAR. Khi xuất tệp JAR, có thể gặp lỗi, hãy bỏ qua chúng. Bước 2: Viết mã để tạo mã QR
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCodeGenerator {
public static void main(String[] args) {
String content = "https://github.com/zxing/zxing/releases"; // Nội dung của mã QR
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // Cấp độ sửa lỗi
hints.put(EncodeHintType.MARGIN, 2); // Độ rộng viền, mặc định là 5
try {
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);
Path path = new File("C:/Users/Administrator/Desktop/qrcode.png").toPath(); // Đường dẫn và tên tệp chứa mã QR
MatrixToImageWriter.writeToPath(matrix, "png", path); // Định dạng ảnh là PNG
} catch (Exception e) {
e.printStackTrace();
}
}
}
Cấp độ sửa lỗi càng cao, dữ liệu được lưu trữ càng ít, và yêu cầu về độ rõ nét của mã QR càng thấp.
L: 7% M: 15% Q: 25% H: 30%
Ngoài zxing, bạn cũng có thể sử dụng JavaScript hoặc jquery.qrcode.js để tạo mã QR.
Bước 3: Viết mã để phân tích mã QR
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
public class QRCodeReader {
public static void main(String[] args) throws IOException, NotFoundException {
MultiFormatReader reader = new MultiFormatReader();
File file = new File("C:/Users/Administrator/Desktop/qrcode.png");
BufferedImage image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
Result result = reader.decode(binaryBitmap, hints);
System.out.println("Kết quả phân tích: " + result.toString());
System.out.println("Định dạng mã QR: " + result.getBarcodeFormat());
System.out.println("Nội dung văn bản: " + result.getText());
}
}