public byte[] encode(String contents) { if (contents.length() != 13) { throw new IllegalArgumentException( "Requested contents should be 13 digits long, but got " + contents.length()); } int firstDigit = Integer.parseInt(contents.substring(0, 1)); int parities = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit]; byte[] result = new byte[codeWidth]; int pos = 0; pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1); // See {@link #EAN13Reader} for a description of how the first digit & left bars are encoded for (int i = 1; i <= 6; i++) { int digit = Integer.parseInt(contents.substring(i, i + 1)); if ((parities >> (6 - i) & 1) == 1) { digit += 10; } pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], 0); } pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, 0); for (int i = 7; i <= 12; i++) { int digit = Integer.parseInt(contents.substring(i, i + 1)); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 1); } pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1); return result; }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (format != BarcodeFormat.UPC_A) { throw new IllegalArgumentException("Can only encode UPC-A, but got " + format); } // Transform a UPC-A code into the equivalent EAN-13 code and write it that way return subWriter.encode('0' + contents, BarcodeFormat.EAN_13, width, height, hints); }
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Hashtable hints) throws WriterException { Writer writer; if (format == BarcodeFormat.EAN_8) { writer = new EAN8Writer(); } else if (format == BarcodeFormat.EAN_13) { writer = new EAN13Writer(); } else if (format == BarcodeFormat.QR_CODE) { writer = new QRCodeWriter(); } else if (format == BarcodeFormat.CODE_39) { writer = new Code39Writer(); } else if (format == BarcodeFormat.CODE_128) { writer = new Code128Writer(); } else if (format == BarcodeFormat.ITF) { writer = new ITFWriter(); } else { throw new IllegalArgumentException("No encoder available for format " + format); } return writer.encode(contents, format, width, height, hints); }
int pos = 0; pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true); digit += 10; pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], false); pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, false); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], true); appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true);
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (format != BarcodeFormat.UPC_A) { throw new IllegalArgumentException("Can only encode UPC-A, but got " + format); } // Transform a UPC-A code into the equivalent EAN-13 code and write it that way return subWriter.encode('0' + contents, BarcodeFormat.EAN_13, width, height, hints); }
int pos = 0; pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true); digit += 10; pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], false); pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, false); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], true); appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true);