我正在嘗試創建一個Grails應用程序,它可以顯示TIFF文件和其他圖像的預覽。如何將TIFF ImageReader添加到在Grails中註冊的人
背景
圖像是從一個SOAP服務構造的,該服務給了我圖像的字節。在服務方法中,我使用byte [],從中構建一個ByteArrayInputStream,然後從中創建一個BufferedImage。
def inputStream = new ByteArrayInputStream(bytes)
BufferedImage originalImage = ImageIO.read(inputStream)
ImageIO.write(originalImage, 'png', response.outputStream)
對於JPG,我可以很容易地將圖像作爲img標籤的src流式傳輸到瀏覽器。 TIFFs,但是,我需要將圖像轉換爲其他格式(最好是JPG或PNG),以使它們成爲標籤的src。
的問題
我知道I need JAI in order to read the TIFF files。 jai_core.jar,jai_codec.jar文件位於我的類路徑中。事實上,因爲我在Mac OSX上,它們會自動安裝。然而,當我運行Grails應用程序,並嘗試構建從SOAP服務接收的字節的TIFF圖像,我得到這個錯誤:
| 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
我嘗試以下script找出安裝了哪些圖像閱讀器:
IIORegistry reg = IIORegistry.getDefaultInstance();
Iterator spIt = reg.getServiceProviders(ImageReaderSpi.class, false);
spIt.each(){
println it.getVendorName() << " | " << it.getVersion() << " | "<< it.getDescription() ;
}
此輸出以下:
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
但是,如果我跑,在Groovy的控制檯相同的Groovy腳本,我得到這個輸出:
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
同一套閱讀器,但它也包括蘋果的TIFF閱讀器。爲什麼GroovyConsole能夠找到它而不是我的Grails環境,即使它們都使用相同的JRE?有沒有一種方法可以通過從導入com.sun.media.jai
或com.sun.media.imageio.plugins.tiff
進口的某些導入手動添加TIFF閱讀器?
我嘗試添加了TIFFImageReaderSpi的手工登記到我的服務方法:
import com.sun.imageio.plugins.tiff.TIFFImageReaderSpi
...
IIORegistry reg = IIORegistry.getDefaultInstance()
reg.registerServiceProvider(new TIFFImageReaderSpi())
的originalImage變量仍然回來空。
我有理由確定(至少部分)問題是我需要註冊一個可以處理TIFF文件的ImageWriter。然而,在我的JAI課程中似乎沒有這樣的課程。在尋找它的時候,JAI似乎已經有點像圖書館一樣不存在了。如果有人在Grails中取得成功,我可以選擇將TIFF轉換爲更友好的圖像。 – jonnybot
+1格式良好的問題。 – dmahapatro
@jonnybot ImageIO使用服務查找機制。通常不需要手動註冊'Spi's,只需將相應的JAR放在classpath上即可。此外,您不需要支持TIFF的ImageWriter,因爲您只需編寫JPEG和PNG。 – haraldK