diff --git a/ebike-operations/src/main/java/com/cdzy/operations/service/impl/EbikeBikeQrServiceImpl.java b/ebike-operations/src/main/java/com/cdzy/operations/service/impl/EbikeBikeQrServiceImpl.java index 46fa0d5..76b7967 100644 --- a/ebike-operations/src/main/java/com/cdzy/operations/service/impl/EbikeBikeQrServiceImpl.java +++ b/ebike-operations/src/main/java/com/cdzy/operations/service/impl/EbikeBikeQrServiceImpl.java @@ -53,7 +53,7 @@ public class EbikeBikeQrServiceImpl extends ServiceImpl增加内容文字 - * - * @param content 二维码内容 - * @param text 文字内容 - * @return Base64编码的二维码 - */ - public static BufferedImage generateQRCode(String content, String text) { - // 1. 生成基础二维码 - try (ByteArrayOutputStream qrStream = QRCode.from(content) - .withSize(300, 300) - .withCharset("UTF-8") - .withErrorCorrection(ErrorCorrectionLevel.H) // 纠错等级(H为最高) - .to(ImageType.PNG) // 输出格式 - .stream()) { - if(text == null || text.isEmpty()){ - throw new RuntimeException("二维码内容为空"); - } - - // 2. 将二维码转换为 BufferedImage - ByteArrayInputStream inputStream = new ByteArrayInputStream(qrStream.toByteArray()); - BufferedImage qrImage = ImageIO.read(inputStream); - - // 3. 创建新 BufferedImage 并绘制文字 - BufferedImage finalImage = new BufferedImage( - qrImage.getWidth(), qrImage.getHeight()+ 20, // 增加文字区域高度, - BufferedImage.TYPE_INT_ARGB - ); - Graphics2D g2d = finalImage.createGraphics(); - g2d.drawImage(qrImage, 0, 0, null); - - // 设置文字样式 - g2d.setFont(new Font("Arial", Font.BOLD, 20)); - g2d.setColor(Color.BLACK); - - // 计算文字位置(底部居中) - int textWidth = g2d.getFontMetrics().stringWidth(text); - int x = (qrImage.getWidth() - textWidth) / 2; - int y = qrImage.getHeight() - 2; // 底部留白 - - // 添加文字说明 - g2d.drawString(text, x, y); - g2d.dispose(); - - // 使用 Java 8+ 的 Base64 编码(避免自动换行问题) - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - ImageIO.write(finalImage, "PNG", stream); - return finalImage; - } catch (Exception e) { - throw new RuntimeException("生成二维码失败", e); - } - } - - public static ByteArrayInputStream generateQRCodeInputStearm(String content, String text) { // 1. 生成基础二维码 try (ByteArrayOutputStream qrStream = QRCode.from(content) @@ -148,4 +93,75 @@ public class QRGenUtil { throw new RuntimeException("生成二维码失败", e); } } + + public static ByteArrayInputStream generateQRCodeWithStyledText(String content, String text) { + try { + // 生成基础二维码 + ByteArrayOutputStream qrStream = QRCode.from(content) + .withSize(300, 300) + .withCharset("UTF-8") + .withErrorCorrection(ErrorCorrectionLevel.H) + .to(ImageType.PNG) + .stream(); + + // 转换为 BufferedImage + BufferedImage qrImage = ImageIO.read(new ByteArrayInputStream(qrStream.toByteArray())); + + int qrWidth = qrImage.getWidth(); + int qrHeight = qrImage.getHeight(); + int textAreaHeight = 60; // 增加文字区域高度 + int totalHeight = qrHeight + textAreaHeight; + + BufferedImage finalImage = new BufferedImage(qrWidth, totalHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = finalImage.createGraphics(); + + // 设置白色背景 + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, qrWidth, totalHeight); + + // 绘制二维码 + g2d.drawImage(qrImage, 0, 0, null); + + // 绘制文字(带样式) + if (text != null && !text.isEmpty()) { + // 设置字体样式 + Font font = new Font("微软雅黑", Font.BOLD, 24); + g2d.setFont(font); + + // 设置抗锯齿 + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + + // 计算文字位置 + FontMetrics metrics = g2d.getFontMetrics(); + int textWidth = metrics.stringWidth(text); + int textX = (qrWidth - textWidth) / 2; + int textY = qrHeight + 20; + + // 绘制文字阴影(可选) + g2d.setColor(Color.LIGHT_GRAY); + g2d.drawString(text, textX + 1, textY + 1); + + // 绘制文字主体 + g2d.setColor(new Color(51, 51, 51)); // 深灰色 + g2d.drawString(text, textX, textY); + + // 可选:添加分隔线 +// g2d.setColor(new Color(200, 200, 200)); +// g2d.setStroke(new BasicStroke(1)); +// g2d.drawLine(20, qrHeight + 15, qrWidth - 20, qrHeight + 15); + } + + g2d.dispose(); + + // 转换为 InputStream + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(finalImage, "PNG", baos); + + return new ByteArrayInputStream(baos.toByteArray()); + + } catch (Exception e) { + throw new RuntimeException("生成二维码失败", e); + } + } }