繁体   English   中英

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

[英]How to improve image quality using im4java?

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

我正在使用的代码如下所示:

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。 提前致谢...

这是我正在使用的代码,它就像魅力

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM