java上传图片剪切工具类
package com.up.util; import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class OperateImage { private String srcpath; //原图路径
private String subpath; //目标存放路径
private String imageType; //图片类型
private int x;
private int y;
private int width; //图片目标宽度
private int height; //图片目标高度 public OperateImage() {
} public OperateImage(String srcpath, int x, int y, int width, int height) {
this.srcpath = srcpath;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
} public int getHeight() {
return height;
} public void setHeight(int height) {
this.height = height;
} public String getSrcpath() {
return srcpath;
} public void setSrcpath(String srcpath) {
this.srcpath = srcpath;
if(srcpath != null) {
this.imageType = srcpath.substring(srcpath.indexOf(".")+1, srcpath.length());
}
} public String getSubpath() {
return subpath;
} public void setSubpath(String subpath) {
this.subpath = subpath;
} public int getWidth() {
return width;
} public void setWidth(int width) {
this.width = width;
} public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} public String getImageType() {
return imageType;
} public void setImageType(String imageType) {
this.imageType = imageType;
} public boolean cut() throws IOException {
FileInputStream is = null;
ImageInputStream iis = null;
boolean bol = false;
try {
is = new FileInputStream(srcpath);
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(this.imageType);
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
//实际高度大于目标高度或者实际宽度大于目标宽度则进行剪切
File o = new File(srcpath);
BufferedImage bii = ImageIO.read(o);
int itempWidth = bii.getWidth(); //实际宽度
int itempHeight = bii.getHeight(); //实际高度
if ((itempHeight > height) || (itempWidth > width)) {
ImageIO.write(bi, this.imageType, new File(subpath));
bol = true;
}
} finally {
if (is != null)
is.close();
if (iis != null)
iis.close();
}
return bol;
} public static void main(String[] args) {
OperateImage o = new OperateImage("E:\\testdata\\1.jpg", 0, 0, 100, 100);
o.setSubpath("E:\\testdata\\2.jpg");
o.setImageType("jpg");
try {
o.cut();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java上传图片剪切工具类的更多相关文章
- HttpTool.java(在java tool util工具类中已存在) 暂保留
HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...
- java文件处理工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...
- java格式处理工具类
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类
Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类 =========================== ©Copyright 蕃薯耀 2017年9月25日 http://www ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- 使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。
1.使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间. package com.fline.aic.utils; import java.text.DateFormat ...
- JAVA 8 日期工具类
JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...
- JavaSE-基础语法(二)-系统类(java.lang.*)和工具类(java.util.*)
系统类(java.lang.*)和工具类(java.util.*) 一.系统类(java.lang.*) 这个包下包含java语言的核心类,如String.Math.System和Thread类等,使 ...
- java 解析excel工具类
java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...
随机推荐
- seajs源码
/*** Sea.js 3.0.0 | seajs.org/LICENSE.md 中文注释由 李祥威 添加,为个人对细节的理解,官方解释很详细的地方就不说了 难免有错漏,联系我: chuangweil ...
- 使用mitmproxy嗅探双向认证ssl链接——嗅探AWS IoT SDK的mqtts
亚马逊AWS IoT使用MQTTS(在TLS上的MQTT)来提供物联网设备与云平台直接的通信功能.出于安全考虑,建议给每个设备配备了证书来认证,同时,设备也要安装亚马逊的根证书:这样,在使用8883端 ...
- day2--通过setup设置网卡
配置IP地址,安装完成centos之后,需要配IP地址,流程如下: 1.首先开启虚拟机,如下: 2.登陆账号,root账户登陆,如下: 3.输入setup,按回车键进入,设置IP的方法有很多种,此处采 ...
- Ubuntu & Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践
相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...
- 一个ICON图标的转换程序
抽空写了一个ICON图标的转换程序,支持png\jpe\bmp格式到ico的转换.具体的程序就在下面,如果看的人多,过两天再把思路写一下.废话不说,见代码. ...
- python基础阶段 经典练习题 拾英札记(2)
因为编程的练习题是交互式的,在不断调试和不断渐进完善中,你会有一种成就感和快乐感,不断的修缮,不断的尝试. 其实,认知自己,和探索世界,也是这样的啊. 只要不放弃,要坚持. #7 根据列表lt,实现 ...
- NodeJS 常用模块积累
cluster&forever cluster & forever 虽然 nodejs 原生已经提供了 cluster 模块,大部分情况下可以满足我们的基本需求,但这两个模块 clus ...
- vimgdb安装
vimgdb install ************** a) You need: vim-7.3.tar.bz2 http://www.vim.org/sources.php vimgdb- (t ...
- PHP设计模式之工厂模式
工厂模式(Factory pattern)和单例模式一样,是另外一种创建型模式. 和单例模式不同的是,单例模式会创建和管理一个单独的类型的单一对象,工厂模式则是用于创建多种不同类型的类的多个对象. 工 ...
- js 图片转换为base64
<input id="file" type="file"> <img id="img" style="max-h ...