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上传图片剪切工具类的更多相关文章

  1. HttpTool.java(在java tool util工具类中已存在) 暂保留

    HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...

  2. java文件处理工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

  3. java格式处理工具类

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  4. Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类

    Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类   =========================== ©Copyright 蕃薯耀 2017年9月25日 http://www ...

  5. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  6. 使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。

    1.使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间. package com.fline.aic.utils; import java.text.DateFormat ...

  7. JAVA 8 日期工具类

    JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...

  8. JavaSE-基础语法(二)-系统类(java.lang.*)和工具类(java.util.*)

    系统类(java.lang.*)和工具类(java.util.*) 一.系统类(java.lang.*) 这个包下包含java语言的核心类,如String.Math.System和Thread类等,使 ...

  9. java 解析excel工具类

      java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...

随机推荐

  1. 4. ZooKeeper 基本操作

    ZooKeeper的数据模型及其API支持以下九个基本操作: 操作 描述 create 在ZooKeeper命名空间的指定路径中创建一个znode delete 从ZooKeeper命名空间的指定路径 ...

  2. mapreduce解析执行sql流程

    样例准备 编号 姓名 性别 班级编号 1 name_1 male 1 2 name_2 female 2 3 name_3 male 3 4 name_4 female 4 5 name_5 male ...

  3. Node.js初探之POST方式传输

    小知识:POST比GET传输的数据量大很多 POST发数据--"分段" 实例: 准备一个form.html文件: <!DOCTYPE html> <html> ...

  4. [转载] zookeeper工作原理、安装配置、工具命令简介

    转载自http://www.cnblogs.com/kunpengit/p/4045334.html 1 Zookeeper简介Zookeeper 是分布式服务框架,主要是用来解决分布式应用中经常遇到 ...

  5. [转载] 多图详解Spring框架的设计理念与设计模式

    转载自http://developer.51cto.com/art/201006/205212_all.htm Spring作为现在最优秀的框架之一,已被广泛的使用,51CTO也曾经针对Spring框 ...

  6. javaweb-3-在Eclipse中引入Tomcat

    一.在Eclipse中引入Tomcat 第一步: 第二步: 第三步: 第四部:

  7. mysql之ALTER COLUMN、CHANGE COLUMN、MODIFY COLUMN的区别

    ALTER COLUMN:设置或删除列的默认值(操作速度非常快) 例子: alter table film alter column rental_duration set default 5; al ...

  8. web-故道白云&Clound的错误

    web-故道白云 题目: 解题思路: 0x01 首先看到题目说html里有秘密,就看了下源代码如图, 重点在红圈那里,表示输入的变量是id,当然上一行的method="get"同时 ...

  9. ERP中通过自定义单打开流程图

    背景: AIO75系统中,制作流程图时选择所属模块,即可在对应模块的左侧列表展示流程图入口. 但在AIO5商务版中没有相关入口,故本文提供使用自定义菜单的方式挂出流程图. 具体步骤: 1.先去看一下是 ...

  10. python常用数据结构

    0. 字典初始化 d = {'a':1,'b':2} 或 d={} d['a'] = 1 d['b'] = 2 是不是和json格式数据很相似,语法和JavaScript又很相似 1. 变量接受序列分 ...