条形码EAN-13到二进制

时间:2012-04-26 13:34:58

标签: java barcode

我需要从EAN-13数字创建字节数组。 我如何强调,这里是描述:http://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29#Binary_encoding_of_data_digits_into_EAN-13_barcode

也许有人已经创建了这个算法?请分享。

1 个答案:

答案 0 :(得分:0)

以下代码可以编码EAN-13条形码。 funnyEncode方法是您似乎需要的特殊版本。

public class EAN13Encoder {

    public static void main(String[] args) {
        byte[] result = encode(4821111111111L);
        result = funnyEncode(4821111111111L);
    }

    public static byte[] encode(long ean) {
        EAN13Encoder encoder = new EAN13Encoder(12);
        encoder.buildBars(getDigitArray(ean));
        return encoder.getByteArray();
    }

    public static byte[] funnyEncode(long ean) {
        EAN13Encoder encoder = new EAN13Encoder(16);
        encoder.moveBitPos(9);
        encoder.buildBars(getDigitArray(ean));
        byte[] byteArray = encoder.getByteArray();
        return hexEncode(byteArray);
    }

    private byte[] byteArray;
    private int bitPos = 0;

    private EAN13Encoder(int byteArrayLength) {
        byteArray = new byte[byteArrayLength];
    }

    private byte[] getByteArray() {
        return byteArray;
    }

    private void moveBitPos(int diff) {
        bitPos += diff;
    }

    public static int[] getDigitArray(long ean) {
        int[] digitArray = new int[13];
        int index = 12;
        while (ean > 0) {
            digitArray[index] = (int)(ean % 10);
            ean = ean / 10;
            index--;
        }
        return digitArray;
    }

    private void buildBars(int[] digitArray) {
        // left synchronization bars
        addBits(3, 0x05);

        // left half
        int firstDigit = digitArray[0];
        int lg_switch = LG_SWITCH[firstDigit];
        for (int i = 0; i < 6; i++) {
            int digit = digitArray[i + 1];
            int pattern = (lg_switch & (0x20 >> i)) == 0 ? L_BARS[digit] : G_BARS[digit];
            addBits(7, pattern);
        }

        // middle synchronization bars
        addBits(5, 0x0a);

        // right half
        for (int i = 0; i < 6; i++) {
            int digit = digitArray[i + 7];
            int pattern = R_BARS[digit];
            addBits(7, pattern);
        }

        // right synchronization bars
        addBits(3, 0x05);
    }

    private void addBits(int numBits, int pattern) {
        int currentBytePos = bitPos >> 3;
        int remainingBits = 8 - (bitPos & 0x07);
        byte currentByte = byteArray[currentBytePos];
        if (numBits <= remainingBits) {
            currentByte = (byte)(currentByte | (pattern << (remainingBits - numBits)));
        } else {
            currentByte = (byte)(currentByte | (pattern >> (numBits - remainingBits)));
            byteArray[currentBytePos] = currentByte;
            currentBytePos++;
            currentByte = (byte)(pattern << (8 - (numBits - remainingBits)));
        }
        byteArray[currentBytePos] = currentByte;
        bitPos += numBits;
    }

    private static byte[] hexEncode(byte[] src) {
        byte[] dest = new byte[src.length * 2];
        for (int i = 0; i < src.length; i++) {
            dest[i * 2] = HEX_DIGITS[(src[i] & 0xff) >>> 4];
            dest[i * 2 + 1] = HEX_DIGITS[src[i] & 0x0f];
        }
        return dest;
    }

    private static final int[] LG_SWITCH = { 0x00, 0x0b, 0x0d, 0x0e, 0x13, 0x19, 0x1c, 0x15, 0x16, 0x1a };    
    private static final int[] L_BARS = { 0x0d, 0x19, 0x13, 0x3d, 0x23, 0x31, 0x2f, 0x3b, 0x37, 0x0b };
    private static final int[] G_BARS = { 0x27, 0x33, 0x1b, 0x21, 0x1d, 0x39, 0x05, 0x11, 0x09, 0x17 };
    private static final int[] R_BARS = { 0x72, 0x66, 0x6c, 0x42, 0x5c, 0x4e, 0x50, 0x44, 0x48, 0x74 };
    private static final byte[] HEX_DIGITS = { 0x30, 0x31, 032, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };
}