簡體   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