图片放缩

最近由于硬盘太小,数码照片太多,占了太多空间,写一个批量放缩照片的类!

package com.my.test.image; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ImageTool { private Boolean proportion=true; private List fileInList=new ArrayList(); private List fileOutList=new ArrayList(); public Boolean photojob(String inputdir,String outputdir,double bl){ if(!check(inputdir,outputdir,bl)){ return false; } for(int i=0;i<fileInList.size();i++){ start((String)fileInList.get(i),(String)fileOutList.get(i),bl); } return true; } public Boolean start(String in,String out,double bl){ //建立输出文件对象 File file = new File(out); FileOutputStream tempout = null; try { tempout = new FileOutputStream(file); } catch (Exception ex) { System.out.println(ex.toString()); } Image img = null; //用于操作windows的一些组件 Toolkit tk = Toolkit.getDefaultToolkit(); Applet app = new Applet(); MediaTracker mt = new MediaTracker(app); try { img = tk.getImage(in); mt.addImage(img, 0); mt.waitForID(0); } catch (Exception e) { e.printStackTrace(); } if (img.getWidth(null) == -1) { return false; } else { int new_w; int new_h; if (this.proportion == true) { //判断是否是等比缩放. //为等比缩放计算输出的图片宽度及高度 new_w = (int) (((double) img.getWidth(null))*bl); new_h = (int) (((double) img.getHeight(null)) *bl); } else { new_w = 100; //输出的图片宽度 new_h = 100; //输出的图片高度 } BufferedImage buffImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB); Graphics g = buffImg.createGraphics(); g.setColor(Color.white); g.fillRect(0, 0, new_w, new_h); g.drawImage(img, 0, 0, new_w, new_h, null); g.dispose(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout); try { encoder.encode(buffImg); tempout.close(); } catch (IOException ex) { System.out.println(ex.toString()); } } return true; } public Boolean check(String inputdir,String outputdir,double bl){ //判断放缩比例 if(!checkbl(bl)){ return false; } //判断目录 if(!checkFile( inputdir, outputdir)){ return false; } return true; } //判断目录是否存在,过滤文件名 public Boolean checkFile(String inputdir,String outputdir){ File file=null; File file1=null; try { file = new File(inputdir); } catch (NullPointerException e) { return false; } try { file1 = new File(outputdir); } catch (NullPointerException e) { return false; } if(file.exists()){ if(file.isFile()){ fileInList.add(file.getParent()+"/"+file.getName()); if(file1.isDirectory()){ fileOutList.add(outputdir+"/"+file.getName().split("//.")[0]+"_out."+file.getName().split("//.")[1]); }else if(file1.isFile()){ fileOutList.add(file1.getParent()+"/"+file.getName().split("//.")[0]+"_out."+file.getName().split("//.")[1]); } }else if(file.isDirectory()){ for(int i=0;i<file.list().length;i++){ if(checkname(file.list()[i])){ fileInList.add(file.getPath()+"/"+file.list()[i]); if(file1.isDirectory()){ fileOutList.add(outputdir+"/"+file.list()[i].split("//.")[0]+"_out."+file.list()[i].split("//.")[1]); }else if(file1.isFile()){ fileOutList.add(file1.getParent()+"/"+file.list()[i].split("//.")[0]+"_out."+file.list()[i].split("//.")[1]); } } } } }else{ return false; } return true; } //判断放缩比例 public Boolean checkbl(double bl){ if(bl<0){ System.out.println("放缩比例必须大于零!"); return false; } return true; } //过滤扩展名 public Boolean checkname(String name){ if(name.split("//.")[1].equals("jpg")){ return true; }else if(name.split("//.")[1].equals("bmp")){ return true; } return false; } public static void main(String[] args) { ImageTool it=new ImageTool(); String in="C://Documents and Settings//All Users//Documents//My Pictures//示例图片"; String out="D://temp"; it.photojob(in,out, 0.2); System.out.println("原文件:/n"+in); System.out.println("目标文件:/n"+out); System.out.println("放缩完毕!"); } }

你可能感兴趣的:(exception,String,File,null,applet,照片)