图片裁剪工具类(支持jpg,png,bmp,gif(剪切后为静态))
来源:互联网 发布:软件项目验收阶段 编辑:程序博客网 时间:2024/06/04 23:27
import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import javax.imageio.ImageIO;import javax.imageio.ImageReadParam;import javax.imageio.ImageReader;import javax.imageio.stream.ImageInputStream;/** * 图片裁剪操作 * @author */public class ImageCutUtil {/** * 获取图片宽度 * @param file 图片文件 * @return 宽度 */ public static int getImgWidth(File file) { InputStream is = null; BufferedImage src = null; int ret = -1; try { is = new FileInputStream(file); src = javax.imageio.ImageIO.read(is); ret = src.getWidth(null); // 得到源图宽 is.close(); } catch (Exception e) { e.printStackTrace(); } return ret; } /** * 获取图片高度 * @param sourcePath 图片路径 * @return 宽度 */ public static int getImgWidth(String sourcePath) { File file=new File(sourcePath); return getImgWidth(file); } /** * 获取图片高度 * @param file 图片文件 * @return 高度 */ public static int getImgHeight(File file) { InputStream is = null; BufferedImage src = null; int ret = -1; try { is = new FileInputStream(file); src = javax.imageio.ImageIO.read(is); ret = src.getHeight(null); // 得到源图高 is.close(); } catch (Exception e) { e.printStackTrace(); } return ret; } /** * 获取图片高度 * @param sourcePath 图片路径 * @return 高度 */ public static int getImgHeight(String sourcePath) { File file=new File(sourcePath); return getImgHeight(file); }/** * 图片裁切 * 选择的总区域必须与图片原始大小相同 * @param X 选择区域左上角的x坐标 * @param Y 选择区域左上角的y坐标 * @param width 选择区域的宽度 * @param height 选择区域的高度 * @param sourcePath 源图片路径 * @param descpath 裁切后图片的保存路径 */public static void cut(int X, int Y, int width, int height,String sourcePath, String descpath) {FileInputStream is = null;ImageInputStream iis = null;try {is = new FileInputStream(sourcePath);String fileSuffix = sourcePath.substring(sourcePath.lastIndexOf(".") + 1);Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(fileSuffix);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);ImageIO.write(bi, fileSuffix, new File(descpath));System.out.println("图片剪切成功,生成图片路径:"+descpath);} catch (Exception ex) {ex.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}is = null;}if (iis != null) {try {iis.close();} catch (IOException e) {e.printStackTrace();}iis = null;}}}/** * 图片裁切 * 选择的总区域与图片原始大小不同 * @param selectWidth 选择区域的宽度 * @param selectHeight 选择区域的高度 * @param selectX 选择区域左上角的x坐标 * @param selectY 选择区域左上角的y坐标 * @param areaWidth 选择区域总宽度 * @param areaHeight 选择区域总高度 * @param sourcePath 源图片路径 * @param descpath 裁切后图片的保存路径 */public static void cut(int areaWidth, int areaHeight,int selectX, int selectY, int selectWidth, int selectHeight,String sourcePath, String descpath) {int imgWidth=getImgWidth(sourcePath);int imgHeight=getImgHeight(sourcePath);int X=selectX*imgWidth/areaWidth;int Y=selectY*imgHeight/areaHeight;int width=selectWidth*imgWidth/areaWidth;int height=selectHeight*imgHeight/areaHeight;cut(X, Y, width, height, sourcePath, descpath);}public static void main(String[] args) {System.out.println("图片的宽度:"+getImgWidth("d:/1.bmp"));System.out.println("图片的高度:"+getImgHeight("d:/1.bmp"));ImageCutUtil.cut(30, 50, 300, 400, "d:/1.bmp", "d:/2.bmp");}}
- 图片裁剪工具类(支持jpg,png,bmp,gif(剪切后为静态))
- 获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP'
- fso获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP
- VB6.0用GDI+保存图片为JPG、TIFF、PNG、GIF、BMP等格式
- VB6.0用GDI+保存图片为JPG、TIFF、PNG、GIF、BMP等格式
- JPG,PNG,GIF,WBMP,BMP
- 使用系统imgdecmp库解码JPG,GIF,PNG,BMP图片
- 图片中,bmp . png . jpg . gif ……含义
- 使用系统imgdecmp库解码JPG,GIF,PNG,BMP图片
- C# 图片格式(JPG,BMP,PNG,GIF)等转换为ICO图标
- 批量图片缩小工具,JPG|PNG|BMP图片缩小工具
- 解决eWebEditor上传图片提示:请选择一个有效的文件,支持的格式有(GIF|JPG|JPEG|BMP|PNG)!
- 加载多种图片格式(gif,jpg,png.bmp)
- ios下 jpg gif bmp 转 png
- VC6 CImage 加载jpg png bmp gif
- JPG png GIF BMP图片格式的区别
- png gif jpg图片区别
- 保存为BMP,Jpg,Png格式的图片
- 1056. Mice and Rice (25)
- vs2008快捷键极其技巧
- ios7 UIBarButtonItem 边距问题
- 1057. Stack (30)
- 献给2014届毕业生的建议
- 图片裁剪工具类(支持jpg,png,bmp,gif(剪切后为静态))
- web网页input框设置,点击input框弹出键盘,再收回键盘,焦点框会随页面滚动
- 2013第四届“蓝桥杯”CC++程序设计高职高专组试题及部分答案
- 动态规划_矩阵链连乘问题
- 19.【elipse的安装和使用】
- JBoss 系列五十一:使用Apache httpd(mod_proxy)和JBoss构架高可用集群环境
- ##iOS##关于UITableView的tableHeaderView的注意点
- 记linux内核处理bootargs到内核并加载模块时传入参数(module_param_named()等)
- jsp相对路径和绝对路径小谈