From f6a1bf1d9b19dd8b3750034048f3876d086db1f1 Mon Sep 17 00:00:00 2001 From: ZQN <364596817@qq.com> Date: 星期三, 14 八月 2024 17:11:49 +0800 Subject: [PATCH] 企业二维码添加 --- project-common/src/main/java/com/project/common/utils/qrcode/QRCodeUtil.java | 73 ++++++++++++++++++++++++++++++++++++ 1 files changed, 72 insertions(+), 1 deletions(-) diff --git a/project-common/src/main/java/com/project/common/utils/qrcode/QRCodeUtil.java b/project-common/src/main/java/com/project/common/utils/qrcode/QRCodeUtil.java index 81fa4b6..b34ffe2 100644 --- a/project-common/src/main/java/com/project/common/utils/qrcode/QRCodeUtil.java +++ b/project-common/src/main/java/com/project/common/utils/qrcode/QRCodeUtil.java @@ -2,6 +2,7 @@ import com.google.zxing.*; +import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; @@ -14,7 +15,11 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; import java.util.Hashtable; +import java.util.Map; public class QRCodeUtil { private static final String CHARSET = "utf-8"; @@ -106,7 +111,8 @@ QRCodeUtil.encode(content, null, outputStream, false); } - public static String decode(File file) throws IOException, NotFoundException { + public static String decode(File file) throws IOException, NotFoundException + { BufferedImage image; image = ImageIO.read(file); if (image == null) { @@ -133,5 +139,70 @@ } } + + //浜岀淮鐮佸浘涓婂甫瀛� + public static void contextLoads2(String content, String name, String filePathStr) + { + int qrCodeWidth = 700; + int qrCodeHeight = 700; + int textPadding = 5; // 鏂囨湰涓庝簩缁寸爜涔嬮棿鐨勯棿璺� + int textSize = 30; // 鏂囨湰瀛椾綋澶у皬 + int totalHeight = qrCodeHeight + 300; + + try { + // 鐢熸垚浜岀淮鐮佺殑BitMatrix + BitMatrix bitMatrix = generateQRCode(content, qrCodeWidth, qrCodeHeight); + + // 灏咮itMatrix杞崲涓築ufferedImage + BufferedImage qrCodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix); + + // 鍒涘缓涓�涓柊鐨凚ufferedImage鏉ュ绾充簩缁寸爜鍜屾枃鏈� + BufferedImage combinedImage = new BufferedImage( + qrCodeWidth, 900, BufferedImage.TYPE_INT_RGB); + + // 缁樺埗浜岀淮鐮佸埌鏂扮殑BufferedImage涓� + Graphics2D g2d = combinedImage.createGraphics(); + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, qrCodeWidth, 900); + g2d.drawImage(qrCodeImage, 0, 0, null); + + // 璁剧疆鏂囨湰鏍峰紡 + Font font = new Font("BLACK", Font.PLAIN, textSize); + g2d.setFont(font); + g2d.setColor(Color.BLACK); // 鏂囨湰棰滆壊 + + // 缁樺埗鏂囨湰鍒板浘鐗囦笅鏂� + FontMetrics metrics = g2d.getFontMetrics(); + int textX = (qrCodeWidth - metrics.stringWidth("娌堜笜鎯犱紒鎵ф硶")) / 2; + int textX1 = (qrCodeWidth - metrics.stringWidth(name)) / 2; + int textY = qrCodeHeight + textPadding; + g2d.drawString("娌堜笜鎯犱紒鎵ф硶", textX, textY); + g2d.drawString(name, textX1, textY+50); + + g2d.dispose(); + + // 鎸囧畾瀛樺偍鍥剧墖鐨勮矾寰� + Path filePath = Paths.get(filePathStr); + + // 纭繚鏂囦欢璺緞鐨勭埗鐩綍瀛樺湪 + filePath.getParent().toFile().mkdirs(); + + // 淇濆瓨鍥剧墖鍒版枃浠� + ImageIO.write(combinedImage, "PNG", filePath.toFile()); + + System.out.println("QR Code with text has been generated: " + filePath.toAbsolutePath()); + } catch (WriterException | IOException e) { + e.printStackTrace(); + } + } + + + private static BitMatrix generateQRCode(String text, int width, int height) throws WriterException + { + Map<EncodeHintType, Object> hints = new HashMap<>(); + hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); + return new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); + } + } -- Gitblit v1.9.1