我想使用IM4J(ImageMagick的Java包装器)来创建JPEG的缩略图,这是我对这两个库的第一次体验(曾经)。请注意,这是我的技术负责人交给我的硬性要求(所以请不要建议使用除IM4J/ImageMagick以外的任何其他解决方案) - 我的双手与技术选择紧密相连!ImageMagick/IM4J FileNotFoundException
我得到FileNotFoundException
和convert
命令,它告诉我没有这些库(或两个)之一正确设置。
在我的电脑上,这里是我的目录结构:
C:/
myApp/
images/ --> where all of my JPEGs are
thumbnails/ --> where I want ImageMagick to send the converted thumbnails to
imageMagickHome/ --> Where I downloaded the DLL to
ImageMagick-6.7.6-1-Q16-windows-dll.exe
...
在我的Java项目,我请确保IM4J JAR(im4java-1.2.0.jar
)是在运行时类路径。尽管我需要使用IM4J的1.2.0版本,但我可以随意使用我想要的任何版本的ImageMagick。我只是选择了这个版本,因为它似乎是我的Windows 7(32位)机器的最新/稳定版本。 如果我应该使用不同的版本,请在您的答案中通过ImageMagick下载页面向我发送一个链接。
至于ImageMagick的,我刚刚下载的EXE从here并把它放在上面提到的文件夹中 - 我没有做任何安装,向导,微星,环境变量的配置等
然后在我的Java代码:
// In my driver...
File currentFile = new File("C:/myApp/images/test.jpg"); --> exists and is sitting at this location
File thumbFile = new File("C:/myApp/thumbnails/test-thumb.jpg"); --> doesnt exist yet! (destination file)
Thumbnailer myThumbnailer = new Thumbnailer();
myThumbnailer.generateThumbnail(currentFile, thumbFile);
// Then the Thumbnailer:
public class Thumbnailer
{
// ... omitted for brevity
public void generateThumbnail(File originalFile, File thumbnailFile)
{
// Reads appConfig.xml from classpath, validates it against a schema,
// and reads the contents of an element called <imPath> into this
// method's return value. See below
String imPath = getIMPathFromAppConfigFile();
org.im4java.core.IMOperation op = new Operation();
op.colorspace(this.colorSpace);
op.addImage(originalFile.getAbsolutePath());
op.flatten();
op.addImage(thumbnailFile.getAbsolutePath());
ConvertCmd cmd = new ConvertCmd();
cmd.setSearchPath(imPath);
// This next line is what throws the FileNotFoundException
cmd.run(op);
}
}
我appConfig.xml文件中包含的Impath的部分:
<imPath>C:/myApp/imageMagickHome</imPath>
请注意 - 如果此appConfig.xml格式不正确,我们的模式验证器将会捕获它。由于我们没有获得模式验证错误,因此我们可以将其排除为罪魁祸首。 但是,注意我的文件路径分隔符;他们都是正斜杠。我这样做是因为我被告知,在Windows系统中,正斜杠与* nix反斜杠相同,参考文件路径。信不信由你,我们正在开发Windows 机器,但部署到linux服务器,所以这是我的解决方案(再次,我的电话不是!)。
IM4J甚至承认Windows用户有时可能会遇到麻烦,并且在this文章中解释说Windows开发人员可能必须设置一个IM4JAVA_TOOLPATH
env var才能使此库正常工作。我尝试了这个建议,创建了一个新的系统范围的同名环境变量,并将其值设置为C:\myApp\imageMagickHome
。仍然没有区别。 但请注意,我在此使用反斜杠。这是因为这个env var对我的机器来说是本地的,而appConfig.xml是一个配置描述符,它被部署到linux服务器上。
从我所知道的,罪魁祸首可能是以下中的一个(或多个):
- 我没有正确地“安装” ImageMagick的EXE和应该使用安装程序/ MSI;或者我需要为ImageMagick(而不是IM4J)本身添加一些其他环境变量
- 也许我还没有正确配置IM4J并需要添加更多环境变量
- 可能是Windows/* nix“/”vs “\”上述
我也困惑,为什么我得到一个名为“转换”文件FileNotFoundException
提到从我appConfig.xml文件问题:
的Java。 io.FileNotFoundException:转换
我认为这是一个批处理/ shell文件居住在IM4J罐子里面(因为我下载的ImageMagick唯一的东西就是EXE)。但是,如果我提取IM4J jar,我只能看到其中的类。我看到“脚本生成器”类,所以我假设这些在我的cmd.run(op)
调用之前启动并创建convert
文件,也许这就是我所缺少的(也许我需要在执行之前手动启动其中一个生成器,如CmdScriptGenerator
我Thumbnailer
方法。或者,也许我的下载是不完整的。
无论哪种方式,我只是不精通足以与任一库知道从哪里开始。
感谢任何帮助。
不错的问题。 +1 – 2012-04-18 15:59:49
@PinnyM - 我刚刚运行安装程序并将ImageMagick安装到该“C:/ myApp/imageMagickHome”目录中。现在我在我的文件系统上看到以下内容:'C:/ myApp/imageMagickHome/ImageMagick-6.7.6-Q16 /'。我重新运行了驱动程序,并得到**相同的**错误。我是否需要更改我的env var或appConfig.xml设置以包含这个新的(自动安装的)目录? – IAmYourFaja 2012-04-18 16:06:28