首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用barcode4j生成随机条形码?

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

Stack Overflow用户
提问于 2014-08-26 01:28:30
回答 2查看 4.1K关注 0票数 1

我试着用这种方法生成随机的ean-8条形码。我已经生成了从10000000到99999999的随机数,以生成ean-8代码的随机8位数。这给了我一个错误。

代码语言:javascript
复制
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

这是代码。

代码语言:javascript
复制
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位数字时,程序可以正常运行。我该怎么办?我哪里错了?有没有其他方法可以生成随机的8位数来生成ean-8条形码?

EN

回答 2

Stack Overflow用户

发布于 2014-08-26 01:37:49

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

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

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

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

票数 3
EN

Stack Overflow用户

发布于 2014-08-26 02:10:49

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

代码语言:javascript
复制
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;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25491231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档