如何将TIFF ImageReader添加到在Grails中注册的人员 [英] How to add TIFF ImageReader to those registered in Grails

查看:97
本文介绍了如何将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。



问题 h1>

我知道我需要JAI才能读取TIFF文件。 jai_core.jar,jai_codec.jar文件位于我的类路径中。事实上,因为我在Mac OSX上,它们会自动安装。但是,当我运行grails应用程序并尝试从SOAP服务接收的字节构造TIFF图像时,出现此错误:

  |错误2013-06-18 15:23:38,135 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver  - 处理请求时发生IllegalArgumentException:[GET] / BDMPlugin / BDMPlugin / displayImageFromRef  - 参数:
pageRef :28:22072FBCA0A8889D9C041D76A588BCF4DCB40376A23B5FD5C301378C8E66EB9F4933A5DFCA46365F927D9E91B337B6E1E980FB4406644801
类型:TIFF
im == null !. Stacktrace如下:
消息:im == null!
Line |方法
- >> 1457 |写入javax.imageio.ImageIO
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 1571 |写入''
| 28 | writeImageToResponse in edu.missouristate.bdmplugin.ImageService
| 44 | '
|中的bytesToPng 39 | displayImageFromRef in edu.missouristate.bdmplugin.BDMPluginController
| 895 | java.util.concurrent.ThreadPoolExecutor中的runTask $ Worker
| 918 |跑 。 。 。 。 。 。 。 。 in''
^ 680 |在java.lang.Thread中运行

我尝试了以下

 .jroller.com / phidias / entry / groovy_what_imagereaders_are_installedrel =nofollow noreferrer>脚本 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 |标准BMP图像阅读器
Sun Microsystems,Inc. | 1.0 |标准GIF图像阅读器
Sun Microsystems,Inc. | 1.0 |标准WBMP图像阅读器
Sun Microsystems,Inc. | 1.0 |标准PNG图像阅读器
Sun Microsystems,Inc. | 0.5 |标准的JPEG图像阅读器

但是,如果我在Groovy控制台中运行相同的Groovy脚本,输出:

  Sun Microsystems,Inc. | 0.5 |标准JPEG图像阅读器
Sun Microsystems,Inc. | 1.0 |标准BMP图像阅读器
Sun Microsystems,Inc. | 1.0 |标准WBMP图像阅读器
Sun Microsystems,Inc. | 1.0 |标准PNG图像阅读器
Sun Microsystems,Inc. | 1.0 |标准GIF图像阅读器
Apple计算机公司| 1.0 |标准TIFF图像阅读器

同一套阅读器,但它也包含Apple的TIFF阅读器。为什么GroovyConsole能够找到它而不是我的Grails环境,即使它们都使用相同的JRE?有没有一种方法可以通过一些从导入 com.sun.media.jai com.sun.media导入来手动添加TIFF阅读器。 imageio.plugins.tiff



我尝试添加TIFFImageReaderSpi到我的服务方法的手动注册:

  import com.sun.imageio.plugins.tiff.TIFFImageReaderSpi 
...
IIORegistry reg = IIORegistry.getDefaultInstance()
reg.registerServiceProvider(new TIFFImageReaderSpi())

originalImage变量仍然返回null。

解决方案

因此,似乎遇到了一些问题。



首先, import com.sun.imageio.plugins.tiff.TIFFImageReaderSpi 语句是导入Apple TIFF阅读器,显然这不符合阅读我的TIFF 。



我需要的是 import com.sun.media.imageioimpl.plugins.tiff.TIFFImageReaderSp我,但是这给我带来了几个不同的错误;别担心,我可以修复它们。 :)

首先,导入没有解决。为了获得com.sun.media.imageioimpl软件包,我从 https获得捆绑JAI的源代码://github.com/stain/jai-imageio-core 。我将它导入到Eclipse中,然后使用Eclipse的导出工具构建了一个JAR。这我放在我的项目的lib文件夹,但导入仍然没有解决。我必须手动将该jar添加到我的项目的类路径中,然后导入才能解决。

第二,当我运行应用程序时,出现以下错误: / p>

  |错误2013-06-19 11:15:27,665 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver  - 处理请求时发生IllegalArgumentException:[GET] / pluginproject / Controller / action  -  parameters:
vendorName == null! Stacktrace如下:
消息:vendorName == null!
Line |方法
- >> 59 | < INIT> in javax.imageio.spi.IIOServiceProvider
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 214 | < INIT>在javax.imageio.spi.ImageReaderWriterSpi
|中192 | < INIT> 。 。 。 。 。 。在javax.imageio.spi.ImageReaderSpi
|中88 | < INIT> in com.sun.media.imageioimpl.plugins.tiff.TIFFImageReaderSpi
| 31 | bytesToPng。 。 。 。 in edu.mystateu.pluginproject.ImageService

vendorName == null?幸运的是,我发现这个问题/答案



创建jai-imageio-core的jar文件,我不得不手动指定清单文件的位置,而不是让Eclipse生成一个新的空白文件。清单文件位于/jai-imageio-core/src/main/resources/META-INF/MANIFEST.MF中,一旦我指定使用该文件,导入的lib就会解析并读取我的映像。



最后,服务方法的代码非常好。我只需要将JAI正确导入到我的项目中。非常感谢@haraldK,他的反馈让我走上了正轨。

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.

解决方案

So there seem to have been a couple layers to the problem.

First, the import com.sun.imageio.plugins.tiff.TIFFImageReaderSpi statement was importing the Apple TIFF reader, which apparently just wasn't up to the job of reading my TIFF.

What I really needed was import com.sun.media.imageioimpl.plugins.tiff.TIFFImageReaderSpi, but that presented me with a couple of different errors; don't worry, I was able to fix them. :)

First, the import wasn't resolving. To get the com.sun.media.imageioimpl package, I got the source for a bundled JAI from https://github.com/stain/jai-imageio-core. I imported that into Eclipse, then built a JAR using Eclipse's Export tool. This I placed in my project's lib folder, but the import still wasn't resolving. I had to manually add that jar to my project's classpath, and then the import would resolve.

Second, when I ran the app, I'd get this error:

| Error 2013-06-19 11:15:27,665 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver  - IllegalArgumentException occurred when processing request: [GET] /pluginproject/Controller/action - parameters:
vendorName == null!. Stacktrace follows:
Message: vendorName == null!
   Line | Method
->>  59 | <init>              in javax.imageio.spi.IIOServiceProvider
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   214 | <init>              in javax.imageio.spi.ImageReaderWriterSpi
|   192 | <init> . . . . . .  in javax.imageio.spi.ImageReaderSpi
|    88 | <init>              in com.sun.media.imageioimpl.plugins.tiff.TIFFImageReaderSpi
|    31 | bytesToPng . . . .  in edu.mystateu.pluginproject.ImageService

vendorName == null? Fortunately, I found this question/answer.

When creating the jar file for jai-imageio-core, I had to manually specify the location of the manifest file, rather than letting Eclipse generate a new blank one. The manifest file was located in /jai-imageio-core/src/main/resources/META-INF/MANIFEST.MF, and once I specified to use that one, the imported lib resolved and read my image.

In the end, the service method's code was perfectly fine. I just needed to actually get JAI imported into my project correctly. Thanks very much to @haraldK, whose feedback got me on the right track.

这篇关于如何将TIFF ImageReader添加到在Grails中注册的人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆