0

I want to display some tiff's which a stored in BLOB's in an oracle database. I just tried to convert them from tiff to jpg or png using im4java, but that didn't work. Maybe there is a possibility to display .tiff and other img data types in Jasper without converting them?! Or if not, does anybody have a detailed solution how to convert them? (which image class do I need to configure, how to implement in a scriptlet etc.)

Thanks everybody!

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

      ConvertCmd convert = new ConvertCmd();
      convert.run(op, new Object[]{inputImage.getAbsolutePath(), outputImage.getAbsolutePath()});
    return outputImage;`
Aaron R.
  • 93
  • 2
  • 9
  • In what way did it not work? Did the conversion fail? Are you unable to display the converted files? Did you get any exceptions or error messages? – Harald K Jun 20 '17 at 08:07
  • @haraldK `net.sf.jasperreports.engine.JRException: The cell cannot be added. at com.jaspersoft.studio.editor.preview.actions.export.AExportA‌​ction$3.run(AExportA‌​ction.java:188) at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55) Caused by: net.sf.jasperreports.engine.JRException: Image read failed.` That's a part of the error message, so I think the conversion failed. I don't know how to exactly implement the code in a scriptlet, so the conversion executes before every detail band.. Another problem is that not all of the images are tiff, but also jpg and some more old img types.. – Aaron R. Jun 20 '17 at 09:14

1 Answers1

0

Solved the problem, here's the code worked for me using jai-imageIO.jar to convert tiff to 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;
}
Community
  • 1
  • 1
Aaron R.
  • 93
  • 2
  • 9