1

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.getYHeight());

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.

Tom
  • 461
  • 2
  • 7
  • 19

2 Answers2

4

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();
Eriksberger
  • 191
  • 4
  • Thanks for responding. I will keep your code sample handy should I have to convert images in the future. Turns out I didn't need to convert anything this time. – Tom Aug 17 '11 at 19:01
1

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.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Tom
  • 461
  • 2
  • 7
  • 19