java 图片裁剪代码
package com.actionsoft.apps.addons.invoice.pc.test;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import jodd.log.Logger;
import jodd.log.LoggerFactory;
public class JavaTest {
public static void main(String[] args) {
// System.out.println(",1,2,3,4,5,张三,李四,".replaceAll("张三,",""));
new JavaTest().cutImage("C:/Users/Administrator/Pictures/5.jpg", "D:/",200, 300, 400, 300);
}
private Logger log = LoggerFactory.getLogger(getClass());
private static String DEFAULT_CUT_PREVFIX = "cut_";
/**
* Description: 依据原图与裁切size截取局部图片
* @param srcImg 源图片
* @param output 图片输出流
* @param rect 须要截取部分的坐标和大小
*/
public void cutImage(File srcImg, OutputStream output,java.awt.Rectangle rect) {
if (srcImg.exists()) {
java.io.FileInputStream fis = null;
ImageInputStream iis = null;
try {
fis = new FileInputStream(srcImg);
// ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG,
// JPEG, WBMP, GIF, gif]
String types = Arrays.toString(ImageIO.getReaderFormatNames())
.replace("]", ",");
String suffix = null;
// 获取图片后缀
if (srcImg.getName().indexOf(".") > -1) {
suffix = srcImg.getName().substring(srcImg.getName().lastIndexOf(".") + 1);
}// 类型和图片后缀所有小写。然后推断后缀是否合法
if (suffix == null
|| types.toLowerCase().indexOf(suffix.toLowerCase() + ",") < 0) {
log.error("Sorry, the image suffix is illegal. the standard image suffix is {}."+ types);
return;
}
// 将FileInputStream 转换为ImageInputStream
iis = ImageIO.createImageInputStream(fis);
// 依据图片类型获取该种类型的ImageReader
ImageReader reader = ImageIO.getImageReadersBySuffix(suffix).next();
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, suffix, output);
log.info("图片生成成功,请到文件夹下查看");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
if (iis != null)
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
log.warn("the src image is not exist.");
}
}
//生成目标文件路径
public void cutImage(File srcImg, String destImgPath,java.awt.Rectangle rect) {
File destImg = new File(destImgPath);
if (destImg.exists()) {
String p = destImg.getPath();
try {
if (!destImg.isDirectory())
p = destImg.getParent();
if (!p.endsWith(File.separator))
p = p + File.separator;
cutImage(srcImg,new java.io.FileOutputStream(p + DEFAULT_CUT_PREVFIX+ "_"+ srcImg.getName()), rect);
} catch (FileNotFoundException e) {
log.warn("the dest image is not exist.");
}
} else
log.warn("the dest image folder is not exist.");
}
public void cutImage(String srcImg, String destImg, int x, int y, int width,
int height) {
cutImage(new File(srcImg), destImg, new java.awt.Rectangle(x, y, width, height));
}
}
最新文章
- ffmpeg-20161104[07,10,16,21,22,27,30]-bin.7z
- log4j的使用详细解析
- Bise IE6 在你的网站上加上它让IE滚蛋吧
- PLSQL查询表是否被锁定(转)
- Ajax请求中带有IPv6地址后的百分号的问题
- C# 如何执行bat文件 传参数
- Centos 6.4 8250/16550 只生成了4个串口
- hdu4671Backup Plan
- HTML解析引擎:Jumony 开源项目
- Cocos2d—X游戏开发之CCScrollView(滑动视图)(十二)
- 文本挖掘预处理之向量化与Hash Trick
- js 不要使用new
- RMAN备份介质的移动与再恢复测试 [ catalog start with ‘dir’ ]
- (后端)Java跨域过滤器
- Expm 1_3 数组中逆序对个数问题
- 同台同时多开DELPHI2007的解决办法
- 如何配置官方peerDroid,使其运行起来
- 浅谈Mysql 表设计规范(转)
- 使用 Json 文件存储
- 使用github的流程
热门文章
- 如何解决IIS配置HTTPS证书后刷新消失问题
- heaplog
- [Web 前端] 014 css 盒子模型
- org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NumberFormatException: For input string: ";W%"; ### Cause: java.lang.NumberFormatException: For input s
- java8 stream 用法收集
- hdu-2255.奔小康赚大钱(最大权二分匹配)
- 8、神经网络:表述(Neural Networks: Representation)
- C#控制文本框(TextBox)只能输入正数,负数,小数
- 5.MCScanX 与circos下载、安装、运用
- CSS中如何设置父元素透明度不影响子元素透明度