@RequestMapping(value = "/pdf417/{id}", method = RequestMethod.GET, produces = IMAGE_PNG) public void barcode417(@PathVariable("id") String id, HttpServletResponse response) throws IOException, WriterException { if (!CLIENT_ID_PATTERN.matcher(id).matches()) { throw new InputValidationException("Invalid clientId for barcode [" + id + "]"); } response.setContentType(IMAGE_PNG); Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {{ put(EncodeHintType.MARGIN, MARGIN_PIXELS); put(EncodeHintType.ERROR_CORRECTION, 2); put(EncodeHintType.PDF417_COMPACT, true); put(EncodeHintType.PDF417_COMPACTION, Compaction.TEXT); }}; BitMatrix matrix = new MultiFormatWriter().encode(id, BarcodeFormat.PDF_417, BARCODE_WIDTH, BARCODE_HEIGHT, hints); BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(matrix); BufferedImage croppedImage = cropImageWorkaroundDueToZxingBug(bufferedImage); ImageIO.write(croppedImage, "PNG", response.getOutputStream()); }
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException { if (format != BarcodeFormat.PDF_417) { throw new IllegalArgumentException("Can only encode PDF_417, but got " + format); } PDF417 encoder = new PDF417(); int margin = 30; int errorCorrectionLevel = 2; if (hints != null) { if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) { encoder.setCompact(((Boolean) hints.get(EncodeHintType.PDF417_COMPACT)) .booleanValue()); } if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) { encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION)); } if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows()); } if (hints.containsKey(EncodeHintType.MARGIN)) { margin = ((Number) hints.get(EncodeHintType.MARGIN)).intValue(); } if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) { errorCorrectionLevel = ((Number) hints.get(EncodeHintType.ERROR_CORRECTION)) .intValue(); } if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { encoder.setEncoding(Charset.forName((String) hints.get(EncodeHintType .CHARACTER_SET))); } } return bitMatrixFromEncoder(encoder, contents, errorCorrectionLevel, width, height, margin); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (format != BarcodeFormat.PDF_417) { throw new IllegalArgumentException("Can only encode PDF_417, but got " + format); } PDF417 encoder = new PDF417(); int margin = WHITE_SPACE; if (hints != null) { if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) { encoder.setCompact((Boolean) hints.get(EncodeHintType.PDF417_COMPACT)); } if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) { encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION)); } if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows()); } if (hints.containsKey(EncodeHintType.MARGIN)) { margin = ((Number) hints.get(EncodeHintType.MARGIN)).intValue(); } if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { String encoding = (String) hints.get(EncodeHintType.CHARACTER_SET); encoder.setEncoding(Charset.forName(encoding)); } } return bitMatrixFromEncoder(encoder, contents, width, height, margin); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException { if (format != BarcodeFormat.PDF_417) { throw new IllegalArgumentException("Can only encode PDF_417, but got " + format); } PDF417 encoder = new PDF417(); int margin = WHITE_SPACE; if (hints != null) { if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) { encoder.setCompact((Boolean) hints.get(EncodeHintType.PDF417_COMPACT)); } if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) { encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION)); } if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows()); } if (hints.containsKey(EncodeHintType.MARGIN)) { margin = ((Number) hints.get(EncodeHintType.MARGIN)).intValue(); } if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { String encoding = (String) hints.get(EncodeHintType.CHARACTER_SET); encoder.setEncoding(Charset.forName(encoding)); } } return bitMatrixFromEncoder(encoder, contents, width, height, margin); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException { if (format != BarcodeFormat.PDF_417) { throw new IllegalArgumentException("Can only encode PDF_417, but got " + format); } PDF417 encoder = new PDF417(); if (hints != null) { if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) { encoder.setCompact((Boolean) hints.get(EncodeHintType.PDF417_COMPACT)); } if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) { encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION)); } if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows()); } } return bitMatrixFromEncoder(encoder, contents, width, height); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (format != BarcodeFormat.PDF_417) { throw new IllegalArgumentException("Can only encode PDF_417, but got " + format); } PDF417 encoder = new PDF417(); if (hints != null) { if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) { encoder.setCompact((Boolean) hints.get(EncodeHintType.PDF417_COMPACT)); } if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) { encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION)); } if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows()); } } return bitMatrixFromEncoder(encoder, contents, width, height); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (format != BarcodeFormat.PDF_417) { throw new IllegalArgumentException("Can only encode PDF_417, but got " + format); } PDF417 encoder = new PDF417(); int margin = WHITE_SPACE; int errorCorrectionLevel = DEFAULT_ERROR_CORRECTION_LEVEL; if (hints != null) { if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) { encoder.setCompact(Boolean.valueOf(hints.get(EncodeHintType.PDF417_COMPACT).toString())); } if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) { encoder.setCompaction(Compaction.valueOf(hints.get(EncodeHintType.PDF417_COMPACTION).toString())); } if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows()); } if (hints.containsKey(EncodeHintType.MARGIN)) { margin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString()); } if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) { errorCorrectionLevel = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString()); } if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { Charset encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); encoder.setEncoding(encoding); } } return bitMatrixFromEncoder(encoder, contents, errorCorrectionLevel, width, height, margin); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException { if (format != BarcodeFormat.PDF_417) { throw new IllegalArgumentException("Can only encode PDF_417, but got " + format); } PDF417 encoder = new PDF417(); int margin = WHITE_SPACE; int errorCorrectionLevel = DEFAULT_ERROR_CORRECTION_LEVEL; if (hints != null) { if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) { encoder.setCompact(Boolean.valueOf(hints.get(EncodeHintType.PDF417_COMPACT).toString())); } if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) { encoder.setCompaction(Compaction.valueOf(hints.get(EncodeHintType.PDF417_COMPACTION).toString())); } if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); encoder.setDimensions(dimensions.getMaxCols(), dimensions.getMinCols(), dimensions.getMaxRows(), dimensions.getMinRows()); } if (hints.containsKey(EncodeHintType.MARGIN)) { margin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString()); } if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) { errorCorrectionLevel = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString()); } if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { Charset encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); encoder.setEncoding(encoding); } } return bitMatrixFromEncoder(encoder, contents, errorCorrectionLevel, width, height, margin); }
@Override public Compaction pdf417Compaction() { return pdf417Compaction; }