im4java包处理图片
使用方法:首先要安装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包处理图片的更多相关文章
- GraphicsMagick+Im4Java在windows和linux下的配置
GraphicsMagick介绍及安装 Im4Java包为: im4java-1.2.0.jar 直接在lib下引用即可 GraphicsMagick的安装如下: windows下: 安装:Graph ...
- linux的零碎使用
一.Linux(rehat.centos.ubuntu...)基础知识 上午: putty软件连接linux服务器: [root @ foundation2 ~ ] # 用户名 ...
- 转:ImageMagick +Jmagick安装
原文来自于: 目录 一.ImageMagick介绍 二.安装支持库 三.在Linux上用源码编译安装ImageMagick与Jmagick 四.在Linux上使用yum安装ImageMagick与Jm ...
- 在windows和Linux上安装ImageMagick与jmagick,Maven配置、Java图片压缩代码(整理网上、结合自己情况、编写出来的新安装方式)
安装过程(如图所示) .Exceptionin thread "main" java.lang.UnsatisfiedLinkError:C:\WINDOWS\system32\j ...
- linux的使用以及linux服务器应用的部署
一.Linux(rehat.centos.ubuntu...)基础知识 上午: putty软件连接linux服务器: [root @ foundation2 ~ ] # 用户名 ...
- 使用tensorflow深度学习识别验证码
除了传统的PIL包处理图片,然后用pytessert+OCR识别意外,还可以使用tessorflow训练来识别验证码. 此篇代码大部分是转载的,只改了很少地方. 代码是运行在linux环境,tesso ...
- " ModuleNotFoundError: No module named 'tkinter' "的解决方法
踩坑场景 在使用pillow这个包处理图片的时候,运行程序,报错ModuleNotFoundError: No module named 'tkinter',遇到ModuleNotFoundError ...
- Eclipse中处理图片引包问题
在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEG ...
- Im4java 操作 ImageMagick 处理图片
背景 之前用的是JMagick,各种限制各种坑,直到使用了Im4java,真是相当的好用啊. 项目描述 ImageMagic的安装可参考:图片处理软件 ImageMagick 的安装和使用 Im4ja ...
随机推荐
- NSUserDefault
NSUserDefault是Cocoa提供的默认应用程序状态保持接口.它提供了简化的plist文件持久化方法.通过NSUserDefault类,你可以把用户首选项保存到plist文件中.到应用程序结束 ...
- OCPC(Optimized Cost per Click)机制
背景 在线广告中,广告按照CPM排序,排在前面的广告竞争有限广告位(截断).其中,CPM=bid*pctr.注GSP二价计费的,按照下一位bid计费.适当调整bid,可以提高竞价的排名,从而获得展现的 ...
- C++课程设计2
PS:大一下学期C++课程设计 1.成绩管理系统 #include<stdio.h> #include<string> #include<iostream> #in ...
- 【JDK1.8】JDK1.8集合源码阅读——IdentityHashMap
一.前言 今天我们来看一下本次集合源码阅读里的最后一个Map--IdentityHashMap.这个Map之所以放在最后是因为它用到的情况最少,也相较于其他的map来说比较特殊.就笔者来说,到目前为止 ...
- python 正则的使用 —— 编写一个简易的计算器
在 Alex 的博客上看到的对正则这一章节作业是编写一个计算器,要求能计算出下面的算式. 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 + ...
- lucene6+HanLP中文分词
1.前言 前一阵把博客换了个模版,模版提供了一个搜索按钮,这让我想起一直以来都想折腾的全文搜索技术,于是就用lucene6.2.1加上HanLP的分词插件做了这么一个模块CSearch.效果看这里:h ...
- 创业公司快速搭建立体化监控之路(WOT2016)
本文内容:创业型公司如何快速搭建可扩展,可落地的立体化监控平台 一.需求缘起 创业型公司有系统监控么?来看两个case: case 1:CXO大群内贴了一张"用户微信投诉"的截图 ...
- RUP 方法简介
1.什么是RUP: Rational Unified Process(以下简称RUP) 是一套软件工程方法,主要由 Ivar Jacobson的 The Objectory Approch 和 The ...
- 自学Python5.4-内置模块(2)
内置模块(2) 7. xml8.conf9.requests10.logging11.paramiko12.time & datetime 时间相关的操作,时间主要分三种表示方式: 时间戳 ...
- javascript 面向对象设计之 Function 普通类
var test = "Class01"; function Class01(privateValue, publicValue) { var _this = this; if ( ...