public byte[] encode(String contents) { int length = contents.length(); if (length > 80) { throw new IllegalArgumentException( "Requested contents should be less than 80 digits long, but got " + length); } int codeWidth = 11 + 11 + 13; //start plus check plus stop character //get total code width for this barcode for (int i = 0; i < length; i++) { int[] patterns = Code128Reader.CODE_PATTERNS[contents.charAt(i) - ' ']; for (int j = 0; j < patterns.length; j++) { codeWidth += patterns[j]; } } byte[] result = new byte[codeWidth]; int pos = appendPattern(result, 0, Code128Reader.CODE_PATTERNS[104], 1); int check = 104; //append next character to bytematrix for(int i = 0; i < length; i++) { check += (contents.charAt(i) - ' ') * (i + 1); pos += appendPattern(result, pos, Code128Reader.CODE_PATTERNS[contents.charAt(i) - ' '],1); } //compute checksum and append it along with end character and quiet space check %= 103; pos += appendPattern(result,pos,Code128Reader.CODE_PATTERNS[check],1); pos += appendPattern(result,pos,Code128Reader.CODE_PATTERNS[106],1); return result; }
BitMatrix bitMatrix = new Code128Writer().write( "ABC-abc-1234", BarcodeFormat.CODE_128, 350, 150, hints );
private static int chooseCode(CharSequence value, int start, int oldCode) { CType lookahead = findCType(value, start); if (lookahead == CType.UNCODABLE || lookahead == CType.ONE_DIGIT) { return CODE_CODE_B; // no choice lookahead = findCType(value, start + 2); if (lookahead == CType.UNCODABLE || lookahead == CType.ONE_DIGIT) { return CODE_CODE_B; // not worth switching now lookahead = findCType(value, start + 3); if (lookahead == CType.TWO_DIGITS) { // then two more digits, switch return CODE_CODE_C; while ((lookahead = findCType(value, index)) == CType.TWO_DIGITS) { index += 2; lookahead = findCType(value, start + 1);
int newCodeSet = chooseCode(contents, position, codeSet); int pos = 0; for (int[] pattern : patterns) { pos += appendPattern(result, pos, pattern, true);
Code128Writer code128Writer = new Code128Writer(); Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); BitMatrix bitMatrix = code128Writer.encode(barcodeMessage, BarcodeFormat.CODE_128, width, height-textHeight-(2*fm.getAscent()), hintMap);
int newCodeSet = chooseCode(contents, position, codeSet); int pos = 0; for (int[] pattern : patterns) { pos += appendPattern(result, pos, pattern, true);
Writer codeWriter; if (barcodeFormat == BarcodeFormat.QR_CODE) { codeWriter = new QRCodeWriter (); } else if (barcodeFormat == BarcodeFormat.CODE_128) { codeWriter = new Code128Writer (); } else { throw new RuntimeException ("Format Not supported."); }
private static int chooseCode(CharSequence value, int start, int oldCode) { CType lookahead = findCType(value, start); if (lookahead == CType.UNCODABLE || lookahead == CType.ONE_DIGIT) { return CODE_CODE_B; // no choice lookahead = findCType(value, start + 2); if (lookahead == CType.UNCODABLE || lookahead == CType.ONE_DIGIT) { return CODE_CODE_B; // not worth switching now lookahead = findCType(value, start + 3); if (lookahead == CType.TWO_DIGITS) { // then two more digits, switch return CODE_CODE_C; while ((lookahead = findCType(value, index)) == CType.TWO_DIGITS) { index += 2; lookahead = findCType(value, start + 1);
cos.endText(); bitMatrix = new Code128Writer().encode("M1G542LL/A", BarcodeFormat.CODE_128, 150, 80, null); BufferedImage buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix); PDXObjectImage ximage = new PDPixelMap(document, buffImg); cos.endText(); bitMatrix = new Code128Writer().encode("123456789", BarcodeFormat.CODE_128, 150, 80, null); buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix); ximage = new PDPixelMap(document, buffImg); bitMatrix = new Code128Writer().encode("M1G542LL/A", BarcodeFormat.CODE_128, 150, 80, null); buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix); cos.endText(); bitMatrix = new Code128Writer().encode("352065061762230", BarcodeFormat.CODE_128, 150, 80, null); buffImg=MatrixToImageWriter.toBufferedImage(bitMatrix); ximage = new PDPixelMap(document, buffImg);
codeWriter = new QRCodeWriter (); } else if (barcodeFormat == BarcodeFormat.CODE_128) { codeWriter = new Code128Writer (); } else { throw new RuntimeException ("Format Not supported.");
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); }