From 9b1b0782ecb77d6ee958774da94606478482b063 Mon Sep 17 00:00:00 2001
From: fei <791364011@qq.com>
Date: 星期二, 20 一月 2026 22:41:32 +0800
Subject: [PATCH] 增加了签名和注解接口

---
 archiveManager/src/main/java/com/ruoyi/service/impl/BarcodeService.java |  172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 166 insertions(+), 6 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 86f8bf1..9ee3c57 100644
--- a/archiveManager/src/main/java/com/ruoyi/service/impl/BarcodeService.java
+++ b/archiveManager/src/main/java/com/ruoyi/service/impl/BarcodeService.java
@@ -23,20 +23,180 @@
 @Service
 
 public class BarcodeService {
-    public byte[] generateBarcodeImage(String barcodeText) {
+    /**
+     * 娣诲姞鏂囧瓧鍒版潯鐮侊紙浼樺寲鐗堟湰锛�
+     */
+    private void addTextToBarcode(Graphics2D g2d, String text, int width, int barcodeHeight) {
+        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);
+
+        // 浣跨敤鏇存竻鏅扮殑瀛椾綋
+        Font font = new Font("Tahoma", Font.PLAIN, 45);
+        g2d.setFont(font);
+        g2d.setColor(Color.BLACK);
+
+        // 璁$畻鏂囧瓧浣嶇疆
+        FontMetrics fm = g2d.getFontMetrics();
+        int textWidth = fm.stringWidth(text);
+        int textHeight = fm.getHeight();
+
+        // 灞呬腑瀵归綈
+        int x = (width - textWidth) / 2;
+        int y = barcodeHeight + textHeight - fm.getDescent(); // 绮剧‘鍨傜洿灞呬腑
+
+        // 缁樺埗鏂囧瓧
+        g2d.drawString(text, x, y);
+    }
+    public byte[] generateBarcodeImage(String barcodeText)   {
+        try {
+            BarcodeFormat format = BarcodeFormat.CODE_39;
+            Code39Writer writer = new Code39Writer();
+
+            // 缂栫爜鍙傛暟 - 璁剧疆杈硅窛涓�0
+            Map<EncodeHintType, Object> hints = new HashMap<>();
+            hints.put(EncodeHintType.MARGIN, 0); // 鍏抽敭锛氳缃竟璺濅负0
+
+            // 鐢熸垚 BitMatrix锛堢函鏉$爜锛屾棤鏂囧瓧锛�
+            // 娉ㄦ剰锛氬嵆浣胯缃簡MARGIN=0锛孼Xing鍙兘浠嶄細淇濈暀鏈�灏忚竟璺�
+            BitMatrix matrix = writer.encode(barcodeText, format, 1300, 141, hints);
+
+            // 鏂规1.1锛氱洿鎺ヨ鍓櫧杈�
+            return generateBarcodeWithCroppedMargin(matrix, barcodeText);
+        }
+        catch (IOException e) {
+            throw new RuntimeException("Error generating barcode", e);
+        } catch (WriterException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private byte[] generateBarcodeWithCroppedMargin(BitMatrix matrix, String barcodeText) throws IOException {
+        int width = matrix.getWidth();
+        int height = matrix.getHeight();
+
+        // 鎵惧埌鏉$爜鐨勫疄闄呰竟鐣�
+        int left = findLeftBoundary(matrix);
+        int right = findRightBoundary(matrix);
+        int top = findTopBoundary(matrix);
+        int bottom = findBottomBoundary(matrix);
+
+        // 璁$畻鏉$爜鐨勫疄闄呭搴﹀拰楂樺害
+        int barcodeWidth = right - left + 1;
+        int barcodeHeight = bottom - top + 1;
+
+        // 璁$畻鏂囧瓧鍖哄煙楂樺害
+        int textAreaHeight = 30 + 27;
+
+        // 鍒涘缓鏈�缁堝浘鍍忥紙绮剧‘灏哄锛�
+        BufferedImage finalImage = new BufferedImage(
+                barcodeWidth,
+                barcodeHeight + textAreaHeight,
+                BufferedImage.TYPE_BYTE_BINARY
+        );
+
+        Graphics2D g2d = finalImage.createGraphics();
+
+        // 鐧借壊鑳屾櫙
+        g2d.setColor(Color.WHITE);
+        g2d.fillRect(0, 0, barcodeWidth, barcodeHeight + textAreaHeight);
+
+        // 缁樺埗鏉$爜锛堝彧缁樺埗鏈夋晥閮ㄥ垎锛�
+        g2d.setColor(Color.BLACK);
+        for (int x = left; x <= right; x++) {
+            for (int y = top; y <= bottom; y++) {
+                if (matrix.get(x, y)) {
+                    g2d.fillRect(x - left, y - top, 1, 1);
+                }
+            }
+        }
+
+        // 娣诲姞鑷畾涔夋枃瀛�
+        addTextToBarcode(g2d, barcodeText, barcodeWidth, barcodeHeight);
+
+        g2d.dispose();
+
+        // 淇濆瓨
+        ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
+        ImageIO.write(finalImage, "png", resultStream);
+
+        return resultStream.toByteArray();
+    }
+
+    // 杈呭姪鏂规硶锛氭壘鍒版潯鐮佺殑杈圭晫
+    private int findLeftBoundary(BitMatrix matrix) {
+        int width = matrix.getWidth();
+        int height = matrix.getHeight();
+
+        for (int x = 0; x < width; x++) {
+            for (int y = 0; y < height; y++) {
+                if (matrix.get(x, y)) {
+                    return x;
+                }
+            }
+        }
+        return 0;
+    }
+
+    private int findRightBoundary(BitMatrix matrix) {
+        int width = matrix.getWidth();
+        int height = matrix.getHeight();
+
+        for (int x = width - 1; x >= 0; x--) {
+            for (int y = 0; y < height; y++) {
+                if (matrix.get(x, y)) {
+                    return x;
+                }
+            }
+        }
+        return width - 1;
+    }
+
+    private int findTopBoundary(BitMatrix matrix) {
+        int width = matrix.getWidth();
+        int height = matrix.getHeight();
+
+        for (int y = 0; y < height; y++) {
+            for (int x = 0; x < width; x++) {
+                if (matrix.get(x, y)) {
+                    return y;
+                }
+            }
+        }
+        return 0;
+    }
+
+    private int findBottomBoundary(BitMatrix matrix) {
+        int width = matrix.getWidth();
+        int height = matrix.getHeight();
+
+        for (int y = height - 1; y >= 0; y--) {
+            for (int x = 0; x < width; x++) {
+                if (matrix.get(x, y)) {
+                    return y;
+                }
+            }
+        }
+        return height - 1;
+    }
+
+
+
+
+    public byte[] generateBarcodeImage1(String barcodeText) {
         try {
            // 浣跨敤 BitMatrix 鐢熸垚绾潯鐮侊紝涓嶅寘鍚换浣曟枃瀛�
-            BarcodeFormat format = BarcodeFormat.CODE_128; // 鏍规嵁鎮ㄧ殑鏉$爜绫诲瀷
+            BarcodeFormat format = BarcodeFormat.CODE_39; // 鏍规嵁鎮ㄧ殑鏉$爜绫诲瀷
 
             // 鍒涘缓缂栫爜鍣�
-            Code128Writer writer = new Code128Writer();
+            Code39Writer writer = new Code39Writer();
 
             // 缂栫爜鍙傛暟
             Map<EncodeHintType, Object> hints = new HashMap<>();
             hints.put(EncodeHintType.MARGIN, 0); // 鏃犺竟妗�
 
             // 鐢熸垚 BitMatrix锛堢函鏉$爜锛屾棤鏂囧瓧锛�
-            BitMatrix matrix = writer.encode(barcodeText, format, 0, 63, hints);
+            BitMatrix matrix = writer.encode(barcodeText, format, 1128, 199, hints);
 
             int width = matrix.getWidth();
             int height = matrix.getHeight();
@@ -73,7 +233,7 @@
             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);
+            Font font = new Font("Tahoma", Font.PLAIN, 18);
             g2d.setFont(font);
 
             FontMetrics fm = g2d.getFontMetrics();
@@ -150,7 +310,7 @@
             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);
+            Font font = new Font("Tahoma", Font.PLAIN, 18);
             g2d.setFont(font);
 
             FontMetrics fm = g2d.getFontMetrics();

--
Gitblit v1.9.1