org.im4java.core.ConvertCmd Java Examples

The following examples show how to use org.im4java.core.ConvertCmd. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MagickImgUtils.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩图片   ---暂时不使用
 *
 * @param orgImg            图片源地址 local or network url.
 * @param destImg           图片输入地址
 * @param destWidth         图片目标宽度
 * @param destHeight        图片目标高度
 * @param isDistortion      是否保留视觉比例,true则强行改变宽高尺寸匹配使用给定的宽和高
 * @param methodChoice      如果使用ImageMagick,设为false,使用GraphicsMagick,就设为true,默认为false
 * @param windowsMagickPath 若系统为windows则,必须指定GraphicsMagick的安装路径,例如:S:/Program Files (x86)/GraphicsMagick-1.3.21-Q8,其他系统为空
 * @return void 输出到目标地址
 */
@Deprecated
public static final void compressImg(String orgImg, String destImg, int destWidth, int destHeight, boolean isDistortion, boolean methodChoice, String windowsMagickPath) throws TSException {
    try {
        IMOperation imop = new IMOperation();  // or IMOperation GMOperation
        imop.addImage(orgImg);
        //图片压缩比,有效值范围是0.0-100.0,数值越大,缩略图越清晰
        imop.quality(75.0);
        //width 和height可以是原图的尺寸,也可以是按比例处理后的尺寸
        if (isDistortion) {
            imop.addRawArgs("-resize", destWidth + "x" + destHeight + "!");
        } else {
            imop.addRawArgs("-resize", destWidth + "x" + destHeight);
        }
        imop.addRawArgs("-gravity", "center");
        imop.addImage(destImg);
        // 如果使用ImageMagick,设为false,使用GraphicsMagick,就设为true,默认为false
        //环境变量ok
        //未安装装GraphicsMagick java.io.FileNotFoundException: gm
        //未安装了ImageMagick java.io.FileNotFoundException: convert
        ConvertCmd convert = new ConvertCmd(methodChoice);
        // linux下不要设置此值,不然会报错
        String os = System.getProperty("os.name").toLowerCase();
        if (os.indexOf("windows") >= 0) {
            String msg = "GraphicsMagick";
            if (!methodChoice) msg = "ImageMagick";
            if (StringUtils.isEmpty(windowsMagickPath)) {
                throw new TSException(TSEDictionary.SYSTEM_EXCEPTION.getCode(), "If the system for Windows, " + msg + "'s path cannot be empty.");
            }
            convert.setSearchPath(windowsMagickPath);
        }
        convert.run(imop);
    } catch (Exception e) {
        throw new TSException(TSEDictionary.UNDEFINED_FAIL.getCode(), "Image compress failure." + e.getMessage());
    }
}
 
Example #2
Source File: ImageUtils.java    From AppiumTestDistribution with GNU General Public License v3.0 5 votes vote down vote up
public void wrapDeviceFrames(String deviceFrame, String deviceScreenToBeFramed,
                             String framedDeviceScreen)
        throws InterruptedException, IOException, IM4JavaException {
    IMOperation op = new IMOperation();
    op.addImage(deviceFrame);
    op.addImage(deviceScreenToBeFramed);
    op.gravity("center");
    op.composite();
    op.opaque("none");
    op.addImage(framedDeviceScreen);
    ConvertCmd cmd = new ConvertCmd();
    cmd.run(op);
}
 
Example #3
Source File: ImageResource.java    From entando-components with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Resize images using im4Java
 */
private void saveResizedImage(ResourceDataBean bean, ImageResourceDimension dimension) throws ApsSystemException {
    if (dimension.getIdDim() == 0) {
        // skips element with id zero that shouldn't be resized
        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(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());
        }

        //svg files won't resize correctly via the image processor so save them directly
        if(bean.getMimeType().contains("svg")) {
            FileUtils.copyFile(bean.getFile(), tempFile);
            this.getStorageManager().saveFile(subPath, this.isProtectedResource(), new FileInputStream(tempFile));
        }else {
            // 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));
        }

        boolean deleted = tempFile.delete();

        if (!deleted) {
            logger.warn(FAILED_TO_DELETE_TEMP_FILE, tempFile);
        }
    } catch (Throwable t) {
        logger.error("Error creating resource file instance '{}'", subPath, t);
        throw new ApsSystemException("Error creating resource file instance '" + subPath + "'", t);
    }
}