首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用im4java提高图像质量?

如何使用im4java提高图像质量?
EN

Stack Overflow用户
提问于 2013-02-14 03:01:21
回答 1查看 2.9K关注 0票数 2

我在ImageMagick中使用im4java库进行图像处理操作。

我使用的代码如下所示:

代码语言:javascript
复制
ImageCommand imageCommand = new ConvertCmd();
imageCommand.setSearchPath(getImageMagickPath());
operation = new IMOperation();
operation.quality(100d);
operation.addImage();
operation.resize(getWidth());
operation.addImage();
imageCommand.run(operation, sourceImage, extention);

但是生成的图像质量很差。使用命令行做同样的事情,可以获得4倍的大小和更好的质量。使用im4java时是否需要设置其他设置?文件格式为jpg。先谢谢你...

EN

回答 1

Stack Overflow用户

发布于 2013-03-18 07:58:31

这是我正在使用的代码,它的工作方式很有魅力:

代码语言:javascript
复制
    private static void saveScaleImage(File image, String outputImagePath, int width, Integer height, float quality, boolean fill) throws Exception {
        Info info = new Info(image.getPath(), true);
        String imageFormat = info.getImageFormat();
        IMOperation op = new IMOperation();
        op.addImage(imagen.getPath());
        int imageWidth = info.getImageWidth(),
            imageHeight = info.getImageHeight();
        if (imageWidth> 0 && imageHeight > 0 && (imageFormat == null || !imageFormat.equalsIgnoreCase("TIFF"))) {
            //fill transparencies && extra space with white
            op.size(imageWidth, imageHeight);
            op.addRawArgs("xc:white");
            op.addImage(outputImagePath);
            // set up command
            CompositeCmd composite = new CompositeCmd();
            composite.run(op);
            op = new IMOperation();
            op.addImage(outputImagePath);
        }
        //add strip irrelevant info
        op.strip();
        //resize
        if (fill) { //crop image + fill
            op.resize(width, height, "^").gravity("center").extent(width, height);
        } else { //adjust
           op.resize(width, height);
        }
        boolean smaller = imageWidth > 0 && imageHeight > 0 && imageWidth < width && imageHeight < width;
        if (smaller) {
            op.addImage(outputImagePath);
            op.gravity("center");
        }
        if (imageFormat == null || imageFormat.equalsIgnoreCase("PNG")) {
            //jpeg teawks
            op.type("optimize").quality((double) quality).blur(0d, .16);
            //default to jpeg format
            op.addImage("jpeg:" + outputImagePath);
        } else {
            op.addImage(outputImagePath);
        }
        // set up command
        ConvertCmd convert = new ConvertCmd();
        //run command
        convert.run(op);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14860993

复制
相关文章

相似问题

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