package image;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 {

    public static void cut(String srcpath,String subpath,int x,int y,int width,int height) throws IOException{ 
         
        FileInputStream is = null ;
        ImageInputStream iis =null ;
     
        try{   
            //读取图片文件
            is = new FileInputStream(srcpath); 
            
            Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg");  
            ImageReader reader = it.next(); 
            //获取图片流 
            iis = ImageIO.createImageInputStream(is);
        
            reader.setInput(iis,true) ;
      
            ImageReadParam param = reader.getDefaultReadParam(); 
             
            /**//*
             * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象
             * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。 
             */ 
            Rectangle rect = new Rectangle(x, y, width, height); 
              
            //提供一个 BufferedImage,将其用作解码像素数据的目标。 
            param.setSourceRegion(rect);             /**//*
             * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将
             * 它作为一个完整的 BufferedImage 返回。
             */
            BufferedImage bi = reader.read(0,param);                
      
            //保存新图片 
            ImageIO.write(bi, "jpg", new File(subpath));     
        }
        
        finally{
            if(is!=null)
               is.close() ;       
            if(iis!=null)
               iis.close();  
        } 
    }
    
    
    public static boolean readFloder(String path) throws IOException{
File file = new File(path);   
    if (file.isDirectory()) {//如果是个文件夹   
      String[] filelist = file.list();//得到下面所有文件(包括文件夹)   
      int length =filelist.length;
      for (int i = 0; i < length; i++) {//循环每一个文件   
     String filepath =path + "/" + filelist[i];
        File readfile = new File(filepath);   
        
        if (!readfile.isDirectory()) {//如果不是文件夹 同上面的操作   
          
          String name = readfile.getName();
          String endName = name.substring(name.indexOf(".")+1).toLowerCase();
          String photopath = path +"/"+name;
           if(endName.equals("jpg")||endName.equals("jpeg")){
           String subpath = "e:/newphoto/";
           
           File  oldfile  =  new  File(subpath);
           //如果文件夹不存在就创建
           if  (!oldfile.exists()) {
           oldfile.mkdir();
           }
           String newname = subpath+"/"+name;
//            procePhoto(photopath,358, 441, 1, "1");
           cut(photopath, newname, 0, 0, 358, 441);
           }else{ 
           if(name.equals("Thumbs.db")){
           delFile(filepath);
           filepath =""; 
           }
           if(name.equals("Desktop_.ini")){
           delFile(filepath);
           filepath =""; 
           }
           
           }
            
        }else if (readfile.isDirectory()) {//如果是文件夹 那么就递规   
         readFloder(path + "\\" + filelist[i]);   
        }   
      }   
    }
  
       return true;
}
    
    public static void  delFile(String  filePathAndName)  {      
       try  {      
           String  filePath  =  filePathAndName;      
           filePath  =  filePath.toString();      
           java.io.File  myDelFile  =  new  java.io.File(filePath);      
           myDelFile.delete();      
     
       }      
       catch  (Exception  e)  {      
           System.out.println("删除文件操作出错");      
           e.printStackTrace();      
     
       }      
     
   }     
    
    public static void main(String args[]) throws IOException{
         
     String path ="F:/photo/";
      readFloder( path);
    
    } }