您可以尝试im4java,它也像JMagick一样使用ImageMagick。但它不是作为原生库的包装器,而是使用命令行与ImageMagick进行通信。
ImageMagick具有-auto-orient
自动将图像转换为“正确”方向的操作。有关详细信息,请查看文档。
优点
- 非常快,特别是如果ImageMagick是用libjpeg-turbo等优化库编译的(例如在Debian发行版中的标准)
- 支持比Debian等发行版中包含的JMagick版本更多的ImageMagick操作(截至撰写时)。
缺点
- 使用命令行界面与ImageMagick进行通信,由于安全限制,这可能是不允许的。
Maven 依赖
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>
示例 1(带文件)
// prepare operations
final int width = 2000;
final double quality = 85.0;
final IMOperation op = new IMOperation();
op.addImage("/path/to/input/image.jpg"); // input
op.autoOrient();
op.resize(width);
op.quality(quality);
op.addImage("/path/to/output/image.jpg"); // output (rotated image @ 85% quality)
// create and execute command
final ConvertCmd convertCmd = new ConvertCmd();
convertCmd.run(op);
示例 2(带有输入/输出流)
// prepare operations
final String outputFormat = "jpeg";
final int width = 2000;
final double quality = 85.0;
final IMOperation op = new IMOperation();
op.addImage("-"); // input: stdin
op.autoOrient();
op.resize(width);
op.quality(quality);
op.addImage(outputFormat + ":-"); // output: stdout
// create and execute command
final ConvertCmd convertCmd = new ConvertCmd();
final Pipe inPipe = new Pipe(inputStreamWithOriginalImage,
null); // no output stream, using stdin
convertCmd.setInputProvider(inPipe);
final Pipe outPipe = new Pipe(null, // no input stream, using stdout
outputStreamForConvertedImage);
convertCmd.setOutputConsumer(outPipe);
convertCmd.run(op);
示例 3(图像信息)
final boolean baseInfoOnly = true;
final Info info = new Info("/path/to/input/image.jpg",
baseInfoOnly);
System.out.println("Geometry: " + info.getImageGeometry());
表现
您还可以查看此问题以进行性能优化(但请注意,有一个-colorspace rgb
和一个不必要的,因为不支持 JPEG 图像,-coalesce
这两个操作都增加了处理时间)。
文档
在这里,您可以找到包含更多示例的开发人员指南。