我正在使用im4java来转换图像。下面的程序由于堆栈跟踪而崩溃:Caused by: org.im4java.core.CommandException: convert: insufficient image data in file /tmp/magick-254901G7YJ9qaMQv5' @ error/jpeg.c/ReadJPEGImage/1154.
看起来它正在生成一个临时文件。直接在ImageMagick命令行上运行相同的命令会得到正确的结果。
我正在运行ImageMagick-6.9.10-61和im4java 1.4.0
我想知道有没有人有什么见解。
IMOperation op = new IMOperation();
op.addImage("-");
op.addImage("jpg:-");
op.quality(0.85);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
// image is of type MultipartFile
Pipe pipeIn = new Pipe(image.getInputStream(), null);
Pipe pipeOut = new Pipe(null, outputStream);
convertCmd.setInputProvider(pipeIn);
convertCmd.setOutputConsumer(pipeOut);
convertCmd.run(op);
return outputStream.toByteArray();
} catch (Exception e) {
//
}发布于 2019-08-22 15:39:17
我看了一些旧的代码,我们做了一些类似的事情,我意识到你的问题是使用了2个管道,其中一个管道的目标为空,另一个管道的源为空。这可能会导致使用临时文件。
而不是这样做,您需要使用单个管道:
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Pipe pipe = new Pipe(image.getInputStream(), outputStream);
convertCmd.setInputProvider( pipe );
convertCmd.setOutputConsumer( pipe );
return outputStream.toByteArray();
}您可以在进程管道的两端使用相同的
-object。
https://stackoverflow.com/questions/57590610
复制相似问题