我最近开发了一个运行在Tomcat7上的Java,它应该接受来自不同格式(PNG、JPG、BMP)的图片的发布。并执行下列任务:
作为最快的解决方案,我依赖于ImageIO,它在我遇到更多“新”格式之前产生了一个不错的结果。有两个主要问题我无法找到有效的解决办法:
我评估了不同的解决方案,但它们似乎都解决不了这两个问题(我会列出最好的两个):
你们中有人能够实现一种适用于这两种格式的解决方案吗?
发布于 2018-06-15 11:00:29
您可以尝试im4java,它还在引擎盖下使用ImageMagick,比如JMagick。但是,它不是本机库的包装器,而是使用命令行与ImageMagick通信。
ImageMagick有一个操作-auto-orient,它自动将图像转换为“正确”的方向。有关详细信息,请查看文档。
优势
缺点
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和一个不必要的,因为不支持-colorspace rgb图像,-coalesce操作,这都增加了处理时间)。
文档
这里您可以通过更多的示例找到开发人员指南。
https://stackoverflow.com/questions/37011617
复制相似问题