2011-08-16 90 views
1

我使用iText的創建PDF417條碼,像這樣:從CCITT格式轉換iText的條碼圖像爲PNG

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

}

我需要通過barcode.getImage()爲JPG,GIF返回CCITT格式轉換,或PNG,所以我可以將它包含在我在JasperReports中創建的文檔中。

回答

1

我想出瞭解決辦法:

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的支持java.awt.Image類型在報告中使用的圖像。

4

這樣的事情呢?

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(); 
+0

感謝您的回覆。如果將來必須轉換圖像,我會保持您的代碼示例方便。原來我這次不需要轉換任何東西。 – Tom