Search in sources :

Example 1 with IMOperation

use of org.im4java.core.IMOperation 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 IMOperation

use of org.im4java.core.IMOperation in project entando-core by entando.

the class ImageResource method saveResizedImage.

/**
 * Redim images using im4Java
 * @param bean
 * @param dimension
 * @param mimeType
 * @param baseDiskFolder
 * @throws ApsSystemException
 */
private void saveResizedImage(ResourceDataBean bean, ImageResourceDimension dimension) throws ApsSystemException {
    if (dimension.getIdDim() == 0) {
        // salta l'elemento con id zero che non va ridimensionato
        return;
    }
    String imageName = this.getNewInstanceFileName(bean.getFileName(), dimension.getIdDim(), null);
    String subPath = super.getDiskSubFolder() + imageName;
    try {
        this.getStorageManager().deleteFile(subPath, this.isProtectedResource());
        ResourceInstance resizedInstance = new ResourceInstance();
        resizedInstance.setSize(dimension.getIdDim());
        resizedInstance.setFileName(imageName);
        resizedInstance.setMimeType(bean.getMimeType());
        String tempFilePath = System.getProperty("java.io.tmpdir") + File.separator + "temp_" + imageName;
        File tempFile = new File(tempFilePath);
        long realLength = tempFile.length() / 1000;
        resizedInstance.setFileLength(String.valueOf(realLength) + " Kb");
        this.addInstance(resizedInstance);
        // create command
        ConvertCmd convertCmd = new ConvertCmd();
        // Is it a windows system?
        if (this.isImageMagickWindows()) {
            // yes so configure the path where ImagicMagick is installed
            convertCmd.setSearchPath(this.getImageMagickPath());
        }
        // create the operation, add images and operators/options
        IMOperation imOper = new IMOperation();
        imOper.addImage();
        imOper.resize(dimension.getDimx(), dimension.getDimy());
        imOper.addImage();
        convertCmd.run(imOper, bean.getFile().getAbsolutePath(), tempFile.getAbsolutePath());
        this.getStorageManager().saveFile(subPath, this.isProtectedResource(), new FileInputStream(tempFile));
        tempFile.delete();
    } catch (Throwable t) {
        _logger.error("Error creating resource file instance '{}'", subPath, t);
        throw new ApsSystemException("Error creating resource file instance '" + subPath + "'", t);
    }
}
Also used : IMOperation(org.im4java.core.IMOperation) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ConvertCmd(org.im4java.core.ConvertCmd) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with IMOperation

use of org.im4java.core.IMOperation 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)

Example 4 with IMOperation

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

the class VisualTest method compareImagesWithImageMagick.

// ImageMagick Compare Method
public void compareImagesWithImageMagick(String expected, String actual, String difference) throws Exception {
    ProcessStarter.setGlobalSearchPath(imageMagickPath);
    CompareCmd compare = new CompareCmd();
    compare.setSearchPath(imageMagickPath);
    // fix java.lang.NullPointerExceptionTests
    // compare.setErrorConsumer(StandardStream.STDERR);
    IMOperation imOperation = new IMOperation();
    imOperation.fuzz(5.0);
    // The special "-metric" setting of 'AE' (short for "Absolute Error" count),
    // will report (to standard error),
    // a count of the actual number of pixels that were masked, at the current
    // fuzz factor.
    imOperation.metric("AE");
    // Add the expected image
    imOperation.addImage(expected);
    // Add the actual image
    imOperation.addImage(actual);
    // This stores the difference
    imOperation.addImage(difference);
    String script = "myscript";
    try {
        System.out.println("Comparison Started");
        compare.createScript(script, imOperation);
        System.out.println("Comparison Script written to " + script);
        compare.run(imOperation);
    } catch (CommandException ex) {
        // ignore
        System.err.println("Exception (ignored):" + ex.getClass());
        System.err.print(ex);
    } catch (Exception ex) {
        System.err.println("Comparison Failed!");
        System.err.print(ex);
        throw ex;
    }
    // Put the difference image to the global differences folder
    Files.copy(differenceImageFile, differenceFileForParent);
}
Also used : IMOperation(org.im4java.core.IMOperation) CommandException(org.im4java.core.CommandException) CommandException(org.im4java.core.CommandException) IM4JavaException(org.im4java.core.IM4JavaException) IOException(java.io.IOException) CompareCmd(org.im4java.core.CompareCmd)

Aggregations

IMOperation (org.im4java.core.IMOperation)4 IOException (java.io.IOException)3 ConvertCmd (org.im4java.core.ConvertCmd)3 IM4JavaException (org.im4java.core.IM4JavaException)3 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)1 ImgOperateException (com.github.hui.quick.plugin.imagic.exception.ImgOperateException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 CommandException (org.im4java.core.CommandException)1 CompareCmd (org.im4java.core.CompareCmd)1