JasperReports - 在db中显示存储在BLOB中的tiff

时间:2017-06-19 10:20:20

标签: java jasper-reports jpeg tiff image-conversion

我想显示一些存储在oracle数据库中的BLOB中的tiff。我只是尝试使用im4java将它们从tiff转换为jpg或png,但这并不起作用。也许有可能在没有转换它们的情况下在Jasper中显示.tiff和其他img数据类型?!或者如果没有,是否有人有详细的解决方案如何转换它们? (我需要配置哪个图像类,如何在scriptlet中实现等)

谢谢大家!

      IMOperation op = new IMOperation();
      op.addImage();
      op.addImage(); 

      ConvertCmd convert = new ConvertCmd();
      convert.run(op, new Object[]{inputImage.getAbsolutePath(), outputImage.getAbsolutePath()});
    return outputImage;`

1 个答案:

答案 0 :(得分:0)

解决了问题,这里的代码使用jai-imageIO.jar将tiff转换为jpg:

  

import java.awt.image.BufferedImage;   import java.io.ByteArrayInputStream;   import java.io.ByteArrayOutputStream;   import java.io.IOException;   import java.io.InputStream;

     

import javax.imageio.ImageIO;   import javax.imageio.spi.IIORegistry;

     

import net.sf.jasperreports.engine.JRAbstractScriptlet;   import net.sf.jasperreports.engine.JRScriptletException;

public static InputStream convertTifToPng(InputStream inputImage)
        throws IOException, InterruptedException {
    IIORegistry registry = IIORegistry.getDefaultInstance();
    registry.registerServiceProvider(new com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriterSpi());
    registry.registerServiceProvider(new com.sun.media.imageioimpl.plugins.tiff.TIFFImageReaderSpi());

    BufferedImage image;

    if (inputImage != null) {
        image = ImageIO.read(inputImage);
    } else {
        return null;
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    InputStream outputImage = new ByteArrayInputStream(baos.toByteArray());

    return outputImage;
}