使用方法:首先要安装ImageMagick这个工具,安装好这个工具后,再下载im4java包放到项目lib目录里就行了。
package com.stu.util; import java.io.IOException;
import java.util.ArrayList; import org.im4java.core.CompositeCmd;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.core.IdentifyCmd;
import org.im4java.process.ArrayListOutputConsumer; public class ImagesUtil {
/**
* 根据坐标裁剪图片
*
* @param srcPath 要裁剪图片的路径
* @param newPath 裁剪图片后的路径
* @param x 起始横坐标
* @param y 起始纵坐标
* @param x1 结束横坐标
* @param y1 结束纵坐标
*/
public static void cutImage(String srcPath, String newPath, int x, int y, int x1,
int y1) throws Exception {
int width = x1 - x;
int height = y1 - y;
IMOperation op = new IMOperation();
op.addImage(srcPath);
/** width:裁剪的宽度 * height:裁剪的高度 * x:裁剪的横坐标 * y:裁剪纵坐标 */
op.crop(width, height, x, y);
op.addImage(newPath);
ConvertCmd convert = new ConvertCmd();
convert.run(op);
}
/**
* 根据尺寸缩放图片
* @param width 缩放后的图片宽度
* @param height 缩放后的图片高度
* @param srcPath 源图片路径
* @param newPath 缩放后图片的路径
*/
public static void zoomImage(Integer width, Integer height, String srcPath, String newPath) throws Exception {
IMOperation op = new IMOperation();
op.addImage(srcPath);
if(width == null){//根据高度缩放图片
op.resize(null, height);
}else if(height == null){//根据宽度缩放图片
op.resize(width, null);
}else {
op.resize(width, height);
}
op.addImage(newPath);
ConvertCmd convert = new ConvertCmd();
convert.run(op);
} /**
* 给图片加水印
* @param srcPath 源图片路径
*/
public static void addImgText(String srcPath,String content) throws Exception {
IMOperation op = new IMOperation();
op.font("微软雅黑");
op.gravity("southeast");
op.pointsize(18).fill("#BCBFC8").draw("text 0,0 "+content); //("x1 x2 x3 x4") x1 格式,x2 x轴距离 x3 y轴距离 x4名称
op.addImage();
op.addImage();
ConvertCmd convert = new ConvertCmd();
try {
convert.run(op,srcPath,srcPath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 图片水印
*
* @param srcImagePath 源图片
* @param waterImagePath 水印
* @param destImagePath 生成图片
* @param gravity 图片位置
* @param dissolve 水印透明度
*/
public static void waterMark(String waterImagePath, String srcImagePath, String destImagePath, String gravity, int dissolve) {
IMOperation op = new IMOperation();
op.gravity(gravity);
op.dissolve(dissolve);
op.addImage(waterImagePath);
op.addImage(srcImagePath);
op.addImage(destImagePath);
CompositeCmd cmd = new CompositeCmd();
try {
cmd.run(op);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IM4JavaException e) {
e.printStackTrace();
}
}
/**
* 图片旋转
*
* @param srcImagePath
* @param destImagePath
* @param angle
*/
public static void rotate(String srcImagePath, String destImagePath, double angle) {
try {
IMOperation op = new IMOperation();
op.rotate(angle);
op.addImage(srcImagePath);
op.addImage(destImagePath);
ConvertCmd cmd = new ConvertCmd();
cmd.run(op);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 图片信息
*
* @param imagePath
* @return
*/
public static String showImageInfo(String imagePath) {
String line = null;
try {
IMOperation op = new IMOperation();
op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");
op.addImage(1);
IdentifyCmd identifyCmd = new IdentifyCmd(true);
ArrayListOutputConsumer output = new ArrayListOutputConsumer();
identifyCmd.setOutputConsumer(output);
identifyCmd.run(op, imagePath);
ArrayList<String> cmdOutput = output.getOutput();
assert cmdOutput.size() == 1;
line = cmdOutput.get(0); } catch (Exception e) {
e.printStackTrace();
}
return line;
}
/**
* 图片合成
* @param args
* @param maxWidth
* @param maxHeight
* @param newpath
* @param mrg
* @param type 1:横,2:竖
*/
public static void montage(String[] args,Integer maxWidth,Integer maxHeight,String newpath,Integer mrg,String type) {
IMOperation op = new IMOperation();
ConvertCmd cmd = new ConvertCmd();
String thumb_size = maxWidth+"x"+maxHeight+"^";
String extent = maxWidth+"x"+maxHeight;
if("1".equals(type)){
op.addRawArgs("+append");
}else if("2".equals(type)){
op.addRawArgs("-append");
} op.addRawArgs("-thumbnail",thumb_size);
op.addRawArgs("-gravity","center");
op.addRawArgs("-extent",extent); Integer border_w = maxWidth / 40;
op.addRawArgs("-border",border_w+"x"+border_w);
op.addRawArgs("-bordercolor","#ccc"); op.addRawArgs("-border",1+"x"+1);
op.addRawArgs("-bordercolor","#fff"); for(String img : args){
op.addImage(img);
}
if("1".equals(type)){
Integer whole_width = ((mrg / 2) +1 + border_w + maxWidth + border_w + (mrg / 2) +1)*args.length - mrg;
Integer whole_height = maxHeight + border_w + 1;
op.addRawArgs("-extent",whole_width + "x" +whole_height);
}else if("2".equals(type)){
Integer whole_width = maxWidth + border_w + 1;
Integer whole_height = ((mrg / 2) +1 + border_w + maxHeight + border_w + (mrg / 2) +1)*args.length - mrg;
op.addRawArgs("-extent",whole_width + "x" +whole_height);
}
op.addImage(newpath);
try {
cmd.run(op);
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) throws Exception{
//addImgText("e://a2.jpg");
//zoomImage(300, 150, "e://a.jpg", "e://a1.jpg");
//zoomImage(300, 150, "e://b.jpg", "e://b1.jpg");
//zoomImage(300, 150, "e://c.jpg", "e://c1.jpg");
//zoomImage(300, 150, "e://d.jpg", "e://d1.jpg");
//zoomImage(300, 150, "e://e.jpg", "e://e1.jpg");
// zoomImage(300, 150, "e://f.jpg", "e://f1.jpg");
//waterMark("e://cc.jpg", "e://aa.jpg", "e://bb.jpg", "southeast", 30);
//rotate("e://aa.jpg", "e://ee.jpg", 90);
//System.out.println(showImageInfo("e://aa.jpg"));
String[] files = new String[5];
files[0] = "e://a1.jpg";
files[1] = "e://b1.jpg";
files[2] = "e://c1.jpg";
files[3] = "e://d1.jpg";
files[4] = "e://e1.jpg";
//montage(files, 280, 200, "e://liboy1.jpg", 0,"2");
//cropImage("e://a.jpg", "e://liboy22.jpg", 1024, 727, 500, 350);
cutImage("e://a.jpg", "e://liboy222.jpg", 5, 10, 100, 120);
}
} //如果是在windows下运行,则需要配置ImageMagick的路径(),如果找不到convert的话,把目录下的magick.exe拷贝一份命名convert.exe

ConvertCmd convert = new ConvertCmd();
convert.setSearchPath("E:\\ImageMagick-7.0.5-Q16");
convert.run(op);

 

im4java包处理图片的更多相关文章

  1. GraphicsMagick+Im4Java在windows和linux下的配置

    GraphicsMagick介绍及安装 Im4Java包为: im4java-1.2.0.jar 直接在lib下引用即可 GraphicsMagick的安装如下: windows下: 安装:Graph ...

  2. linux的零碎使用

    一.Linux(rehat.centos.ubuntu...)基础知识 上午: putty软件连接linux服务器: [root @ foundation2   ~ ]         # 用户名   ...

  3. 转:ImageMagick +Jmagick安装

    原文来自于: 目录 一.ImageMagick介绍 二.安装支持库 三.在Linux上用源码编译安装ImageMagick与Jmagick 四.在Linux上使用yum安装ImageMagick与Jm ...

  4. 在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)

    安装过程(如图所示) .Exceptionin thread "main" java.lang.UnsatisfiedLinkError:C:\WINDOWS\system32\j ...

  5. linux的使用以及linux服务器应用的部署

    一.Linux(rehat.centos.ubuntu...)基础知识 上午: putty软件连接linux服务器: [root @ foundation2   ~ ]         # 用户名   ...

  6. 使用tensorflow深度学习识别验证码

    除了传统的PIL包处理图片,然后用pytessert+OCR识别意外,还可以使用tessorflow训练来识别验证码. 此篇代码大部分是转载的,只改了很少地方. 代码是运行在linux环境,tesso ...

  7. " ModuleNotFoundError: No module named 'tkinter' "的解决方法

    踩坑场景 在使用pillow这个包处理图片的时候,运行程序,报错ModuleNotFoundError: No module named 'tkinter',遇到ModuleNotFoundError ...

  8. Eclipse中处理图片引包问题

    在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEG ...

  9. Im4java 操作 ImageMagick 处理图片

    背景 之前用的是JMagick,各种限制各种坑,直到使用了Im4java,真是相当的好用啊. 项目描述 ImageMagic的安装可参考:图片处理软件 ImageMagick 的安装和使用 Im4ja ...

随机推荐

  1. Apache服务器配置

    之前做代码一直按照传统化的方法部署别人的网站,但是一直不成功,尝试了很多次最后才发现时虚拟主机的问题 使用apache默认为127.0.0.1和网站的配置发生冲突. 因此在apache的conf文件夹 ...

  2. 【深度学习系列】用PaddlePaddle和Tensorflow实现AlexNet

    上周我们用PaddlePaddle和Tensorflow实现了图像分类,分别用自己手写的一个简单的CNN网络simple_cnn和LeNet-5的CNN网络识别cifar-10数据集.在上周的实验表现 ...

  3. Code Kata:大整数比较大小&大整数四则运算---加减法 javascript实现

    大整数的四则运算已经是老生常谈的问题了.很多的库也已经包含了各种各样的解决方案. 作为练习,我们从最简单的加减法开始. 加减法的核心思路是用倒序数组来模拟一个大数,然后将两个大数的利用竖式进行运算. ...

  4. hiho-1015- KMP算法

    #1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  5. Postgres是如何管理空值的

    创建表test,y字段插入null. test=# create table test(x bigint,y bigint,z text); CREATE TABLE test=# insert in ...

  6. PHP开发b2c商城价格

    电商的快速发展不断地挤压传统企业的生存空间,渠道越来越窄,所以现在很多企业开始往线上发展,搭建自己的B2C商城,直接面向消费者进行销售.那开发b2c商城价格怎么样?很多企业都是比较关心到商城价格这个问 ...

  7. [Phonegap+Sencha Touch] 移动开发24 打包wp8.1的App,执行时输入框聚焦弹出软键盘之后,界面上移而不恢复原位的解决的方法

    这个现象仅仅出如今phonegap打包sencha touch的wp8.1程序会出现(仅wp8.1,wp8正常),其他js框架我測试了几个(app framework, jquery mobile), ...

  8. XMPP学习及使用1

    XMPP 简单介绍 本小节将简要介绍 XMPP,它的起源.以及为何它是一个适合实时 web 通信的协议.您将检查 XMPP 通信设置的组件,并查看展示这些组件怎样使用的演示样例. Web 标准和 XM ...

  9. USACO Section 2.1 Healthy Holsteins

    /* ID: lucien23 PROG: holstein LANG: C++ */ #include <iostream> #include <fstream> #incl ...

  10. Office 365 机器人(Bot)开发入门

    作者:陈希章 发表于 2017年7月29日 前言 作为人工智能技术的一个主要的表现形式,这些年机器人(bot)的应用越来越广泛.不管是有实物的,还是纯软件的,现在的机器人技术应该说已经走入寻常百姓家了 ...