【问题标题】:Convert iText Barcode Image from CCITT format to PNG将 iText 条形码图像从 CCITT 格式转换为 PNG
【发布时间】:2011-08-16 12:42:43
【问题描述】:

我正在使用 iText 创建一个 PDF417 条形码,如下所示:

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() 返回的 CCITT 格式转换为 JPG、GIF 或 PNG,以便我可以将其包含在我在 JasperReports 中创建的文档中。

【问题讨论】:

    标签: java jasper-reports itext


    【解决方案1】:

    这样的事情怎么样?

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

    【讨论】:

    • 感谢您的回复。如果将来我必须转换图像,我会将您的代码示例放在手边。原来我这次不需要转换任何东西。
    【解决方案2】:

    我想出的解决方案:

    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 类型。

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 1970-01-01
      • 2011-05-11
      • 2017-05-11
      • 2023-03-05
      • 1970-01-01
      • 2013-06-13
      • 2023-04-01
      相关资源
      最近更新 更多