java 生成缩略图工具包

来源:互联网 发布:linux培训 编辑:程序博客网 时间:2024/06/06 09:45
Java代码 复制代码 收藏代码
  1. package com.facelook.util;
  2. import java.awt.Image;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import javax.imageio.ImageIO;
  9. import com.sun.image.codec.jpeg.JPEGCodec;
  10. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  11. public class ImageSmall {
  12. private File fromFile;
  13. private File toFile = new File("d:\\a");
  14. private String type;
  15. public ImageSmall(File fromFile,String type){
  16. this.fromFile = fromFile;
  17. this.type = type ;
  18. }
  19. public File samll() throws FileNotFoundException{
  20. toFile.deleteOnExit();
  21. toFile = new File("d:\\a");
  22. FileOutputStream out = new FileOutputStream(toFile);
  23. try {
  24. Image img = ImageIO.read(fromFile);
  25. BufferedImage tag = new BufferedImage(50,50, BufferedImage.TYPE_INT_RGB);
  26. tag.getGraphics().drawImage(img.getScaledInstance(50,50, Image.SCALE_SMOOTH), 0,0, null);
  27. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  28. encoder.encode(tag);
  29. out.close();
  30. } catch (IOException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. return toFile;
  35. }
  36. public File getFromFile() {
  37. return fromFile;
  38. }
  39. public void setFromFile(File fromFile) {
  40. this.fromFile = fromFile;
  41. }
  42. public File getToFile() {
  43. return toFile;
  44. }
  45. public void setToFile(File toFile) {
  46. this.toFile = toFile;
  47. }
  48. public String getType() {
  49. return type;
  50. }
  51. public void setType(String type) {
  52. this.type = type;
  53. }
  54. }  
原创粉丝点击