2

我正在使用 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。提前致谢...

4

1 回答 1

0

这是我正在使用的代码,它就像魅力一样工作:

    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);
    }
于 2013-03-17T23:58:30.507 回答