fei
22 小时以前 9b1b0782ecb77d6ee958774da94606478482b063
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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 {
    /**
     * 添加文字到条码(优化版本)
     */
    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,ZXing可能仍会保留最小边距
            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_39; // 根据您的条码类型
 
            // 创建编码器
            Code39Writer writer = new Code39Writer();
 
            // 编码参数
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.MARGIN, 0); // 无边框
 
            // 生成 BitMatrix(纯条码,无文字)
            BitMatrix matrix = writer.encode(barcodeText, format, 1128, 199, 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, 18);
            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, 18);
            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);
        }
    }
}