简体   繁体   English

爪哇。 条码 Ean-13 到字符串

[英]Java. BarCode Ean-13 to String

I have a project, that draws a price tag, which has a barcode in it.我有一个项目,它绘制了一个价格标签,其中有一个条形码。 To draw a barcode i use a JLabel with EAN-13 font set in. The input data, from which the price tag is generated, consists of two barcode attributes: the barcode number为了绘制条码,我使用了一个带有 EAN-13 字体的 JLabel。生成价格标签的输入数据由两个条码属性组成:条码编号

080432402184 for example例如080432402184

and the encoded version, which is passed to the previosly mentioned JLabel和编码版本,它传递给前面提到的 JLabel

!h04324|PRQXTp for that barcode number !h04324|该条形码编号的PRQXTp

The problem is that i dont have access to the code, which generates the encoded version, and the algorithm that generates it has bugs.问题是我无法访问生成编码版本的代码,并且生成它的算法有错误。 Because of that i want to write that thing from scrap, but having trouble finding the encoding algorithm.因此,我想从废料中写出那个东西,但是找不到编码算法。

Can someone point me to where i can find instructions on encoding it?有人可以指出我在哪里可以找到有关编码的说明吗? Thanks.谢谢。

======================================================================= ================================================== ======================

The Barcode4J problem. Barcode4J 问题。 Trying to create a Graphics2D object and draw a barcode on it(cant really use a file out, because the barcode is only a part of the price tag).尝试创建一个 Graphics2D 对象并在其上绘制条形码(不能真正使用文件,因为条形码只是价格标签的一部分)。

Trying to do this using Java2DCanvasProvider:尝试使用 Java2DCanvasProvider 执行此操作:

EAN13Bean bean = new EAN13Bean();

    final int dpi = 150;

    //Configure the barcode generator
    bean.setModuleWidth(UnitConv.in2mm(13.3f / dpi)); //makes the narrow bar 
                                                     //width exactly one pixel
    bean.doQuietZone(true);

    bean.setHeight(chart.getBarcodeMainHeight()-10);
    bean.setFontSize(10f);

    BufferedImage bi = new BufferedImage(chart.getBarcodeMainWidth(), chart.getBarcodeMainHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bi.createGraphics();

    //graphics.fillRect(0, 0, chart.getBarcodeMainWidth(), chart.getBarcodeMainHeight());
    Java2DCanvasProvider canv = new Java2DCanvasProvider(graphics, 0);

    bean.generateBarcode(canv, priceTag.getBarCodeNumber());

    barCodeLabel.setIcon(new ImageIcon(bi));

but i recieve an inverted image block(i'm really new to Graphics2D).但我收到了一个倒置的图像块(我真的是 Graphics2D 的新手)。

Barcode4J has your back on this. Barcode4J 支持您。 It can also generate the images, so you can let go of the JLabel and the special font.它还可以生成图像,因此您可以放弃 JLabel 和特殊字体。

For those who may need this, finally found the algorithm(in C, but rewrote it in Java).对于那些可能需要这个的人,终于找到了算法(用 C 语言,但用 Java 重写)。

public class BarCodeService {
    private static final int[][] leftPartMaps = {{48,48,48,48,48},{48,64,48,64,64},{48,64,64,48,64},{48,64,64,64,48},{64,48,48,64,64},{64,64,48,48,64},{64,64,64,48,48},{64,48,64,48,64},{64,48,64,64,48},{64,64,48,64,48}};
    private static final int[] rightPartMap = {80,80,80,80,80,112};
    private static final int firstDigitArg = 33, secondDigitArg = 96;
    private static final char delimiter = 124;


    public String parseInput(String code){
        char[] data = new char[13];
        char[] givenData = code.toCharArray();

        int length = (givenData.length < 12) ? givenData.length : 12;
        System.arraycopy(givenData, 0, data, 0, length);

        int checkSumDigit = generateCheckSum(data);
        data[12] = String.valueOf(checkSumDigit).charAt(0);

        return String.valueOf(data);
    }

    public String generateCode(String code){
        char[] data = code.toCharArray();
        if(data.length<13){
            System.err.println("Bad data input");
            return null;
        }

        String result = null;
        try{
            result = generateEanString(data);
        }catch(NumberFormatException e){
            System.err.println("Input data had unconvertable characters: "+e.fillInStackTrace());
            result = "";
        }
        return result;
    }

    protected int generateCheckSum(char[] data){
        int result = 0;
        for(int i = 0; i<12;i++){
            int num = Character.getNumericValue(data[i]); 
            num = (i%2 == 0) ? num : num*3;
            result += num;
        }

        result = (result % 10 == 0) ? 0 : ((result/10)+1)*10 - result;
        return result;
    }
    protected String generateEanString(char[] data) throws NumberFormatException{
        char[] resultData = new char[14];

        resultData[0] = (char) (Character.getNumericValue(data[0]) + firstDigitArg);
        resultData[1] = (char) (Character.getNumericValue(data[1]) + secondDigitArg);


    fillLeftPart(data,resultData);
    resultData[7] = delimiter;
    fillRightPart(data,resultData);

    return String.valueOf(resultData);
}

protected void fillLeftPart(char[] inputData, char[] resultData){
    int[] chars = new int[]{
            Character.getNumericValue(inputData[2]),
            Character.getNumericValue(inputData[3]),
            Character.getNumericValue(inputData[4]),
            Character.getNumericValue(inputData[5]),
            Character.getNumericValue(inputData[6])
    };

    int pointer = Character.getNumericValue(inputData[0]);
    for(int i = 0; i<leftPartMaps[pointer].length; i++){
        int n = i+2;
        resultData[n] = (char)(chars[i] + leftPartMaps[pointer][i]);
    }

}

protected void fillRightPart(char[] inputData, char[] resultData){
    int[] chars = new int[]{
        Character.getNumericValue(inputData[7]),
        Character.getNumericValue(inputData[8]),
        Character.getNumericValue(inputData[9]),
        Character.getNumericValue(inputData[10]),
        Character.getNumericValue(inputData[11]),
        Character.getNumericValue(inputData[12])
    };

    for(int i = 0; i< rightPartMap.length; i++){
        int n = i + 8;
        resultData[n] = (char)(chars[i] + rightPartMap[i]);
    }
}

public static void main(String[] args) {
    /*System.out.println((char)(Character.getNumericValue('4')+33));
    System.out.println((char)((int)('a')+2));
    System.out.println((int)'%');
    System.out.println("'"+(char)'0'+"'");*/
    //if(true)return;
    // %hB00FB|PUPWVp 4820062050760
    //"%hB00FB|PUQUUr";"4820062051552";
    String testCode = "2205276000000";
    BarCodeService serv = new BarCodeService();
    String parsedString = serv.parseInput(testCode);
    System.out.println("Input: "+testCode+ ", parsed string: "+parsedString);
    String barCodeString = serv.generateCode(parsedString);
    System.out.println("Result: "+barCodeString);
}

} }

My input was from a String, and had bad checksum digits, so the char[] structure was used on purpose.我的输入来自字符串,并且校验和数字错误,因此特意使用了 char[] 结构。

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

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