JAVA生产缩略图

来源:互联网 发布:linux如何停掉mysql 编辑:程序博客网 时间:2024/04/30 12:43
[java] view plain copy
  1. <pre name="code" class="java">package com.hunuo.common;  
  2.   
  3. import java.awt.Image;  
  4. import java.awt.image.BufferedImage;  
  5. import java.io.File;  
  6. import java.io.FileOutputStream;  
  7. import javax.imageio.ImageIO;  
  8. import com.sun.image.codec.jpeg.JPEGCodec;  
  9. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  10.   
  11. public class ImageUtil {  
  12.   
  13.     /** 
  14.      * 生成缩略图 
  15.      * @param srcImageFile 源图片文件的File实例      File file = new File("文件名"); 
  16.      * @param dstImageFileName 待生成的缩略图片完整路径(生成的格式为:image/jpeg); 
  17.      * @throws Exception 
  18.      */  
  19.     public static void makeSmallImage(File srcImageFile,String dstImageFileName) throws Exception {  
  20.         FileOutputStream fileOutputStream = null;  
  21.         JPEGImageEncoder encoder = null;  
  22.         BufferedImage tagImage = null;  
  23.         Image srcImage = null;  
  24.         try{  
  25.             srcImage = ImageIO.read(srcImageFile);  
  26.             int srcWidth = srcImage.getWidth(null);//原图片宽度  
  27.             int srcHeight = srcImage.getHeight(null);//原图片高度  
  28.             int dstMaxSize = 120;//目标缩略图的最大宽度/高度,宽度与高度将按比例缩写  
  29.             int dstWidth = srcWidth;//缩略图宽度  
  30.             int dstHeight = srcHeight;//缩略图高度  
  31.             float scale = 0;  
  32.             //计算缩略图的宽和高  
  33.             if(srcWidth>dstMaxSize){  
  34.                 dstWidth = dstMaxSize;  
  35.                 scale = (float)srcWidth/(float)dstMaxSize;  
  36.                 dstHeight = Math.round((float)srcHeight/scale);  
  37.             }  
  38.             srcHeight = dstHeight;  
  39.             if(srcHeight>dstMaxSize){  
  40.                 dstHeight = dstMaxSize;  
  41.                 scale = (float)srcHeight/(float)dstMaxSize;  
  42.                 dstWidth = Math.round((float)dstWidth/scale);  
  43.             }  
  44.             //生成缩略图  
  45.             tagImage = new BufferedImage(dstWidth,dstHeight,BufferedImage.TYPE_INT_RGB);  
  46.             tagImage.getGraphics().drawImage(srcImage,0,0,dstWidth,dstHeight,null);  
  47.             fileOutputStream = new FileOutputStream(dstImageFileName);  
  48.             encoder = JPEGCodec.createJPEGEncoder(fileOutputStream);  
  49.             encoder.encode(tagImage);  
  50.             fileOutputStream.close();  
  51.             fileOutputStream = null;  
  52.         }finally{  
  53.             if(fileOutputStream!=null){  
  54.                 try{  
  55.                     fileOutputStream.close();  
  56.                 }catch(Exception e){  
  57.                 }  
  58.                 fileOutputStream = null;  
  59.             }  
  60.             encoder = null;  
  61.             tagImage = null;  
  62.             srcImage = null;  
  63.             System.gc();  
  64.         }  
  65.     }  
  66. }  
  67. 这里可能遇到问题  
  68. import com.sun.image.codec.jpeg.JPEGCodec;   
  69. import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  70. 报错:   
  71. Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar   
  72.   
  73.   
  74. 此时解决办法:   
  75. Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。  
  76.   
  77. </pre><br>  
  78. <br>  
  79. <pre></pre>  
  80. <pre></pre>  
  81.     
0 0
原创粉丝点击