java图片裁切工具类

  1. import java.awt.Rectangle;  
  2. import java.awt.image.BufferedImage;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.util.Iterator;  
  7.   
  8. import javax.imageio.ImageIO;  
  9. import javax.imageio.ImageReadParam;  
  10. import javax.imageio.ImageReader;  
  11. import javax.imageio.stream.ImageInputStream;  
  12.   
  13. public class ImgCutUtil {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.         File picture = new File("C:/Users/Administrator/Pictures/我的图片/neusoft.png");
  20.         BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture));    
  21.         System.out.println(String.format("%.1f",picture.length()/1024.0));  
  22.         System.out.println(sourceImg.getWidth());  
  23.         System.out.println(sourceImg.getHeight()); 
  24.         ImgCutUtil.cut(3050300400"d:/1.jpg""d:/100.jpg");  
  25.   
  26.     }  
  27.   
  28.     /** 
  29.      * 图片裁切 
  30.      * @param x1 选择区域左上角的x坐标 
  31.      * @param y1 选择区域左上角的y坐标 
  32.      * @param width 选择区域的宽度 
  33.      * @param height 选择区域的高度 
  34.      * @param sourcePath 源图片路径 
  35.      * @param descpath 裁切后图片的保存路径 
  36.      */  
  37.     public static void cut(int x1, int y1, int width, int height,  
  38.             String sourcePath, String descpath) {  
  39.   
  40.         FileInputStream is = null;  
  41.         ImageInputStream iis = null;  
  42.         try {  
  43.             is = new FileInputStream(sourcePath);  
  44.             String fileSuffix = sourcePath.substring(sourcePath  
  45.                     .lastIndexOf(".") + 1);  
  46.             Iterator<ImageReader> it = ImageIO  
  47.                     .getImageReadersByFormatName(fileSuffix);  
  48.             ImageReader reader = it.next();  
  49.             iis = ImageIO.createImageInputStream(is);  
  50.             reader.setInput(iis, true);  
  51.             ImageReadParam param = reader.getDefaultReadParam();  
  52.             Rectangle rect = new Rectangle(x1, y1, width, height);  
  53.             param.setSourceRegion(rect);  
  54.             BufferedImage bi = reader.read(0, param);  
  55.             ImageIO.write(bi, fileSuffix, new File(descpath));  
  56.         } catch (Exception ex) {  
  57.             ex.printStackTrace();  
  58.         } finally {  
  59.             if (is != null) {  
  60.                 try {  
  61.                     is.close();  
  62.                 } catch (IOException e) {  
  63.                     e.printStackTrace();  
  64.                 }  
  65.                 is = null;  
  66.             }  
  67.             if (iis != null) {  
  68.                 try {  
  69.                     iis.close();  
  70.                 } catch (IOException e) {  
  71.                     e.printStackTrace();  
  72.                 }  
  73.                 iis = null;  
  74.             }  
  75.         }  
  76.   
  77.     }  
  78.   
  79. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值