Java生成]C1开头EAN128(GS1-128)条形码,]d2开头GS1 DataMatrix 二维码

 

前一段时间遇到一个生成条形码,二维码的需求。因为别人给了个例子,上面的条形码以]C1开头,二维码以]d2开头,刚开始原本是以为开头这些是包含在生成对应文件的字符串中,后面对方反馈说不是在字符串中,这是一种类似于编码格式。自己下来琢磨了很久,国内对于这块的资料比较少。在Google上翻阅了大量的文章,后面终于弄了出来。在此做以记录,希望有同样需求的朋友可以快速找到解决办法。

1.EAN128(GS1-128)条形码

GS1128是Code128的一个子类,在Code128的基础上增加了AI(Application Identifier)应用标识符,具体详解请查看,我这里就不多做叙述了,直接上代码。

https://www.crifan.com/files/doc/docbook/symbology_gs1128/release/html/symbology_gs1128.html

引入barcode4j的jar包.

       <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j</artifactId>
            <version>2.1</version>
        </dependency>

关于barcode4j参考文档如下http://barcode4j.sourceforge.net/trunk/javadocs/index.html,里面有各种类型条码的生成方法,若生成条码,可以先去官网查看是否有对应的条码类型。

BarCode4j的一部分条码类型。

写对应条码的生成方法。使用EAN128Bean生成对应的条码,如果想开头有"]C1"应用标识符,记得设置表格格式为CODESET_C;

   public static byte[] generateBarCode(String msg) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EAN128Bean bean = new EAN128Bean();
        // 精细度
        final int dpi = 70;
        // module宽度
        final double moduleWidth = UnitConv.in2mm(1.0f / dpi);
        // 配置对象
        bean.setModuleWidth(moduleWidth);
        bean.setHeight(29);
        bean.doQuietZone(false);
        //是否显示条形码下的文字
        //bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
        bean.setCodeset(Code128Constants.CODESET_C);
        String format = "image/bmp";
        try {
            // 输出到流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, format, dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            // 生成条形码
            bean.generateBarcode(canvas, msg);
            // 结束绘制
            canvas.finish();
            return baos.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

测试方法调用生成对应条码。

    @Test
    void contextLoads() throws Exception {
        FileOutputStream fileOutputStream=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\barCode.jpg");
        fileOutputStream.write(BarcodeUtil.generateBarCode("12345678987654321"));
    }

生成结果如下,我手动调整了宽度,看起来更清晰一点。 

 

扫描条形码结果如下,在条码的前面自动加上了“]C1”。软件是NEOREADER,在google商城可以下载。 

2.生成DataMatrix二维码

上面的barcode4j也可以生成普通的二维码,但是我搞了很久,生成的我二维码没有“]d2”开头,不知道是我使用方式不对还是有什么原因。我也尝试用了Google的ZXing,但是也并没有成功。后面翻阅了一篇文章,里面有这样一段话,大意是说Bacode4j和ZXing都不支持GS1编码格式,然后推荐使用OkapiBarcode。然后自己试了下,解决了这个问题。

If we start with Barcode4J then we quickly notice that its last release was version 2.0.1 in December of 2010. So let’s just go ahead and remove that from our list without further ado.

This leaves us with two potential candidates. ZXing certainly has the bigger community, but as it turns out, does not actually support GS1 encoding due to irregularities with the <GS> character mentioned above (or at least it didn’t at the time of writing).

文章参考地址:https://blog.jayway.com/2016/06/30/gs1-datamatrix-codes-java/

 导入jar包

       <dependency>
            <groupId>uk.org.okapibarcode</groupId>
            <artifactId>okapibarcode</artifactId>
            <version>0.1.2</version>
        </dependency>

编写生成方法

  public static byte[] createDataMarix(String input) throws IOException {
        int magnification = 10;
        int barcodeSize = 0 * magnification;
        DataMatrix dataMatrix = new DataMatrix();
        dataMatrix.setDataType(Symbol.DataType.GS1);
        dataMatrix.setPreferredSize(0);
        dataMatrix.forceSquare(true);
        dataMatrix.setContent(input);
        BufferedImage image = new BufferedImage((dataMatrix.getWidth() * magnification) + (2 * barcodeSize),
                (dataMatrix.getHeight() * magnification) + (2 * barcodeSize),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, (dataMatrix.getWidth() * magnification) + (2 * barcodeSize), (dataMatrix.getHeight() * magnification) + (2 * barcodeSize));
        Java2DRenderer renderer = new Java2DRenderer(g2d, 10, 0, Color.WHITE, Color.BLACK);
        renderer.render(dataMatrix);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        return baos.toByteArray();
    }

编写调用方法,这里注意将二维码中的FNC1都要用"[]"括起来,不然会生成失败。生成结果如下:

  @Test
    void contextLoads() throws Exception {
        FileOutputStream fileOutputStream=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\dataMatrix.jpg");
        fileOutputStream.write(DataMatrixCode.createDataMarix("[01]99312650999998[91]LAM297548101000965009[420]4552[8008]200918110325"));
    }

 

扫描结果如下,二维码前面会自动有"]d2",在每个AI前面会加上<GS>分隔符。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值