严智鑫
2025-11-13 945bc394f40d8af1072a53da9a94f24207124e6d
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
package com.northglass.util;
 
import java.awt.image.BufferedImage;
 
import javax.imageio.ImageIO;
 
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.EAN13Encoder;
import org.jbarcode.paint.BaseLineTextPainter;
import org.jbarcode.paint.EAN13TextPainter;
import org.jbarcode.paint.WidthCodedPainter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import sun.misc.BASE64Encoder;
 
@SuppressWarnings("restriction")
public class JbarcodeUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(JbarcodeUtil.class);
 
    /**
     * 商品条形码
     * 
     * @param strBarCode
     *            商品条形码:13位
     * @param dimension
     *            商品条形码:尺寸 ,必须设置成2,否则打出的一维码扫描不了
     * @param barheight
     *            商品条形码:高度
     * @return 图片(Base64编码)
     */
    public BufferedImage getimage(String str, String dimension, String barheight) {
        LOGGER.debug("JbarcodeUtil:" + str);
 
        try {
            int length = str.length();
            JBarcode localJBarcode = new JBarcode(EAN13Encoder.getInstance(), WidthCodedPainter.getInstance(),
                    EAN13TextPainter.getInstance());
 
            String barCode = str.substring(0, length - 1);
            LOGGER.debug("str:" + barCode);
            // 尺寸,面积,大小
            localJBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
            // 高度 10.0 = 1cm 默认1.5cm
            localJBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
            // 宽度
            localJBarcode.setWideRatio(Double.valueOf(25).doubleValue());
            // localJBarcode.setShowText(true);// 显示图片下字符串内容
            // localJBarcode.setShowCheckDigit(true);// 显示字符串内容中是否显示检查码内容
            localJBarcode.setCheckDigit(false);// 不生成检查码
            // 校验13位
            BufferedImage localBufferedImage = localJBarcode.createBarcode(barCode);
            return localBufferedImage;
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return null;
    }
 
    /**
     *128条形码 
     * @param strBarCode 
     *条形码:0-100位 
     * @param dimension 
     * 商品条形码:尺寸
     * @param barheight 
     * 商品条形码:高度 
     * @return 图片(Base64编码)
     */
    public BufferedImage generateBarCode128(String strBarCode, String dimension, String barheight) {
        try {
            ByteArrayOutputStream outputStream = null;
            BufferedImage bi = null;
            int len = strBarCode.length();
            JBarcode productBarcode = new JBarcode(Code128Encoder.getInstance(), WidthCodedPainter.getInstance(),
                    EAN13TextPainter.getInstance());
            // 尺寸,面积,大小 密集程度
            productBarcode.setXDimension(Double.valueOf(dimension).doubleValue());
            // 高度 10.0 = 1cm 默认1.5cm
            productBarcode.setBarHeight(Double.valueOf(barheight).doubleValue());
            // 宽度
            productBarcode.setWideRatio(Double.valueOf(30).doubleValue());
            // 是否显示字体
            productBarcode.setShowText(true);
            // 显示字体样式
            productBarcode.setTextPainter(BaseLineTextPainter.getInstance());
            // 生成二维码
            bi = productBarcode.createBarcode(strBarCode);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", outputStream);
            BASE64Encoder encoder = new BASE64Encoder();
//            //
//            System.err.println(encoder.encode(outputStream.toByteArray()));
            BufferedImage localBufferedImage = productBarcode.createBarcode(strBarCode);
            return localBufferedImage;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}