本文共 5152 字,大约阅读时间需要 17 分钟。
本文介绍一个功能强大的Java QR Code工具类QRCodeUtil,该工具类可用于快速生成带有图片嵌入的二维码,并支持解码操作。本文将详细介绍工具类的功能实现原理、使用方法以及示例代码。
二维码生成
二维码解码
图片处理
文件管理
在项目中添加依赖项:
com.google.zxing core 3.3.0
package com.example.qrcode;public class QRCodeUtil { // 字符集设置 private static final String CHARSET = "utf-8"; // 二维码尺寸 private static final int QRCODE_SIZE = 300; // LOGO尺寸 private static final int WIDTH = 60; private static final int HEIGHT = 60; // 二维码生成相关 public static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception { // 设置编码字符集 Map hints = new HashMap<>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.CHARACTER_SET, CHARSET); hints.put(EncodeHintType.MARGIN, 1); // 生成二维码矩阵 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); // 创建图片 BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB); for (int x = 0; x < bitMatrix.getWidth(); x++) { for (int y = 0; y < bitMatrix.getHeight(); y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } // 处理图片嵌入或压缩 if (imgPath == null || "".equals(imgPath)) { return image; } // 插入图片 insertImage(image, imgPath, needCompress); return image; } // 插入图片逻辑 private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception { File file = new File(imgPath); if (!file.exists()) { System.err.println("图片文件不存在:" + imgPath); return; } // 读取图片 Image src = ImageIO.read(file); // 处理图片缩放 if (needCompress) { int width = src.getWidth(null); int height = src.getHeight(null); if (width > WIDTH || height > HEIGHT) { // 缩放图片 Image scaledImage = src.getScaledInstance(WIDTH, HEIGHT, Image.SCALE_SMOOTH); BufferedImage tag = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(scaledImage, 0, 0, null); g.dispose(); src = scaledImage; } } // 绘制图片到二维码 Graphics2D graph = source.createGraphics(); int x = (QRCODE_SIZE - src.getWidth(null)) / 2; int y = (QRCODE_SIZE - src.getHeight(null)) / 2; graph.drawImage(src, x, y, WIDTH, HEIGHT, null); // 绘制圆角边框 Shape shape = new RoundRectangle2D.Float(x, y, WIDTH, HEIGHT, 6, 6); BasicStroke stroke = new BasicStroke(3f); graph.setStroke(stroke); graph.draw(shape); graph.dispose(); } // 生成二维码图片 public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception { BufferedImage image = createImage(content, imgPath, needCompress); mkdirs(destPath); ImageIO.write(image, "JPG", new File(destPath)); } // 创建目录 private static void mkdirs(String destPath) { File file = new File(destPath); if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } } // 其他方法(省略部分代码)} public static String decode(File file) throws Exception { BufferedImage image = ImageIO.read(file); if (image == null) { return null; } BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Map hints = new Hashtable<>(); hints.put(DecodeHintType.CHARACTER_SET, CHARSET); Result result = new MultiFormatReader().decode(bitmap, hints); return result.getText();} // 示例:生成带有图片的二维码QRCodeUtil.encode("http://example.com", "path/to/image.jpg", "output/jpg", true); // 示例:读取二维码内容String result = QRCodeUtil.decode(new File("path/to/output.jpg")); 本工具类基于Zing图形库,依赖项配置如下:
com.google.zxing core 3.3.0
public class QrCodeTest { public static void main(String[] args) throws Exception { // 二维码内容 String text = "https://blog.csdn.net/liuno0"; // 图片路径 String imgPath = "E:/图像/juan_/juan_backup3.jpg"; // 输出路径 String destPath = "D:/liunn/liunn.jpg"; // 生成二维码 QRCodeUtil.encode(text, imgPath, destPath, true); // 解码二维码 String decodedText = QRCodeUtil.decode(new File(destPath)); System.out.println("解码结果:" + decodedText); }} 通过本文介绍的QRCodeUtil工具类,开发人员可以快速实现二维码的生成与解码功能。工具类支持图片嵌入、尺寸自定义、错误校正等高级功能,适合在多种应用场景中使用。