java通过坐标截取整图代码

package com.maystar.utils;
    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 CutPicture {
     private String srcpath;
     private String subpath;
     private String imageType;
     private int x;
     private int y;
     private int width;
     private int height;
     public CutPicture() {
     }
      
     public CutPicture(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 void cut() throws IOException {
      FileInputStream is = null;
      ImageInputStream iis = null;
      try {
       is = new FileInputStream(srcpath);
       this.setSrcpath(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);
       ImageIO.write(bi, this.imageType, new File(subpath));
      } finally {
       if (is != null)
        is.close();
       if (iis != null)
        iis.close();
      }
     } 
      
     public static void main(String[] args) {
      CutPicture o = new CutPicture("d:\\DSC01129.JPG", 111, 222, 888, 777);
      o.setSubpath("d:\\1.jpg");
      try {
       o.cut();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
    }
 

转载于:https://my.oschina.net/u/1054538/blog/686126

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值