From bcb1d905904fd43034f7c95077336e5cb849eff1 Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期日, 18 一月 2026 23:17:22 +0800
Subject: [PATCH] 修改了对应代码
---
archiveManager/src/main/java/com/ruoyi/service/impl/BarcodeService.java | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 153 insertions(+), 11 deletions(-)
diff --git a/archiveManager/src/main/java/com/ruoyi/service/impl/BarcodeService.java b/archiveManager/src/main/java/com/ruoyi/service/impl/BarcodeService.java
index 46a774e..86f8bf1 100644
--- a/archiveManager/src/main/java/com/ruoyi/service/impl/BarcodeService.java
+++ b/archiveManager/src/main/java/com/ruoyi/service/impl/BarcodeService.java
@@ -1,34 +1,176 @@
package com.ruoyi.service.impl;
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.WriterException;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.oned.Code128Writer;
+import com.google.zxing.oned.Code39Writer;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.springframework.stereotype.Service;
+import javax.imageio.ImageIO;
+import java.awt.*;
import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
@Service
public class BarcodeService {
public byte[] generateBarcodeImage(String barcodeText) {
try {
- Code128Bean barcodeGenerator = new Code128Bean();
- final int dpi = 160;
- barcodeGenerator.setModuleWidth(0.58);
- barcodeGenerator.setBarHeight(14.2); // 璁剧疆鏉″舰鐮侀珮搴︿负64
- barcodeGenerator.doQuietZone(false);
+ // 浣跨敤 BitMatrix 鐢熸垚绾潯鐮侊紝涓嶅寘鍚换浣曟枃瀛�
+ BarcodeFormat format = BarcodeFormat.CODE_128; // 鏍规嵁鎮ㄧ殑鏉$爜绫诲瀷
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- BitmapCanvasProvider canvas = new BitmapCanvasProvider(
- outputStream, "image/png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
+ // 鍒涘缓缂栫爜鍣�
+ Code128Writer writer = new Code128Writer();
- barcodeGenerator.generateBarcode(canvas, barcodeText);
- canvas.finish();
+ // 缂栫爜鍙傛暟
+ Map<EncodeHintType, Object> hints = new HashMap<>();
+ hints.put(EncodeHintType.MARGIN, 0); // 鏃犺竟妗�
- return outputStream.toByteArray();
+ // 鐢熸垚 BitMatrix锛堢函鏉$爜锛屾棤鏂囧瓧锛�
+ BitMatrix matrix = writer.encode(barcodeText, format, 0, 63, hints);
+
+ int width = matrix.getWidth();
+ int height = matrix.getHeight();
+
+ // 璁$畻鏂囧瓧鍖哄煙
+ int textAreaHeight = 30 + 10;
+
+ // 鍒涘缓鏈�缁堝浘鍍�
+ BufferedImage finalImage = new BufferedImage(
+ width,
+ height + textAreaHeight,
+ BufferedImage.TYPE_BYTE_BINARY
+ );
+
+ Graphics2D g2d = finalImage.createGraphics();
+
+ // 鐧借壊鑳屾櫙
+ g2d.setColor(Color.WHITE);
+ g2d.fillRect(0, 0, width, height + textAreaHeight);
+
+ // 缁樺埗鏉$爜锛堜粠 BitMatrix锛�
+ g2d.setColor(Color.BLACK);
+ for (int x = 0; x < width; x++) {
+ for (int y = 0; y < height; y++) {
+ if (matrix.get(x, y)) {
+ g2d.fillRect(x, y, 1, 1);
+ }
+ }
+ }
+
+ // 娣诲姞鑷畾涔夋枃瀛�
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+ g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
+ g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
+
+ Font font = new Font("Tahoma", Font.PLAIN, 20);
+ g2d.setFont(font);
+
+ FontMetrics fm = g2d.getFontMetrics();
+ int textWidth = fm.stringWidth(barcodeText);
+ int x = (width - textWidth) / 2;
+ int y = height + 30 - 5; // 璋冩暣鍨傜洿浣嶇疆
+
+ g2d.drawString(barcodeText, x, y);
+ g2d.dispose();
+
+ // 淇濆瓨
+ ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
+ ImageIO.write(finalImage, "png", resultStream);
+
+ return resultStream.toByteArray();
+ // return outputStream.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Error generating barcode", e);
+ } catch (WriterException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+
+
+ public byte[] generateBarcodeImageScarn(String barcodeText) {
+ try {
+ // 浣跨敤 BitMatrix 鐢熸垚绾潯鐮侊紝涓嶅寘鍚换浣曟枃瀛�
+ BarcodeFormat format = BarcodeFormat.CODE_39; // 鏍规嵁鎮ㄧ殑鏉$爜绫诲瀷
+
+ // 鍒涘缓缂栫爜鍣�
+ Code39Writer writer = new Code39Writer();
+
+ // 缂栫爜鍙傛暟
+ Map<EncodeHintType, Object> hints = new HashMap<>();
+ hints.put(EncodeHintType.MARGIN, 0); // 鏃犺竟妗�
+
+ // 鐢熸垚 BitMatrix锛堢函鏉$爜锛屾棤鏂囧瓧锛�
+ BitMatrix matrix = writer.encode(barcodeText, format, 230, 63, hints);
+
+ int width = matrix.getWidth();
+ int height = matrix.getHeight();
+
+ // 璁$畻鏂囧瓧鍖哄煙
+ int textAreaHeight = 30 + 10;
+
+ // 鍒涘缓鏈�缁堝浘鍍�
+ BufferedImage finalImage = new BufferedImage(
+ width,
+ height + textAreaHeight,
+ BufferedImage.TYPE_BYTE_BINARY
+ );
+
+ Graphics2D g2d = finalImage.createGraphics();
+
+ // 鐧借壊鑳屾櫙
+ g2d.setColor(Color.WHITE);
+ g2d.fillRect(0, 0, width, height + textAreaHeight);
+
+ // 缁樺埗鏉$爜锛堜粠 BitMatrix锛�
+ g2d.setColor(Color.BLACK);
+ for (int x = 0; x < width; x++) {
+ for (int y = 0; y < height; y++) {
+ if (matrix.get(x, y)) {
+ g2d.fillRect(x, y, 1, 1);
+ }
+ }
+ }
+
+ // 娣诲姞鑷畾涔夋枃瀛�
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+ g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
+ g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
+
+ Font font = new Font("Tahoma", Font.PLAIN, 20);
+ g2d.setFont(font);
+
+ FontMetrics fm = g2d.getFontMetrics();
+ int textWidth = fm.stringWidth(barcodeText);
+ int x = (width - textWidth) / 2;
+ int y = height + 30 - 5; // 璋冩暣鍨傜洿浣嶇疆
+
+ g2d.drawString(barcodeText, x, y);
+ g2d.dispose();
+
+ // 淇濆瓨
+ ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
+ ImageIO.write(finalImage, "png", resultStream);
+
+ return resultStream.toByteArray();
+ // return outputStream.toByteArray();
+ } catch (IOException e) {
+ throw new RuntimeException("Error generating barcode", e);
+ } catch (WriterException e) {
+ throw new RuntimeException(e);
}
}
}
--
Gitblit v1.9.1