我正在尝试使用IM4J(ImageMagick 的 Java 包装器)来创建 JPEG 的缩略图,这是我对这两个库的第一次体验(有史以来)。请注意,这是我的技术主管向我提出的硬性要求(所以请不要建议使用除 IM4J/ImageMagick 以外的任何东西)解决方案 - 我的双手被束缚在这里的技术选择上!
我得到一个FileNotFoundException
on the andconvert
命令,它告诉我我没有正确设置这些库之一(或两者)。
在我的电脑上,这是我的目录结构:
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并将它放在上面提到的文件夹中——我没有做任何安装、向导、MSI、环境变量配置等。
然后,在我的 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 用户有时会遇到麻烦,并在本文中解释说, Windows 开发人员可能必须设置一个IM4JAVA_TOOLPATH
env var 才能使该库正常工作。我尝试了这个建议,创建了一个新的同名系统范围环境变量并将其值设置为C:\myApp\imageMagickHome
. 还是没有区别。但请注意,我在这里使用了反斜杠。这是因为这个 env var 在我的机器上是本地的,而 appConfig.xml 是一个配置描述符,它被部署到 linux 服务器上。
据我所知,罪魁祸首可能是以下一项(或多项):
- 我没有正确“安装”ImageMagick EXE,应该使用安装程序/MSI;或者我需要为 ImageMagick(不是 IM4J)本身添加一些其他环境变量
- 也许我仍然没有正确配置 IM4J,需要添加更多环境变量
- 可能是我的 appConfig.xml 文件中的 Windows/*nix "/" vs. "" 问题,如上所述
我也很困惑为什么我会得到一个FileNotFoundException
名为“convert”的文件:
java.io.FileNotFoundException:转换
我假设这是一个位于 IM4J jar 内某处的批处理/shell 文件(因为我为 ImageMagick 下载的唯一内容是 EXE)。但是,如果我提取 IM4J jar,我只会看到其中的类。我看到“脚本生成器”类,所以我假设这些在我cmd.run(op)
调用之前启动并创建convert
文件,也许这就是我所缺少的(也许我需要手动启动这些生成器之一,就像CmdScriptGenerator
在执行我的Thumbnailer
方法之前一样。 .或者,也许我的下载不完整。
无论哪种方式,我对任何一个库都不够精通,不知道从哪里开始。
感谢您对此的任何帮助。