I'm using iText to create a PDF417 bar code like so:
private InputStream getBarcode() throws Exception {
BarcodePDF417 barcode = new BarcodePDF417();
barcode.setText("Sample bar code text");
Image image = barcode.getImage();
image.scalePercent(50, 50 * barcode.g开发者_JAVA百科etYHeight());
return new ByteArrayInputStream(image.getRawData());
}
I need to convert the CCITT format returned by barcode.getImage()
to either JPG, GIF, or PNG so I can include it in a document I'm creating in JasperReports.
How about something like this?
BarcodePDF417 barcode = new BarcodePDF417();
barcode.setText("Bla bla");
java.awt.Image img = barcode.createAwtImage(Color.BLACK, Color.WHITE);
BufferedImage outImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
outImage.getGraphics().drawImage(img, 0, 0, null);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ImageIO.write(outImage, "png", bytesOut);
bytesOut.flush();
byte[] pngImageData = bytesOut.toByteArray();
FileOutputStream fos = new FileOutputStream("C://barcode.png");
fos.write( pngImageData);
fos.flush();
fos.close();
The solution I came up with:
private Image getBarcode() throws Exception {
BarcodePDF417 barcode = new BarcodePDF417();
barcode.setText("Sample bar code text");
barcode.setAspectRatio(.25f);
return barcode.createAwtImage(Color.BLACK, Color.WHITE);
}
JasperReports supports the java.awt.Image
type for images used in a report.
精彩评论