ImageMagick安装使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35092224/article/details/83620069

下载ImageMagick   

http://www.imagemagick.org/script/download.php  找到自己对应的系统版本下载并安装


import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.log4j.Logger;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class ImageUtil {
    private static final Logger logger = Logger.getLogger(ImageUtil.class);

    public static final int IMG_H = 800; // 图片像素高, 默认800
    public static final int IMG_W = 800; // 图片像素宽,默认800
    public static final float PER = 1; // 压缩质量,默认1(范围0-1)

    /**
     * 扩大或压缩图片,留白处理 默认到800x800 压缩质量默认为1
     *
     * @param inputFile
     * @param outputFile
     */
    public void compressPic(File inputFile, File outputFile) throws Exception {
        IMOperation op = new IMOperation();
        op.addImage(inputFile.getPath());
        op.addRawArgs("-resize", IMG_W + "x" + IMG_H);
        op.addRawArgs("-background", "white");
        op.addRawArgs("-gravity", "center");
        op.addRawArgs("-extent",  IMG_W + "x" + IMG_H + "!");// + "!"
        op.addImage(outputFile.getPath());
        ConvertCmd convert = new ConvertCmd();
        //linux下不要设置此值,不然会报错 windows 填写ImageMagick安装目录
        convert.setSearchPath("C:\\Program Files (x86)\\ImageMagick-6.3.9-Q16");
        convert.run(op);
        LogEnum.SERVICE.error("--compressPic上传完成!");
    }

    public void compressPicError(File inputFile, File outputFile) throws Exception{
        IMOperation op = new IMOperation();
        op.addImage(inputFile.getPath());
        op.addRawArgs("-resize", IMG_W + "x" + IMG_H);
        op.addRawArgs("-background", "white");
        op.addRawArgs("-gravity", "center");
        op.addRawArgs("-extent",  IMG_W + "x" + IMG_H + "!");// + "!"
        op.addImage(outputFile.getPath());
        ConvertCmd convert = new ConvertCmd();
        // linux下不要设置此值,不然会报错
//        convert.setSearchPath(imageMagickPath);
        convert.run(op);
        LogEnum.SERVICE.error("compressPicError上传完成!");
    }



    /**
     * 扩大或压缩图片,留白处理 默认到800x800 压缩质量默认为1
     *
     * @param inputFile
     * @param outputFile
     */
    public void compressPic(File inputFile, File outputFile,int IMG_H,int IMG_W) throws Exception {
        IMOperation op = new IMOperation();
        op.addImage(inputFile.getPath());
        op.addRawArgs("-resize", IMG_W + "x" + IMG_H);
        op.addRawArgs("-background", "white");
        op.addRawArgs("-gravity", "center");
        op.addRawArgs("-extent",  IMG_W + "x" + IMG_H + "!");// + "!"
        op.addImage(outputFile.getPath());
        ConvertCmd convert = new ConvertCmd();
        // linux下不要设置此值,不然会报错
//        convert.setSearchPath(imageMagickPath);
        convert.run(op);
        LogEnum.SERVICE.error("compressPic2上传完成!");
    }


    public static void main(String[] args) {
        ImageUtil imageUtil = new ImageUtil();
        try {
            String inStr = "D:\\1020_F6BFF17C01D25EBCB39E48A6FEF2726D_20170213212252541\\e1.jpg";
            String outStr = "D:\\1020_F6BFF17C01D25EBCB39E48A6FEF2726D_20170213212252541\\1.jpg";
            imageUtil.compressPic(new File(inStr),new File(outStr));

           /* Thumbnails.of(inStr).size(800,800).keepAspectRatio(false).toFile(outStr);*/
        } catch (Exception e) {

        }
    }
}

ImageMagick常用指令详解

-quality  : 图片质量,jpg默认99,png默认75

-resize:

100x100      高度和宽度比例保留最高值,高比不变

100x100^     高度和宽度比例保留最低值,宽高比不变

100x100!      宽度和高度强制转换,忽视宽高比

100x100>     更改长宽,当图片长或宽超过规定的尺寸

100x100<     更改长宽 只有当图片长宽都超过规定的尺寸

100x100^>   更改长宽,当图片长或宽超过规定的尺寸。高度和宽度比例保留最低值

100x100^<   更改长宽,只有当图片长宽都超过规定的尺寸。高度和宽度比例保留最低值

100             按指定的宽度缩放,保持宽高比例

 x100          按指定高度缩放,保持宽高比

-gravity : NorthWest, North, NorthEast, West, Center, East,  SouthWest, South, SouthEast截取用的定位指令,定位截取区域在图片中的方位

-crop : 200x200+0+0 截取用的截取指令 ,在用定位指令后,按后两位的偏移值偏移截取范围左上角的像素后,再按前两位的数值,从左上角开始截取相应大小的图片

+repage: 去掉图片裁减后的空白

-dissolve: 30 设定组合图片透明度dissolve示例

+/-profile *   :去掉/添加图片exif信息

展开阅读全文

redhat安装使用imagemagick 以及jmagick的问题

12-03

linux系统版本以及jdk版本:rn[code=text]rn[root@hosta imageprocess]# uname -arnLinux hosta.localdomain 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linuxrn[root@hosta imageprocess]# java -versionrnjava version "1.6.0_30"rnJava(TM) SE Runtime Environment (build 1.6.0_30-b12)rnJava HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)rn[root@hosta imageprocess]# rn[/code]rnrnimagemagick 以及jmagick安装正常,应用服务器为weblogic10.3rnjmagick用源码使用上述JDK进行编译后生成了jmagick-6.4.0.jar/libJMagick-6.4.0.so/libJMagick.so三个文件,其中jmagick-6.4.0.jar放到项目中使用了,在windows下运行正常,在linux下运行,报错java.lang.UnsatisfiedLinkError: no JMagick in java.library.path.rn于是打出java.library.path如下:rn/usr/java/jdk1.6.0_30/jre/lib/amd64/server:/usr/java/jdk1.6.0_30/jre/lib/amd64:/usr/java/jdk1.6.0_30/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/librnrn将libJMagick.so文件拷贝置/usr/lib下,重新启动工程,使用该功能时则报错如下:rnjava.lang.UnsatisfiedLinkError: /usr/lib/libJMagick.so: /usr/lib/libJMagick.so: wrong ELF class: ELFCLASS64 (Possible cause: architecture word width mismatch) rnrn目前来看,系统是64位的,libJMagick.so也是使用源码用64位的JDK编译出来的,请问该如何解决?rnrn另:单独写了个java文件测试,使用javac编译然后java命令运行,则运行正常rn[code=java]rnimport magick.ImageInfo;rnimport magick.MagickApiException;rnimport magick.MagickException;rnimport magick.MagickImage;rnpublic class ProcessImg rn public static void resize1(String picFrom, String picTo) rn try rn ImageInfo info = new ImageInfo(picFrom);rn MagickImage image = new MagickImage(new ImageInfo(picFrom));rn MagickImage scaled = image.scaleImage(100, 86);// 小图片文件的大小.rn scaled.setFileName(picTo);rn scaled.writeImage(info);rnrn catch (MagickApiException ex) rnrn catch (MagickException ex) rnrn rn rn rn public static void main(String[] arg) rn System.setProperty("jmagick.systemclassloader", "no");rn ProcessImg.resize1("/home/app/imageprocess/pic/a.jpg", "/home/app/imageprocess/pic/b.jpg");rn rnrn[/code] 论坛

linux安装使用imagemagick 以及jmagick的问题

12-03

linux系统版本以及jdk版本:rnrnPlain Text codern??12345678 [root@hosta imageprocess]# uname -a Linux hosta.localdomain 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux [root@hosta imageprocess]# java -version java version "1.6.0_30" Java(TM) SE Runtime Environment (build 1.6.0_30-b12) Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode) [root@hosta imageprocess]# rnrnrnimagemagick 以及jmagick安装正常,应用服务器为weblogic10.3rnjmagick用源码使用上述JDK进行编译后生成了jmagick-6.4.0.jar/libJMagick-6.4.0.so/libJMagick.so三个文件,其中jmagick-6.4.0.jar放到项目中使用了,在windows下运行正常,在linux下运行,报错java.lang.UnsatisfiedLinkError: no JMagick in java.library.path.rn于是打出java.library.path如下:rn/usr/java/jdk1.6.0_30/jre/lib/amd64/server:/usr/java/jdk1.6.0_30/jre/lib/amd64:/usr/java/jdk1.6.0_30/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/librnrn将libJMagick.so文件拷贝置/usr/lib下,重新启动工程,使用该功能时则报错如下:rnjava.lang.UnsatisfiedLinkError: /usr/lib/libJMagick.so: /usr/lib/libJMagick.so: wrong ELF class: ELFCLASS64 (Possible cause: architecture word width mismatch) rnrn目前来看,系统是64位的,libJMagick.so也是使用源码用64位的JDK编译出来的,请问该如何解决?rnrn另:单独写了个java文件测试,使用javac编译然后java命令运行,则运行正常rn[code=java]rnimport magick.MagickException;rnimport magick.MagickImage;rnpublic class ProcessImg rn rn public static void resize1(String picFrom, String picTo) rn try rn ImageInfo info = new ImageInfo(picFrom);rn MagickImage image = new MagickImage(new ImageInfo(picFrom));rn MagickImage scaled = image.scaleImage(100, 86);// 小图片文件的大小.rn scaled.setFileName(picTo);rn scaled.writeImage(info);rnrn catch (MagickApiException ex) rnrn catch (MagickException ex) rnrn rn rnrn public static void main(String[] arg) rn System.setProperty("jmagick.systemclassloader", "no");rn ProcessImg.resize1("/home/app/imageprocess/pic/a.jpg", "/home/app/imageprocess/pic/b.jpg");rn rn rnrnrn[/code] 论坛

没有更多推荐了,返回首页