Java:扫描包含图片的文件夹,将符合分辨率格式的复制出来
1 import javax.imageio.ImageIO; 2 import java.awt.image.BufferedImage; 3 import java.io.*; 4 import java.util.LinkedList; 5 6 public class MovePhoto { 7 public static void main(String[] args){ 8 System.out.println("aaa"); 9 showFiles(); 10 } 11 12 public static int DealImage(File file){ 13 int width = 0; 14 int height = 0; 15 BufferedImage bi = null; 16 try{ 17 if(file.exists()){ 18 bi = ImageIO.read(file); 19 if(bi!=null){ 20 width = bi.getWidth(); 21 height = bi.getHeight(); 22 } 23 } 24 }catch(Exception e){ 25 e.printStackTrace(); 26 } 27 if(Math.abs(width-height)<=15 && width<=90 && height<=90){ 28 System.out.println("width=" + width + ", heithg=" + height); 29 return 1; 30 }else{ 31 return 0; 32 } 33 } 34 35 public static void showFiles(){ 36 int fileNum = 0; 37 String newPath = "d:/photo"; 38 File file = new File("d:/image"); 39 if(file.exists()){ 40 LinkedList<File> list = new LinkedList<File>(); 41 File[] files = file.listFiles(); 42 for(File f:files){ 43 File tmp = new File(f.getAbsolutePath()); 44 if(DealImage(tmp)==1){ 45 System.out.println("文件:" + tmp.getName()); 46 newPath = newPath + "/" + tmp.getName().toString(); 47 moveFiles(tmp,newPath); 48 fileNum++; 49 newPath = "d:/photo"; 50 } 51 } 52 } 53 System.out.println(fileNum); 54 } 55 56 public static void moveFiles(File file, String newPath){ 57 BufferedInputStream bif = null; 58 BufferedOutputStream bof = null; 59 if(file.exists()){ 60 try{ 61 bif = new BufferedInputStream(new FileInputStream(file)); 62 bof = new BufferedOutputStream(new FileOutputStream(newPath)); 63 byte[] buffer = new byte[1444]; 64 int length; 65 while((length=bif.read(buffer))>0){ 66 bof.write(buffer,0,length); 67 } 68 } catch (IOException e){ 69 e.printStackTrace(); 70 } finally { 71 try { 72 bif.close(); 73 bof.close(); 74 } catch (IOException e){ 75 e.printStackTrace(); 76 } 77 } 78 } 79 } 80 }