简体   繁体   English

Barcode4j - 在 EAN13 条形码中生成校验位

[英]Barcode4j - Generate check digit in an EAN13 barcode

When yo generate a barcode with Barcode4j as an image, you could obtain the human readable text, too, for instance:当您使用 Barcode4j 作为图像生成条形码时,您也可以获得人类可读的文本,例如:

EAN13 barcode example EAN13 条码示例

In this picture we can see that the human readable text is: 1000000012026在这张图片中我们可以看到人类可读的文本是: 1000000012026

In this example the barcode has been generated with the code 100000001202 and the number 6 is the check digit added by Barcode4j generator.在此示例中,已使用代码100000001202生成条形码,数字6是 Barcode4j 生成器添加的校验位。

So, my question is: Is possible obtain the check digit of an EAN13 generated barcode with Barcode4j?所以,我的问题是:是否可以使用 Barcode4j 获得 EAN13 生成的条形码的校验位? Because I know how to render this as a image, but I don't know how to obtain the human readable text, as a plain text.因为我知道如何将其渲染为图像,但我不知道如何获取人类可读的文本,即纯文本。

Regards,问候,

Miguel.米格尔。

Thanks to Barcode4j plugin, you can calculate the checksum with the barcode format you need.感谢 Barcode4j 插件,您可以使用所需的条形码格式计算校验和。 In Java 7, you can calculate the checkSum as this way:在 Java 7 中,您可以这样计算校验和:

private String calculateCodeWithcheckSum(String codigo){
   EAN13Bean generator = new EAN13Bean();
   UPCEANLogicImpl impl = generator.createLogicImpl();
   codigo += impl.calcChecksum(codigo);
   return codigo;
}

First, you need the EAN13 barcode format, so you can get the class that the plugin provides you, and call its only method: createLogicImpl() .首先,您需要EAN13条形码格式,因此您可以获得插件提供的 class,并调用其唯一方法: createLogicImpl()

This method is used to give you a class of type UPCEANLogicImpl .此方法用于为您提供 UPCEANLogicImpl 类型的UPCEANLogicImpl This is the class you need, because you can find in it the method to calculate the checkSum.这是您需要的 class,因为您可以在其中找到计算 checkSum 的方法。 So, you only have to call the method calcChecksum giving your code ( 100000001202 ), and will give you the checkSum value ( 6 ).因此,您只需要调用方法 calcChecksum 并给出您的代码( 100000001202 ),然后就会给出 checkSum 值( 6 )。

You can check it in the next web: http://www.gs1.org/check-digit-calculator您可以在下一个 web 中查看它: http://www.gs1.org/check-digit-calculator

Adding your code and the checkSum value will give you the value you need ( 1000000012026 )添加您的代码和 checkSum 值将为您提供所需的值( 1000000012026

In case you what to compute the check digit without importing a java library here's the code:如果您在不导入 java 库的情况下计算校验位,代码如下:

public static String ean13CheckDigit(String barcode) {
    int s = 0;
    for (int i = 0; i < 12; i++) {
        int c = Character.getNumericValue(barcode.charAt(i));
        s += c * ( i%2 == 0? 1: 3);
    }
    s = (10 - s % 10) % 10;
    barcode += s;
    return barcode;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM