• Java条形码生成技术-Barcode4j


    以下内容转自:https://www.cnblogs.com/littleatp/p/4815921.html

     

    背景

    目前二维码的应用场景已经遍布各类互联网平台,通常是将产品/商品的唯一编号存储于二维码中以做扫码识别。
    而用于生产环境的条形码技术仍然存在,如硬件设备制造、供应、物流运输等等。
    在常见的产品信息管理、物料订单系统中,存在多个生成及打印条形码(一维码)的需求场景。
     

    解决方案

    Java生成条形码的方案 -- barcode4j、zxing

    barcode4j

    barcode4j开源Java条形码生成库。支持多种编码格式,比如:code-39,code-128等
     

    zxing

    是由google开源的1D/2D编解码类库。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android
     
    本次采用了barcode4j作为解决方案
     

    环境准备

    下载barcode4j-light
    barcode4j 依赖的lib包略显臃肿,其中包括了avalon-framework/servelet-api,
    因此本次选择的是轻量级的版本barcode4j-light
     
    maven地址
     
    1
    2
    3
    4
    5
    <dependency>
    <groupId>net.sf.barcode4j</groupId>
    <artifactId>barcode4j-light</artifactId>
    <version>2.0</version>
    </dependency>

      

    //另外,也可以下载barcode4j-bin包
     
     

    代码实例

    BarcodeUtil工具类
     
    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
    package utils;
     
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
     
    import org.apache.commons.lang.StringUtils;
    import org.krysalis.barcode4j.impl.code39.Code39Bean;
    import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
    import org.krysalis.barcode4j.tools.UnitConv;
     
    /**
     * 条形码工具类
     *
     * @author tangzz
     * @createDate 2015年9月17日
     *
     */
    public class BarcodeUtil {
     
        /**
         * 生成文件
         *
         * @param msg
         * @param path
         * @return
         */
        public static File generateFile(String msg, String path) {
            File file = new File(path);
            try {
                generate(msg, new FileOutputStream(file));
            catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
            return file;
        }
     
        /**
         * 生成字节
         *
         * @param msg
         * @return
         */
        public static byte[] generate(String msg) {
            ByteArrayOutputStream ous = new ByteArrayOutputStream();
            generate(msg, ous);
            return ous.toByteArray();
        }
     
        /**
         * 生成到流
         *
         * @param msg
         * @param ous
         */
        public static void generate(String msg, OutputStream ous) {
            if (StringUtils.isEmpty(msg) || ous == null) {
                return;
            }
     
            Code39Bean bean = new Code39Bean();
     
            // 精细度
            final int dpi = 150;
            // module宽度
            final double moduleWidth = UnitConv.in2mm(1.0f / dpi);
     
            // 配置对象
            bean.setModuleWidth(moduleWidth);
            bean.setWideFactor(3);
            bean.doQuietZone(false);
     
            String format = "image/png";
            try {
     
                // 输出到流
                BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
                        BufferedImage.TYPE_BYTE_BINARY, false0);
     
                // 生成条形码
                bean.generateBarcode(canvas, msg);
     
                // 结束绘制
                canvas.finish();
            catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
     
        public static void main(String[] args) {
            String msg = "123456789";
            String path = "barcode.png";
            generateFile(msg, path);
        }
    }
     

    FAQ

    二维码相对于条形码的优势

       数据容量更大;超越了字母数字的限制;具有抗损毁能力

    关于条形码的各种编码格式
     
     
     
    如何生成或识别二维码
    推荐使用ZXing项目  https://github.com/zxing/zxing
     
  • 相关阅读:
    px, pt, rpx, rem,
    tomcat报错LifecycleException的解决方案
    servlet系列
    tomcat startup.bat双击闪退解决方案。
    代码页
    editplus工具配置
    正则表达式常见字符集
    单片机TM4C123学习(六):看门狗
    stata学习笔记(七):回归分析和稳健性检验
    单片机TM4C123学习(五):UART的使用
  • 原文地址:https://www.cnblogs.com/yadongliang/p/10294573.html
Copyright © 2020-2023  润新知