首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像处理- Java

图像处理- Java
EN

Stack Overflow用户
提问于 2016-05-03 18:22:55
回答 1查看 829关注 0票数 0

我最近开发了一个运行在Tomcat7上的Java,它应该接受来自不同格式(PNG、JPG、BMP)的图片的发布。并执行下列任务:

  • 如果大于2000 if,则按比例缩小
  • 缓存其他维度(如果允许)
  • 运行哈希算法来识别相似的图像。
  • 将它们全部存储并缓存为JPG

作为最快的解决方案,我依赖于ImageIO,它在我遇到更多“新”格式之前产生了一个不错的结果。有两个主要问题我无法找到有效的解决办法:

  • 渐进JPEG
  • 包含EXIF元数据的JPEG

我评估了不同的解决方案,但它们似乎都解决不了这两个问题(我会列出最好的两个):

  • Apache成像(读取EXIF,不能读写JPEG它是慢)
  • JMagick (接受JPEG和渐进JPEG,不关心EXIF)

你们中有人能够实现一种适用于这两种格式的解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2018-06-15 11:00:29

您可以尝试im4java,它还在引擎盖下使用ImageMagick,比如JMagick。但是,它不是本机库的包装器,而是使用命令行与ImageMagick通信。

ImageMagick有一个操作-auto-orient,它自动将图像转换为“正确”的方向。有关详细信息,请查看文档

优势

  • 非常快,特别是如果ImageMagick是用libjpeg-turbo这样的优化库编译的(例如,在Debian发行版中)
  • 支持比发行版(如Debian )中包含的ImageMagick版本更多的JMagick操作(截至编写时)。

缺点

  • 使用命令行接口与ImageMagick通信,由于安全限制,这可能是不允许的。

Maven依赖

代码语言:javascript
复制
    <dependency>
        <groupId>org.im4java</groupId>
        <artifactId>im4java</artifactId>
        <version>1.4.0</version>
    </dependency>

示例1(带有文件)

代码语言:javascript
复制
        // 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(具有输入/输出流)

代码语言:javascript
复制
        // 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(图像信息)

代码语言:javascript
复制
        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操作,这都增加了处理时间)。

文档

这里您可以通过更多的示例找到开发人员指南。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37011617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档