package com.zhjq.utils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class QRCodeUtils { /** * 生成二维码 * * @param content 二维码的内容 * @return BitMatrix对象 */ public static BitMatrix createCode(String content) throws IOException { //二维码的宽高 int width = 200; int height = 200; //其他参数,如字符集编码 Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //容错级别为H hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //白边的宽度,可取0~4 hints.put(EncodeHintType.MARGIN, 0); BitMatrix bitMatrix = null; try { //生成矩阵,因为我的业务场景传来的是编码之后的URL,所以先解码 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); //bitMatrix = deleteWhite(bitMatrix); } catch (WriterException e) { e.printStackTrace(); } return bitMatrix; } /** * 生成带有底部文字的二维码 * * @param content 二维码的内容 * @param text 底部文字 * @throws IOException */ public static byte[] createCodeWithText(String content, String text) throws IOException { // 二维码的宽高 int width = 300; int height = 300; // 其他参数,如字符集编码 Map hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.MARGIN, 0); BitMatrix bitMatrix = null; try { // 生成二维码矩阵 QRCodeWriter writer = new QRCodeWriter(); bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { e.printStackTrace(); } // 创建图像并绘制二维码 int imgWidth = width; int imgHeight = height + 30; // 增加额外空间用于文本 BufferedImage image = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = image.createGraphics(); // 设置背景色 graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, imgWidth, imgHeight); // 绘制二维码 graphics.setColor(Color.BLACK); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (bitMatrix.get(x, y)) { graphics.fillRect(x, y, 1, 1); } } } // 添加底部文字,使用合适的字体 graphics.setColor(Color.BLACK); // 使用支持中文的字体 Font font = new Font("Microsoft YaHei", Font.BOLD, 16); graphics.setFont(font); FontMetrics metrics = graphics.getFontMetrics(font); int textWidth = metrics.stringWidth(text); // 在底部居中绘制文本 graphics.drawString(text, (imgWidth - textWidth) / 2, height + 20); graphics.dispose(); // 保存图像 try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // ImageIO.write(image, "png", new File(filePath)); ImageIO.write(image, "png", outputStream); return outputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 删除生成的二维码周围的白边,根据审美决定是否删除 * * @param matrix BitMatrix对象 * @return BitMatrix对象 */ private static BitMatrix deleteWhite(BitMatrix matrix) { int[] rec = matrix.getEnclosingRectangle(); int resWidth = rec[2] + 1; int resHeight = rec[3] + 1; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); resMatrix.clear(); for (int i = 0; i < resWidth; i++) { for (int j = 0; j < resHeight; j++) { if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j); } } return resMatrix; } }