1

我尝试使用这种方式生成随机 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 位数字?

4

2 回答 2

3

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

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

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

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

于 2014-08-25T17:37:49.437 回答
0

Generate a random number with 7 digits, and add the check-digit with the following method:

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;

}
于 2014-08-25T18:10:49.200 回答