繁体   English   中英

如何使用barcode4j生成随机条码?

[英]How to generate random barcode code using barcode4j?

我试图用这种方式生成随机的ean-8条形码。 我已生成从10000000到99999999的随机数,为ean-8代码生成随机的8位数字。 它给了我一个错误。

Exception in thread "main" java.lang.IllegalArgumentException: Checksum is bad (1).    Expected: 7
at org.krysalis.barcode4j.impl.upcean.EAN8LogicImpl.handleChecksum(EAN8LogicImpl.java:85)
at org.krysalis.barcode4j.impl.upcean.EAN8LogicImpl.generateBarcodeLogic(EAN8LogicImpl.java:102)
at org.krysalis.barcode4j.impl.upcean.UPCEANBean.generateBarcode(UPCEANBean.java:93)
at org.krysalis.barcode4j.impl.ConfigurableBarcodeGenerator.generateBarcode(ConfigurableBarcodeGenerator.java:174)
at barcode2.BARCODE2.main(BARCODE2.java:42)
Java Result: 1

这是代码。

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.krysalis.barcode4j.BarcodeException;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.BarcodeUtil;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

public class BARCODE2 {
public static void main(String[] args) throws ConfigurationException, BarcodeException, IOException {

BarcodeUtil util = BarcodeUtil.getInstance();
BarcodeGenerator gen = util.createBarcodeGenerator(buildCfg("ean-8"));

OutputStream fout = new FileOutputStream("ean-8.jpg");
int resolution = 200;
BitmapCanvasProvider canvas = new BitmapCanvasProvider(
    fout, "image/jpeg", resolution, BufferedImage.TYPE_BYTE_BINARY, false, 0);

int min = 10000000;
int max = 99999999;

Random r = new Random();
int randomnumber = r.nextInt(max - min + 1) + min;

String barcodecods = String.valueOf(randomnumber);

gen.generateBarcode(canvas, barcodecods);
canvas.finish();
}

private static Configuration buildCfg(String type) {
DefaultConfiguration cfg = new DefaultConfiguration("barcode");

//Bar code type
DefaultConfiguration child = new DefaultConfiguration(type);
  cfg.addChild(child);

  //Human readable text position
  DefaultConfiguration attr = new DefaultConfiguration("human-readable");
  DefaultConfiguration subAttr = new DefaultConfiguration("placement");
    subAttr.setValue("bottom");
    attr.addChild(subAttr);

    child.addChild(attr);
return cfg;
}
}

但是当将用于随机代码的字符串值替换为特定的8位数时,程序正常运行。 我该怎么办? 我哪里做错了? 有没有其他方法可以生成ean-8条码生成的随机8位数字?

条形码不仅仅是简单的数字。 整个数字包含一个校验位,它由算术过程从其他数字生成。 因此,并非所有数字都是有效的条形码。

不同的条形码使用不同的校验位算法。 您需要找出您正在使用的库所期望的算法,然后生成满足此要求的条形码。

因此,例如,如果条形码是8位数字,您将生成随机的7位数字并附加正确计算的第8位数字以生成有效的条形码。

注意:校验位是奇偶校验位的十进制等效值。 它允许软件在大多数情况下检测代码是否被错误地读取。 它并不完美,因为有些错误会产生相同的校验位,但它会大大降低误读的可能性。

生成一个7位数的随机数,并使用以下方法添加校验位:

public static int checkdigit(String idWithoutCheckdigit) {

    // allowable characters within identifier
    String validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVYWXZ_";

    // remove leading or trailing whitespace, convert to uppercase
    idWithoutCheckdigit = idWithoutCheckdigit.trim().toUpperCase();

    // this will be a running total
    int sum = 0;

    // loop through digits from right to left
    for (int i = 0; i < idWithoutCheckdigit.length(); i++) {

        // set ch to "current" character to be processed
        char ch = idWithoutCheckdigit.charAt(idWithoutCheckdigit.length() - i - 1);

        // throw exception for invalid characters
        if (validChars.indexOf(ch) == -1)
            throw new RuntimeException("\"" + ch + "\" is an invalid character");

        // our "digit" is calculated using ASCII value - 48
        int digit = ch - 48;

        // weight will be the current digit's contribution to
        // the running total
        int weight;
        if (i % 2 == 0) {

            // for alternating digits starting with the rightmost, we
            // use our formula this is the same as multiplying x 2 and
            // adding digits together for values 0 to 9. Using the
            // following formula allows us to gracefully calculate a
            // weight for non-numeric "digits" as well (from their
            // ASCII value - 48).
            weight = (2 * digit) - (digit / 5) * 9;

        } else {

            // even-positioned digits just contribute their ascii
            // value minus 48
            weight = digit;

        }

        // keep a running total of weights
        sum += weight;

    }
    // avoid sum less than 10 (if characters below "0" allowed,
    // this could happen)
    sum = Math.abs(sum) + 10;
    // check digit is amount needed to reach next number
    // divisible by ten
    return (10 - (sum % 10)) % 10;

}

暂无
暂无

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

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