I'm trying to create a Grails application that can display previews of TIFF files, and other images as well.
Background
The images are constructed from a SOAP service that gives me the bytes of the image. In a service method, I take the byte[], construct a ByteArrayInputStream from it, and then create a BufferedImage from that.
def inputStream = new ByteArrayInputStream(bytes)
BufferedImage originalImage = ImageIO.read(inputStream)
ImageIO.write(originalImage, 'png', response.outputStream)
For JPGs, I can easily stream the images to the browser this way as the src of an img tag. TIFFs, though, I'd need to convert the images into some other format (preferably JPG or PNG) to make them the src of an tag.
The Problem
I know that I need JAI in order to read the TIFF files. The jai_core.jar, jai_codec.jar files are in my classpath. In fact, because I'm on Mac OSX, they're installed automatically. However, when I run the grails application and it tries to construct a TIFF image from the bytes received from the SOAP service, I get this error:
| Error 2013-06-18 15:23:38,135 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver - IllegalArgumentException occurred when processing request: [GET] /BDMPlugin/BDMPlugin/displayImageFromRef - parameters:
pageRef: 28:22072FBCA0A8889D9C041D76A588BCF4DCB40376A23B5FD5C301378C8E66EB9F4933A5DFCA46365F927D9E91B337B6E1E980FB4406644801
type: TIFF
im == null!. Stacktrace follows:
Message: im == null!
Line | Method
->> 1457 | write in javax.imageio.ImageIO
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1571 | write in ''
| 28 | writeImageToResponse in edu.missouristate.bdmplugin.ImageService
| 44 | bytesToPng in ''
| 39 | displayImageFromRef in edu.missouristate.bdmplugin.BDMPluginController
| 895 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
| 918 | run . . . . . . . . in ''
^ 680 | run in java.lang.Thread
I tried the following script to figure out which image readers were installed:
IIORegistry reg = IIORegistry.getDefaultInstance();
Iterator spIt = reg.getServiceProviders(ImageReaderSpi.class, false);
spIt.each(){
println it.getVendorName() << " | " << it.getVersion() << " | "<< it.getDescription() ;
}
This outputs the following:
Sun Microsystems, Inc. | 1.0 | Standard BMP Image Reader
Sun Microsystems, Inc. | 1.0 | Standard GIF image reader
Sun Microsystems, Inc. | 1.0 | Standard WBMP Image Reader
Sun Microsystems, Inc. | 1.0 | Standard PNG image reader
Sun Microsystems, Inc. | 0.5 | Standard JPEG Image Reader
However, if I run that same Groovy script in the Groovy Console, I get this output:
Sun Microsystems, Inc. | 0.5 | Standard JPEG Image Reader
Sun Microsystems, Inc. | 1.0 | Standard BMP Image Reader
Sun Microsystems, Inc. | 1.0 | Standard WBMP Image Reader
Sun Microsystems, Inc. | 1.0 | Standard PNG image reader
Sun Microsystems, Inc. | 1.0 | Standard GIF image reader
Apple computer Inc. | 1.0 | Standard TIFF image reader
Same set of readers, but it also includes Apple's TIFF reader. Why is the GroovyConsole able to find it and not my Grails environment, even though they're both using the same JRE? Is there a way that I can manually add the TIFF reader via some import from import com.sun.media.jai or com.sun.media.imageio.plugins.tiff?
I tried adding a manual registration of the TIFFImageReaderSpi to my service method:
import com.sun.imageio.plugins.tiff.TIFFImageReaderSpi
...
IIORegistry reg = IIORegistry.getDefaultInstance()
reg.registerServiceProvider(new TIFFImageReaderSpi())
The originalImage variable still comes back null.