gpt4 book ai didi

java - ImageMagick/IM4J FileNotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:36 25 4
gpt4 key购买 nike

我正在尝试使用 IM4J (ImageMagick 的 Java 包装器)来创建 JPEG 的缩略图,这是我(有史以来)对这两个库的第一次体验。请注意,这是我的技术主管向我提出的一项硬性要求(所以请不要建议使用除 IM4J/ImageMagick 以外的任何东西)解决方案 - 我的双手被束缚在此处的技术选择上!
我收到了 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,我刚刚从 here 下载了那个 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 格式不正确,我们的模式 validator 将捕获它。由于我们没有收到架构验证错误,因此我们可以将其排除为罪魁祸首。 然而 ,注意我的文件路径分隔符;它们都是正斜杠。我这样做是因为有人告诉我,在 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,需要添加更多的环境变量
  • 可能是我的 appConfig.xml 文件中的 Windows/*nix "/"vs. ""问题,如上所述

  • 我也很困惑为什么我得到一个 FileNotFoundException在名为“convert”的文件上:

    java.io.FileNotFoundException: convert


    我假设这是一个位于 IM4J jar 内某处的批处理/shell 文件(因为我为 ImageMagick 下载的唯一内容是 EXE)。但是,如果我提取 IM4J jar,我只会看到其中的类。我看到“脚本生成器”类,所以我假设这些在我的 cmd.run(op) 之前开始。调用并创建 convert文件,也许这就是我所缺少的(也许我需要在执行我的 CmdScriptGenerator 方法之前手动启动其中一个生成器,例如 Thumbnailer。或者,也许我的下载不完整。
    无论哪种方式,我对任何一个库都不够精通,不知道从哪里开始。
    感谢您对此的任何帮助。

    最佳答案

    首先运行“ImageMagick-6.7.6-1-Q16-windows-dll.exe”安装程序以安装 imagemagick 库。然后确保您的环境路径包含已安装二进制文件的位置(“convert.exe”、“mogrify.exe”等)

    关于java - ImageMagick/IM4J FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213249/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com