Search in sources :

Example 1 with IM4JavaException

use of org.im4java.core.IM4JavaException in project quick-media by liuyueyi.

the class ImgBaseOperate method operate.

/**
 * 执行图片的复合操作
 *
 * @param operates
 * @param sourceFilename 原始图片名
 * @param outputFilename 生成图片名
 * @return
 * @throws ImgOperateException
 */
public static boolean operate(List<ImgWrapper.Builder.Operate> operates, String sourceFilename, String outputFilename) throws ImgOperateException {
    try {
        IMOperation op = new IMOperation();
        boolean operateTag = false;
        String waterFilename = null;
        for (ImgWrapper.Builder.Operate operate : operates) {
            if (!operate.valid()) {
                continue;
            }
            if (operate.getOperateType() == ImgWrapper.Builder.OperateType.CROP) {
                op.crop(operate.getWidth(), operate.getHeight(), operate.getX(), operate.getY());
                // if (operate.getRadio() != null && Math.abs(operate.getRadio() - 1.0) > 0.005) {
                // // 需要对图片进行缩放
                // op.resize((int) Math.ceil(operate.getWidth() * operate.getRadio()));
                // }
                operateTag = true;
            } else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.ROTATE) {
                // fixme 180度旋转后裁图,会出现bug, 先这么兼容
                double rotate = operate.getRotate();
                if (Math.abs((rotate % 360) - 180) <= 0.005) {
                    rotate += 0.01;
                }
                op.rotate(rotate);
                operateTag = true;
            } else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.SCALE) {
                if (operate.getRadio() == null) {
                    if (operate.isForceScale()) {
                        // 强制根据给定的参数进行压缩时
                        StringBuilder builder = new StringBuilder();
                        builder.append("!").append(operate.getWidth() == null ? "" : operate.getWidth()).append("x");
                        builder.append(operate.getHeight() == null ? "" : operate.getHeight());
                        op.addRawArgs("-resize", builder.toString());
                    } else {
                        op.resize(operate.getWidth(), operate.getHeight());
                    }
                } else if (Math.abs(operate.getRadio() - 1) > 0.005) {
                    // 对图片进行比例缩放
                    op.addRawArgs("-resize", "%" + (operate.getRadio() * 100));
                }
                if (operate.getQuality() != null && operate.getQuality() > 0) {
                    op.quality(operate.getQuality().doubleValue());
                }
                operateTag = true;
            } else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.FLIP) {
                op.flip();
                operateTag = true;
            } else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.FLOP) {
                op.flop();
                operateTag = true;
            } else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.WATER && waterFilename == null) {
                // 当前只支持添加一次水印
                op.geometry(operate.getWidth(), operate.getHeight(), operate.getX(), operate.getY()).composite();
                waterFilename = operate.getWaterFilename();
                operateTag = true;
            } else if (operate.getOperateType() == ImgWrapper.Builder.OperateType.BOARD) {
                op.border(operate.getWidth(), operate.getHeight()).bordercolor(operate.getColor());
                operateTag = true;
            }
        }
        if (!operateTag) {
            throw new ImgOperateException("operate illegal! operates: " + operates);
        }
        op.addImage(sourceFilename);
        if (waterFilename != null) {
            op.addImage(waterFilename);
        }
        op.addImage(outputFilename);
        /**
         * 传true到构造函数中,则表示使用GraphicMagic, 裁图时,图片大小会变
         */
        ConvertCmd convert = new ConvertCmd();
        convert.run(op);
    } catch (IOException e) {
        log.error("file read error!, e: {}", e);
        return false;
    } catch (InterruptedException e) {
        log.error("interrupt exception! e: {}", e);
        return false;
    } catch (IM4JavaException e) {
        log.error("im4java exception! e: {}", e);
        return false;
    }
    return true;
}
Also used : IMOperation(org.im4java.core.IMOperation) IOException(java.io.IOException) ConvertCmd(org.im4java.core.ConvertCmd) ImgOperateException(com.github.hui.quick.plugin.imagic.exception.ImgOperateException) IM4JavaException(org.im4java.core.IM4JavaException)

Example 2 with IM4JavaException

use of org.im4java.core.IM4JavaException in project selenium_java by sergueik.

the class VisualTest method resizeImagesWithImageMagick.

public void resizeImagesWithImageMagick(String... pImageNames) throws Exception {
    ConvertCmd cmd = new ConvertCmd();
    cmd.setSearchPath(imageMagickPath);
    IMOperation imOperation = new IMOperation();
    imOperation.addImage();
    imOperation.resize(200, 150);
    imOperation.addImage();
    for (String srcImage : pImageNames) {
        String dstImage = srcImage.substring(0, srcImage.lastIndexOf('.') - 1) + "_small.jpg";
        try {
            System.err.println(String.format("Resized image: '%s'", dstImage));
            cmd.run(imOperation, srcImage, dstImage);
        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
            throw ex;
        } catch (IM4JavaException ex) {
            System.err.println("Exception (ignored): " + ex.getClass());
            ex.printStackTrace();
        }
    }
}
Also used : IMOperation(org.im4java.core.IMOperation) IOException(java.io.IOException) ConvertCmd(org.im4java.core.ConvertCmd) IM4JavaException(org.im4java.core.IM4JavaException)

Aggregations

IOException (java.io.IOException)2 ConvertCmd (org.im4java.core.ConvertCmd)2 IM4JavaException (org.im4java.core.IM4JavaException)2 IMOperation (org.im4java.core.IMOperation)2 ImgOperateException (com.github.hui.quick.plugin.imagic.exception.ImgOperateException)1