产生缩略图

import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import com.sun.jimi.core.Jimi; import com.sun.jimi.core.JimiException; import com.sun.jimi.core.JimiUtils; public class CreatPic { public static void main(String[] args) { String filepath = "c:/temp/body.jpg"; String savepath = "c:/temp"; String filename = "body.jpg"; createThumbnail(filepath, savepath, filename); } //產生縮略圖 public static void createThumbnail(String filepath,String savepath,String filename) { File tarfile = new File(savepath); if(!tarfile.exists()) tarfile.mkdirs(); String dest = savepath+"//" + filename+ "_thumbnail.jpg"; File imgFile = new File(dest); int height = 117; int width = 175; if(imgFile.exists()) { BufferedImage bi; try { bi = ImageIO.read(imgFile); double h = bi.getHeight(); double w = bi.getWidth(); if(h/w >1.2) { height = 170; width = 115; } if(w/h >1.5) { height = 100; width = 170; } } catch (IOException e2) { System.out.println("images file read error! " + imgFile.getAbsolutePath()); e2.printStackTrace(); } } try { FileOutputStream fout = new FileOutputStream(dest); Jimi.putImage("image/jpeg",JimiUtils.getThumbnail(filepath,width,height,0),fout); fout.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (JimiException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } setHidden(dest); } //設置成隱藏文件 private static void setHidden(String filePath) { Runtime rt = Runtime.getRuntime(); try { rt.exec("ATTRIB +H /""+filePath+"/""); } catch (IOException e) { e.printStackTrace(); } } }

jar文件  JimiProClasses.zip

 

《本站download》

你可能感兴趣的:(产生缩略图)