Java图片居中裁剪代码

来源:互联网 发布:动画ppt制作软件 编辑:程序博客网 时间:2024/05/21 01:28
[java] view plain copy
  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. import javax.imageio.ImageIO;  
  8. import javax.imageio.ImageReadParam;  
  9. import javax.imageio.ImageReader;  
  10. import javax.imageio.stream.ImageInputStream;  
  11. public class OperateImage {  
  12.     // ===源图片路径名称如:c:\1.jpg  
  13.     private String srcpath;  
  14.     // ===剪切图片存放路径名称。如:c:\2.jpg  
  15.     private String subpath;  
  16.     // ===剪切点x坐标  
  17.     private int x;  
  18.     private int y;  
  19.     // ===剪切点宽度  
  20.     private int width;  
  21.     private int height;  
  22.     
  23.     public OperateImage() {  
  24.     }  
  25.     
  26.     public OperateImage(int x, int y, int width, int height) {  
  27.         this.x = x;  
  28.         this.y = y;  
  29.         this.width = width;  
  30.         this.height = height;  
  31.     }  
  32.     
  33.     /** 
  34.      * 对图片裁剪,并把裁剪完的新图片保存 。 
  35.      */  
  36.     public void cut() throws IOException {  
  37.         FileInputStream is = null;  
  38.         ImageInputStream iis = null;  
  39.         try {  
  40.             // 读取图片文件  
  41.             is = new FileInputStream(srcpath);  
  42.             /** 
  43.              * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader 
  44.              * 声称能够解码指定格式。 参数:formatName - 包含非正式格式名称 . 
  45.              * (例如 "jpeg" 或 "tiff")等 。 
  46.              */  
  47.             Iterator<ImageReader> it = ImageIO  
  48.                     .getImageReadersByFormatName("jpg");  
  49.             ImageReader reader = it.next();  
  50.             // 获取图片流  
  51.             iis = ImageIO.createImageInputStream(is);  
  52.             /** 
  53.              * iis:读取源。true:只向前搜索 
  54.              * 将它标记为 ‘只向前搜索’。 
  55.              * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader 
  56.              * 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。 
  57.              */  
  58.             reader.setInput(iis, true);  
  59.             /** 
  60.              * <p> 
  61.              * 描述如何对流进行解码的类 
  62.              * <p> 
  63.              * 用于指定如何在输入时从 Java Image I/O 
  64.              * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件 
  65.              * 将从其 ImageReader 实现的 getDefaultReadParam 方法中返回 
  66.              * ImageReadParam 的实例。 
  67.              */  
  68.             ImageReadParam param = reader.getDefaultReadParam();  
  69.             /** 
  70.              * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象 
  71.              * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 
  72.              */  
  73.             Rectangle rect = new Rectangle(x, y, width, height);  
  74.             // 提供一个 BufferedImage,将其用作解码像素数据的目标。  
  75.             param.setSourceRegion(rect);  
  76.             /** 
  77.              * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将 
  78.              * 它作为一个完整的 BufferedImage 返回。 
  79.              */  
  80.             BufferedImage bi = reader.read(0, param);  
  81.             // 保存新图片  
  82.             ImageIO.write(bi, "jpg"new File(subpath));  
  83.         } finally {  
  84.             if (is != null)  
  85.                 is.close();  
  86.             if (iis != null)  
  87.                 iis.close();  
  88.         }  
  89.     }  
  90.     
  91.     public int getHeight() {  
  92.         return height;  
  93.     }  
  94.     
  95.     public void setHeight(int height) {  
  96.         this.height = height;  
  97.     }  
  98.     
  99.     public String getSrcpath() {  
  100.         return srcpath;  
  101.     }  
  102.     
  103.     public void setSrcpath(String srcpath) {  
  104.         this.srcpath = srcpath;  
  105.     }  
  106.     
  107.     public String getSubpath() {  
  108.         return subpath;  
  109.     }  
  110.     
  111.     public void setSubpath(String subpath) {  
  112.         this.subpath = subpath;  
  113.     }  
  114.     
  115.     public int getWidth() {  
  116.         return width;  
  117.     }  
  118.     
  119.     public void setWidth(int width) {  
  120.         this.width = width;  
  121.     }  
  122.     
  123.     public int getX() {  
  124.         return x;  
  125.     }  
  126.     
  127.     public void setX(int x) {  
  128.         this.x = x;  
  129.     }  
  130.     
  131.     public int getY() {  
  132.         return y;  
  133.     }  
  134.     
  135.     public void setY(int y) {  
  136.         this.y = y;  
  137.     }  
  138.     
  139.     public static void main(String[] args) throws Exception {  
  140.         String name = "C:\\2.jpg";  
  141.         OperateImage o = new OperateImage(180180180180);  
  142.         o.setSrcpath(name);  
  143.         o.setSubpath("D:\\6669.jpg");  
  144.         o.cut();  
  145.     }  
  146. }