java graphicsmagick_GraphicsMagick+im4java图片处理

苏乐童
2023-12-01

packageimg.GraphicsMagick;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.util.ArrayList;importjava.util.ResourceBundle;importorg.apache.commons.lang.SystemUtils;importorg.im4java.core.ConvertCmd;importorg.im4java.core.IM4JavaException;importorg.im4java.core.IMOperation;importorg.im4java.core.IdentifyCmd;importorg.im4java.process.ArrayListOutputConsumer;public classTest1 {public static String imageMagickPath = null;private static boolean is_windows = false;/**获取ImageMagick的路径,获取操作系统是否为WINDOWS*/

static{//通过ResourceBundle.getBundle()静态方法来获取,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。

ResourceBundle resource = ResourceBundle.getBundle("config"); //src文件夹下的配置文件直接写文件名

imageMagickPath = resource.getString("imageMagickPath");

is_windows=SystemUtils.IS_OS_WINDOWS;

}public static intgetSize(String imagePath) {int size = 0;

FileInputStream inputStream= null;try{

inputStream= newFileInputStream(imagePath);

size=inputStream.available();

inputStream.close();

inputStream= null;

}catch(FileNotFoundException e) {

size= 0;

System.out.println("文件未找到!");

}catch(IOException e) {

size= 0;

System.out.println("读取文件大小错误!");

}finally{//可能异常为关闭输入流,所以需要关闭输入流

if (null !=inputStream) {try{

inputStream.close();

}catch(IOException e) {

System.out.println("关闭文件读入流异常");

}

inputStream= null;

}

}returnsize;

}public static intgetWidth(String imagePath) {int line = 0;try{

IMOperation op= newIMOperation();

op.format("%w"); //设置获取宽度参数

op.addImage(1);

IdentifyCmd identifyCmd= new IdentifyCmd(true);

ArrayListOutputConsumer output= newArrayListOutputConsumer();

identifyCmd.setOutputConsumer(output);if(is_windows) {

identifyCmd.setSearchPath(imageMagickPath);

}

identifyCmd.run(op, imagePath);

ArrayList cmdOutput =output.getOutput();assert cmdOutput.size() == 1;

line= Integer.parseInt(cmdOutput.get(0));

}catch(Exception e) {

line= 0;

System.out.println("运行指令出错!"+e.toString());

}returnline;

}public static intgetHeight(String imagePath) {int line = 0;try{

IMOperation op= newIMOperation();

op.format("%h"); //设置获取高度参数

op.addImage(1);

IdentifyCmd identifyCmd= new IdentifyCmd(true);

ArrayListOutputConsumer output= newArrayListOutputConsumer();

identifyCmd.setOutputConsumer(output);if(is_windows) {

identifyCmd.setSearchPath(imageMagickPath);

}

identifyCmd.run(op, imagePath);

ArrayList cmdOutput =output.getOutput();assert cmdOutput.size() == 1;

line= Integer.parseInt(cmdOutput.get(0));

}catch(Exception e) {

line= 0;

System.out.println("运行指令出错!"+e.toString());

}returnline;

}public staticString getImageInfo(String imagePath) {

String line= null;try{

IMOperation op= newIMOperation();

op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");

op.addImage(1);

IdentifyCmd identifyCmd= new IdentifyCmd(true);

ArrayListOutputConsumer output= newArrayListOutputConsumer();

identifyCmd.setOutputConsumer(output);if(is_windows) {

identifyCmd.setSearchPath(imageMagickPath);

}

identifyCmd.run(op, imagePath);

ArrayList cmdOutput =output.getOutput();assert cmdOutput.size() == 1;

line= cmdOutput.get(0);

}catch(Exception e) {

e.printStackTrace();

}returnline;

}/*** 根据坐标裁剪图片

*@paramimagePath 源图片路径

*@paramnewPath 处理后图片路径

*@paramx 起始X坐标

*@paramy 起始Y坐标

*@paramwidth 裁剪宽度

*@paramheight 裁剪高度

*@return返回true说明裁剪成功,否则失败*/

public static boolean cutImage1(String imagePath, String newPath, int x, int y, int width, intheight) {boolean flag = false;try{

IMOperation op= newIMOperation();

op.addImage(imagePath);

op.crop(width, height, x, y);/**width:裁剪的宽度 * height:裁剪的高度 * x:开始裁剪的横坐标 * y:开始裁剪纵坐标*/op.addImage(newPath);

ConvertCmd convert= new ConvertCmd(true);if(is_windows) {

convert.setSearchPath(imageMagickPath);

}

convert.run(op);

flag= true;

}catch(IOException e) {

System.out.println("文件读取错误!");

flag= false;

}catch(InterruptedException e) {

flag= false;

}catch(IM4JavaException e) {

flag= false;

}returnflag;

}/*** 根据坐标裁剪图片

*@paramsrcPath 要裁剪图片的路径

*@paramnewPath 裁剪图片后的路径

*@paramx 起始横坐标

*@paramy 起始挫坐标

*@paramx1 结束横坐标

*@paramy1 结束挫坐标*/

public static void cutImage2(String srcPath, String newPath, int x, int y, int x1, inty1) {try{

IMOperation op= newIMOperation();int width = x1 - x; //裁剪的宽度

int height = y1 - y;//裁剪的高度

op.addImage(srcPath);

op.crop(width, height, x, y);

op.addImage(newPath);

ConvertCmd convert= new ConvertCmd(true);if(is_windows) {

convert.setSearchPath(imageMagickPath);

}

convert.run(op);

}catch(IOException e) {

e.printStackTrace();

}catch(InterruptedException e) {

e.printStackTrace();

}catch(IM4JavaException e) {

e.printStackTrace();

}

}/*** 根据尺寸等比例缩放图片 大边达到指定尺寸

* [参数height为null,按宽度缩放比例缩放;参数width为null,按高度缩放比例缩放]

*@paramimagePath 源图片路径

*@paramnewPath 处理后图片路径

*@paramwidth 缩放后的图片宽度

*@paramheight 缩放后的图片高度

*@return返回true说明缩放成功,否则失败*/

public static booleanzoomImage1(String imagePath, String newPath, Integer width, Integer height) {boolean flag = false;try{

IMOperation op= newIMOperation();

op.addImage(imagePath);if (width == null) {//根据高度缩放图片

op.resize(null, height);

}else if (height == null) {//根据宽度缩放图片

op.resize(width);

}else{

op.resize(width, height);

}

op.addImage(newPath);//IM4JAVA是同时支持GraphicsMagick和ImageMagick的,如果为true则使用GM,如果为false支持IM。

ConvertCmd convert = new ConvertCmd(true);//判断系统

String osName = System.getProperty("os.name").toLowerCase();if(osName.indexOf("win") >= 0) {

convert.setSearchPath(imageMagickPath);

}

convert.run(op);

flag= true;

}catch(IOException e) {

System.out.println("文件读取错误!");

flag= false;

}catch(InterruptedException e) {

flag= false;

}catch(IM4JavaException e) {

System.out.println(e.toString());

flag= false;

}returnflag;

}/*** 根据像素缩放图片

*@paramwidth 缩放后的图片宽度

*@paramheight 缩放后的图片高度

*@paramsrcPath 源图片路径

*@paramnewPath 缩放后图片的路径

*@paramtype 1大小 2比例*/

public static String zoomImage2(int width, int height, String srcPath, String newPath, int type, String quality) throwsException {

IMOperation op= newIMOperation();

op.addImage();

String raw= "";if (type == 1) { //按像素大小

raw = width + "x" + height + "^";

}else { //按像素百分比

raw = width + "%x" + height + "%";

}

ConvertCmd cmd= new ConvertCmd(true);

op.addRawArgs("-sample", raw);if ((quality != null && !quality.equals(""))) {

op.addRawArgs("-quality", quality);

}

op.addImage();

String osName= System.getProperty("os.name").toLowerCase();if (osName.indexOf("win") != -1) {

cmd.setSearchPath(imageMagickPath);

}try{

cmd.run(op, srcPath, newPath);

}catch(Exception e) {

e.printStackTrace();

}returnnewPath;

}/*** 图片旋转

*@paramimagePath 源图片路径

*@paramnewPath 处理后图片路径

*@paramdegree 旋转角度*/

public static boolean rotate(String imagePath, String newPath, doubledegree) {boolean flag = false;try{//1.将角度转换到0-360度之间

degree = degree % 360;if (degree <= 0) {

degree= 360 +degree;

}

IMOperation op= newIMOperation();

op.addImage(imagePath);

op.rotate(degree);

op.addImage(newPath);

ConvertCmd cmd= new ConvertCmd(true);if(is_windows) {

cmd.setSearchPath(imageMagickPath);

}

cmd.run(op);

flag= true;

}catch(Exception e) {

flag= false;

System.out.println("图片旋转失败!");

}returnflag;

}/*** 给图片加水印

*@paramsrcPath 源图片路径

*@paramdestPath 目标图片路径*/

public static void addImgText(String srcPath, String destPath) throwsException {try{

IMOperation op= newIMOperation();

op.font("Arial").gravity("southeast").pointsize(150).fill("#BCBFC8").draw("text 100,100 co188.com");

op.quality(85d);//压缩质量

op.addImage(srcPath);

op.addImage(destPath);

ConvertCmd cmd= new ConvertCmd(true);if(is_windows) {

cmd.setSearchPath(imageMagickPath);

}

cmd.run(op);

}catch(Exception e) {

e.printStackTrace();

}

}/*** 先等比例缩放,后居中切割图片

*@paramsrcPath 源图路径

*@paramdesPath 目标图保存路径

*@paramrectw 待切割在宽度

*@paramrecth 待切割在高度*/

public static void cropImageCenter(String srcPath, String desPath, int width, intheight) {try{

IMOperation op= newIMOperation();

op.addImage();

op.resize(width, height,'^').gravity("center").extent(width, height);//op.resize(width, height).background("gray").gravity("center").extent(width, height);

op.addImage();

ConvertCmd convert= new ConvertCmd(true);if(is_windows) {

convert.setSearchPath(imageMagickPath);

}

convert.run(op, srcPath, desPath);

}catch (IOException | InterruptedException |IM4JavaException e) {

e.printStackTrace();

}

}public static void main(String[] args) throwsException {

Long start=System.currentTimeMillis();//System.out.println("原图片宽度:" + getWidth("D:\\test\\map.jpg"));//System.out.println("原图片高度:" + getHeight("D://test//map.jpg"));//System.out.println("原图片信息:" + getImageInfo("D://test//map.jpg"));//cutImage2("D://test//map.jpg", "D://test//m.jpg", 10, 10, 200, 200);//rotate("D://test//map.jpg", "D://test//m.jpg", 10);//drawImg("D://test//map.jpg", "D://test//ma.jpg", 1500, 1500);//zoomImage1( "D://test//map.jpg", "D://test//mapppp.jpg",280, 200);//zoomImage2(280, 200 ,"D://test//map.jpg", "D://test//mapppppppappp.jpg",1,null);//addImgText("D://test//map1.jpg", "D://test//map111.jpg");

cropImageCenter("D://test//haidilao.jpg", "D://test//haidilao1112.jpg", 400, 400);

Long end=System.currentTimeMillis();

System.out.println("time:" + (end -start));

}

}

 类似资料: