From 6c0fd2cb801554f7999d14e45a94ef0f0d0dfb8d Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期五, 16 十月 2026 17:45:33 +0800
Subject: [PATCH] feat(mes): 二维码大批量导出改为后台任务,取消数量上限
---
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/framework/qrcode/QrCodeImageUtils.java | 171 +++++++++++++-------------------------------------------
1 files changed, 41 insertions(+), 130 deletions(-)
diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/framework/qrcode/QrCodeImageUtils.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/framework/qrcode/QrCodeImageUtils.java
index a3fa297..93bb1d1 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/framework/qrcode/QrCodeImageUtils.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/framework/qrcode/QrCodeImageUtils.java
@@ -17,10 +17,7 @@
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -28,9 +25,7 @@
/**
* 浜岀淮鐮佹爣绛惧浘鐗囧伐鍏风被
* <p>
- * 鐢熸垚"浜岀淮鐮� + 鍙充晶鍙鏂囨湰"鐨勫悎鎴愬浘鐗囷細浜岀淮鐮佺紪鐮佸唴瀹圭敱璋冪敤鏂逛紶鍏ワ紙閰嶇疆浜嗘函婧愬湴鍧�鏃朵负
- * 甯� code 鍙傛暟鐨勬函婧� URL锛屾湭閰嶇疆鏃朵负绾爜鍊硷級锛屽彸渚у彔鍔犲叧閿俊鎭枃鏈紝
- * 鍗充娇鏁版嵁搴撲涪澶变篃鑳戒粠鎵撳嵃鍥剧墖杩樺師鍏抽敭淇℃伅銆�
+ * 鐢熸垚浜岀淮鐮佸湪涓娿�佺爜鍊煎湪涓嬬殑绱у噾鏍囩銆備簩缁寸爜鍐呭浠嶄娇鐢ㄨ皟鐢ㄦ柟浼犲叆鐨勬函婧愰摼鎺ャ��
*/
@Slf4j
public final class QrCodeImageUtils {
@@ -38,23 +33,16 @@
private QrCodeImageUtils() {
}
- /** 鎵归噺瀵煎嚭浜岀淮鐮佹暟閲忎笂闄愶紙闃叉涓�娆℃�х敓鎴愯繃澶氬鑷村唴瀛�/鍝嶅簲杩囧ぇ锛� */
- public static final int MAX_QR_EXPORT = 2000;
-
/** 浜岀淮鐮佽竟闀� */
private static final int QR_SIZE = 260;
/** 鍥剧墖鍥涘懆鐣欑櫧 */
private static final int PADDING = 16;
/** 浜岀淮鐮佷笌鏂囨湰闂磋窛 */
- private static final int GAP = 24;
- /** 鏂囨湰鍖哄煙瀹藉害 */
- private static final int TEXT_WIDTH = 380;
+ private static final int GAP = 4;
/** 瀛椾綋澶у皬 */
- private static final int FONT_SIZE = 15;
+ private static final int FONT_SIZE = 24;
/** 琛岄珮 */
- private static final int LINE_HEIGHT = 24;
- /** 瀛楁闂磋窛 */
- private static final int FIELD_GAP = 6;
+ private static final int LINE_HEIGHT = 32;
/**
* 涓枃瀛椾綋鍊欓�夛紙鎸変紭鍏堢骇锛夈��
@@ -76,43 +64,39 @@
/**
* 鐢熸垚浜岀淮鐮佹爣绛惧浘鐗�
*
- * @param code 浜岀淮鐮佸唴瀹癸紙绾爜鍊硷級
- * @param keyValues 鍙犲姞鐨勫彲璇绘枃鏈紙椤哄簭鍗崇粯鍒堕『搴忥級
+ * @param code 浜岀淮鐮佸唴瀹癸紙婧簮閾炬帴鎴栫函鐮佸�硷級
+ * @param displayCode 浜岀淮鐮佷笅鏂规樉绀虹殑鍘熷鐮佸��
* @return 鍚堟垚鍥剧墖
*/
- public static BufferedImage createQrLabel(String code, LinkedHashMap<String, String> keyValues) {
- // 鏃犱腑鏂囧瓧浣撴椂鐩存帴澶辫触锛岄伩鍏嶅鍑轰腑鏂囦负鏂规鐨勬爣绛�
- assertCjkFontAvailable();
+ public static BufferedImage createQrLabel(String code, String displayCode) {
QrConfig config = new QrConfig();
- config.setErrorCorrection(ErrorCorrectionLevel.H); // 鎵撳嵃鏍囩鏄撶(鎹燂紝鐢ㄩ珮瀹归敊
- config.setMargin(1); // 淇濊瘉闈欏尯锛屾壂鐮佺巼
+ config.setErrorCorrection(ErrorCorrectionLevel.H);
+ config.setMargin(4); // 淇濈暀浜岀淮鐮侀潤鍖猴紝淇濊瘉鎵撳嵃鍚庣殑璇嗚鐜�
config.setWidth(QR_SIZE);
config.setHeight(QR_SIZE);
BufferedImage qrImage = QrCodeUtil.generate(code, config);
- Font labelFont = new Font(FONT_NAME, Font.BOLD, FONT_SIZE);
- Font valueFont = new Font(FONT_NAME, Font.PLAIN, FONT_SIZE);
-
- // 绗竴閬嶏細娴嬮噺鏂囨湰鍧楁�婚珮搴︼紝鐢ㄤ簬纭畾鍥剧墖楂樺害
- int textHeight = measureTextHeight(keyValues, labelFont, valueFont);
-
- int height = PADDING * 2 + Math.max(QR_SIZE, textHeight);
- int width = PADDING * 2 + QR_SIZE + GAP + TEXT_WIDTH;
-
+ int width = PADDING * 2 + QR_SIZE;
+ int height = PADDING * 2 + QR_SIZE + GAP + LINE_HEIGHT;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
try {
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
- // 浜岀淮鐮侊紙鍨傜洿灞呬腑锛�
- int qrY = (height - QR_SIZE) / 2;
- g.drawImage(qrImage, PADDING, qrY, null);
- // 鍙鏂囨湰
- int textX = PADDING + QR_SIZE + GAP;
- int textY = (height - textHeight) / 2;
- drawKeyValues(g, keyValues, labelFont, valueFont, textX, textY);
+ g.drawImage(qrImage, PADDING, PADDING, null);
+ String text = StrUtil.nullToEmpty(displayCode);
+ int fontSize = FONT_SIZE;
+ g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, fontSize));
+ while (g.getFontMetrics().stringWidth(text) > QR_SIZE && fontSize > 1) {
+ g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, --fontSize));
+ }
+ FontMetrics metrics = g.getFontMetrics();
+ int textX = (width - metrics.stringWidth(text)) / 2;
+ int textY = PADDING + QR_SIZE + GAP
+ + (LINE_HEIGHT - metrics.getHeight()) / 2 + metrics.getAscent();
+ g.setColor(Color.BLACK);
+ g.drawString(text, textX, textY);
} finally {
g.dispose();
}
@@ -133,16 +117,29 @@
ZipOutputStream zip = new ZipOutputStream(response.getOutputStream());
try {
for (QrLabelInfo label : labels) {
- BufferedImage image = createQrLabel(label.getCode(), label.getKeyValues());
- zip.putNextEntry(new ZipEntry(label.getFileName() + ".png"));
- ImageIO.write(image, "png", zip);
- zip.closeEntry();
+ writeZipEntry(zip, label);
}
zip.finish();
zip.flush();
} finally {
// 涓� close ZipOutputStream锛岄伩鍏嶅叧闂簳灞� ServletOutputStream锛堜笌 FileTypeUtils 鍚岀悊锛�
}
+ }
+
+ /**
+ * 鐢熸垚鍗曞紶鏍囩骞跺啓鍏� ZIP 鏉$洰
+ * <p>
+ * 渚涘悗鍙板鍑轰换鍔¤嚜寤哄啓鍏ュ惊鐜椂澶嶇敤锛氳皟鐢ㄦ柟闇�瑕佽嚜琛屽湪姣忓紶涔嬮棿妫�鏌ュ彇娑堛�侀噰鏍� CPU 骞跺洖鍐欒繘搴︼紝
+ * 鍥犳杩欓噷鍙礋璐�"涓�寮�"鐨勫鐞嗭紝涓嶆帴绠″惊鐜��
+ *
+ * @param zip ZIP 杈撳嚭娴�
+ * @param label 鏍囩淇℃伅
+ */
+ public static void writeZipEntry(ZipOutputStream zip, QrLabelInfo label) throws IOException {
+ BufferedImage image = createQrLabel(label.getCode(), label.getFileName());
+ zip.putNextEntry(new ZipEntry(label.getFileName() + ".png"));
+ ImageIO.write(image, "png", zip);
+ zip.closeEntry();
}
// ==================== 绉佹湁鏂规硶 ====================
@@ -187,92 +184,6 @@
throw new IllegalStateException("褰撳墠鐜娌℃湁浠讳綍鍙覆鏌撲腑鏂囩殑瀛椾綋锛屼簩缁寸爜鏍囩鐨勪腑鏂囧皢鏄剧ず涓烘柟妗嗭紝"
+ "鎷掔粷鐢熸垚鏍囩銆傝鍦ㄦ湇鍔″櫒瀹夎涓枃瀛椾綋锛堝 fonts-noto-cjk锛夊悗閲嶅惎銆�");
}
- }
-
- private static int measureTextHeight(LinkedHashMap<String, String> keyValues, Font labelFont, Font valueFont) {
- BufferedImage scratch = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
- Graphics2D g = scratch.createGraphics();
- try {
- return measureKeyValuesHeight(g, keyValues, labelFont, valueFont);
- } finally {
- g.dispose();
- }
- }
-
- private static int measureKeyValuesHeight(Graphics2D g, LinkedHashMap<String, String> keyValues,
- Font labelFont, Font valueFont) {
- int total = 0;
- for (Map.Entry<String, String> entry : keyValues.entrySet()) {
- String label = StrUtil.emptyToDefault(entry.getKey(), "") + "锛�";
- String value = StrUtil.emptyToDefault(entry.getValue(), "");
- g.setFont(labelFont);
- int labelWidth = g.getFontMetrics().stringWidth(label);
- g.setFont(valueFont);
- List<String> lines = wrapText(g, value, TEXT_WIDTH - labelWidth);
- total += Math.max(1, lines.size()) * LINE_HEIGHT + FIELD_GAP;
- }
- return total;
- }
-
- private static void drawKeyValues(Graphics2D g, LinkedHashMap<String, String> keyValues,
- Font labelFont, Font valueFont, int x, int y) {
- FontMetrics labelMetrics = g.getFontMetrics(labelFont);
- int baselineOffset = labelMetrics.getAscent(); // drawString 鐨� y 鏄熀绾匡紝鍏堜笅绉讳竴涓� ascent
- int baseline = y + baselineOffset;
- for (Map.Entry<String, String> entry : keyValues.entrySet()) {
- String label = StrUtil.emptyToDefault(entry.getKey(), "") + "锛�";
- String value = StrUtil.emptyToDefault(entry.getValue(), "");
-
- g.setFont(labelFont);
- int labelWidth = g.getFontMetrics().stringWidth(label);
- g.setColor(Color.BLACK);
- g.drawString(label, x, baseline);
-
- g.setFont(valueFont);
- int remainingWidth = TEXT_WIDTH - labelWidth;
- List<String> lines = wrapText(g, value, remainingWidth);
- if (!lines.isEmpty()) {
- g.drawString(lines.get(0), x + labelWidth, baseline);
- baseline += LINE_HEIGHT;
- for (int i = 1; i < lines.size(); i++) {
- g.drawString(lines.get(i), x, baseline);
- baseline += LINE_HEIGHT;
- }
- } else {
- baseline += LINE_HEIGHT;
- }
- baseline += FIELD_GAP;
- }
- }
-
- /**
- * 鏂囨湰鎸夋渶澶у搴︽崲琛岋紙閫愬瓧娴嬮噺锛屽吋瀹逛腑鏂囷級
- */
- private static List<String> wrapText(Graphics2D g, String text, int maxWidth) {
- List<String> lines = new ArrayList<>();
- if (StrUtil.isEmpty(text)) {
- return lines;
- }
- if (maxWidth <= 0) {
- lines.add(text);
- return lines;
- }
- StringBuilder current = new StringBuilder();
- FontMetrics metrics = g.getFontMetrics();
- for (int i = 0; i < text.length(); i++) {
- char ch = text.charAt(i);
- String test = current.toString() + ch;
- if (metrics.stringWidth(test) > maxWidth && current.length() > 0) {
- lines.add(current.toString());
- current = new StringBuilder().append(ch);
- } else {
- current.append(ch);
- }
- }
- if (current.length() > 0) {
- lines.add(current.toString());
- }
- return lines;
}
}
--
Gitblit v1.9.3