Java改變圖片的大小

轉自:http://blog.csdn.net/mainstream_code/article/details/7036302

  1. <pre name="code" class="java">/**  
  2.  * 改變圖片的大小到寬爲size,然後高隨着寬等比例變化  
  3.  * @param is 上傳的圖片的輸入流  
  4.  * @param os 改變了圖片的大小後,把圖片的流輸出到目標OutputStream  
  5.  * @param size 新圖片的寬  
  6.  * @param format 新圖片的格式  
  7.  * @throws IOException  
  8.  */    
  9. public static void resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {    
  10.     BufferedImage prevImage = ImageIO.read(is);    
  11.     double width = prevImage.getWidth();    
  12.     double height = prevImage.getHeight();    
  13.     double percent = size/width;    
  14.     int newWidth = (int)(width * percent);    
  15.     int newHeight = (int)(height * percent);    
  16.     BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);    
  17.     Graphics graphics = image.createGraphics();    
  18.     graphics.drawImage(prevImage, 00, newWidth, newHeight, null);    
  19.     ImageIO.write(image, format, os);    
  20.     os.flush();    
  21.     is.close();    
  22.     os.close();    
  23. }  </pre><br>  
  24. <br>  
  25. <pre></pre>  
  26. <pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java">package graphicsTest;    
  27.     
  28. import java.awt.Image;    
  29. import java.awt.image.BufferedImage;    
  30. import java.io.File;    
  31. import java.io.FileOutputStream;    
  32.     
  33. import javax.imageio.ImageIO;    
  34.     
  35. import com.sun.image.codec.jpeg.JPEGCodec;    
  36. import com.sun.image.codec.jpeg.JPEGImageEncoder;    
  37.     
  38. public class GraphicsTest1 {    
  39.     
  40.     // 圖片寬和高的最大尺寸    
  41.     public static final int IMAGEMAXBIG = 2000;    
  42.     // 圖片寬和高的最小尺寸    
  43.     public static final int IMAGEMINBIG = 10;    
  44.     // 按原圖大小生成新圖    
  45.     public static final int CREATENEWIMAGETYPE_0 = 0;    
  46.     // 按指定的大小生成新圖    
  47.     public static final int CREATENEWIMAGETYPE_1 = 1;    
  48.     // 按原圖寬高比例生成新圖-按指定的寬度    
  49.     public static final int CREATENEWIMAGETYPE_2 = 2;    
  50.     // 按原圖寬高比例生成新圖-按指定的高度    
  51.     public static final int CREATENEWIMAGETYPE_3 = 3;    
  52.     // 按原圖寬高比例生成新圖-按指定的寬和高中較大的尺寸    
  53.     public static final int CREATENEWIMAGETYPE_4 = 4;    
  54.     // 按原圖寬高比例生成新圖-按指定的寬和高中較小的尺寸    
  55.     public static final int CREATENEWIMAGETYPE_5 = 5;    
  56.     
  57.     /**  
  58.      *   
  59.      * @param _file  
  60.      *            原圖片  
  61.      * @param createType  
  62.      *            處理類型  
  63.      * @param newW  
  64.      *            新寬度  
  65.      * @param newH  
  66.      *            新高度  
  67.      * @return  
  68.      * @throws Exception  
  69.      */    
  70.     public static String createNewImage(File _file, int createType, int newW,    
  71.             int newH) throws Exception {    
  72.         if (_file == null)    
  73.             return null;    
  74.         String fileName = _file.getPath();    
  75.         if (fileName == null || "".equals(fileName)    
  76.                 || fileName.lastIndexOf(".") == -1)    
  77.             return null;    
  78.         String newFileName = "_NEW";    
  79.         /*  
  80.          * else newFileName = "_" + newFileName;  
  81.          */    
  82.     
  83.         String outFileName = fileName.substring(0, fileName.lastIndexOf("."))    
  84.                 + newFileName    
  85.                 + fileName.substring(fileName.lastIndexOf("."), fileName    
  86.                         .length());    
  87.         String fileExtName = fileName.substring(    
  88.                 (fileName.lastIndexOf(".") + 1), fileName.length());    
  89.         if (newW < IMAGEMINBIG)    
  90.             newW = IMAGEMINBIG;    
  91.         else if (newW > IMAGEMAXBIG)    
  92.             newW = IMAGEMAXBIG;    
  93.     
  94.         if (newH < IMAGEMINBIG)    
  95.             newH = IMAGEMINBIG;    
  96.         else if (newH > IMAGEMAXBIG)    
  97.             newH = IMAGEMAXBIG;    
  98.     
  99.         // 得到原圖信息    
  100.         if (!_file.exists() || !_file.isAbsolute() || !_file.isFile()    
  101.                 || !checkImageFile(fileExtName))    
  102.             return null;    
  103.         if ((new File(outFileName)).exists()) {    
  104.             System.out.println("file [" + outFileName + "] already exists");    
  105.             throw new Exception();    
  106.         }    
  107.         Image src = ImageIO.read(_file);    
  108.         int w = src.getWidth(null);    
  109.         int h = src.getHeight(null);    
  110.     
  111.         // 確定目標圖片的大小    
  112.         int nw = w;    
  113.         int nh = h;    
  114.         if (createType == CREATENEWIMAGETYPE_0)    
  115.             ;    
  116.         else if (createType == CREATENEWIMAGETYPE_1) {    
  117.             nw = newW;    
  118.             nh = newH;    
  119.         } else if (createType == CREATENEWIMAGETYPE_2) {    
  120.             nw = newW;    
  121.             nh = (int) ((double) h / (double) w * nw);    
  122.         } else if (createType == CREATENEWIMAGETYPE_3) {    
  123.             nh = newH;    
  124.             nw = (int) ((double) w / (double) h * nh);    
  125.         } else if (createType == CREATENEWIMAGETYPE_4) {    
  126.             if ((double) w / (double) h >= (double) newW / (double) newH) {    
  127.                 nh = newH;    
  128.                 nw = (int) ((double) w / (double) h * nh);    
  129.             } else {    
  130.                 nw = newW;    
  131.                 nh = (int) ((double) h / (double) w * nw);    
  132.             }    
  133.         } else if (createType == CREATENEWIMAGETYPE_5) {    
  134.             if ((double) w / (double) h <= (double) newW / (double) newH) {    
  135.                 nh = newH;    
  136.                 nw = (int) ((double) w / (double) h * nh);    
  137.             } else {    
  138.                 nw = newW;    
  139.                 nh = (int) ((double) h / (double) w * nw);    
  140.             }    
  141.         }    
  142.     
  143.         // 構造目標圖片    
  144.         BufferedImage tag = new BufferedImage(nw, nh,    
  145.                 BufferedImage.TYPE_INT_RGB);    
  146.     
  147.         // 得到目標圖片輸出流    
  148.         FileOutputStream out = new FileOutputStream(outFileName);    
  149.     
  150.         // 根據需求畫出目標圖片 方式1    
  151.         tag.getGraphics().drawImage(src, 00, nw, nh, null);    
  152.     
  153.         // 將畫好的目標圖輸出到輸出流 方式1    
  154.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
  155.         encoder.encode(tag);    
  156.         out.close();    
  157.         return outFileName;    
  158.     }    
  159.     
  160.     public static boolean checkImageFile(String extName) {    
  161.     
  162.         if ("jpg".equalsIgnoreCase(extName))    
  163.             return true;    
  164.         if ("gif".equalsIgnoreCase(extName))    
  165.             return true;    
  166.         if ("bmp".equalsIgnoreCase(extName))    
  167.             return true;    
  168.         if ("jpeg".equalsIgnoreCase(extName))    
  169.             return true;    
  170.         if ("png".equalsIgnoreCase(extName))    
  171.             return true;    
  172.         return false;    
  173.     }    
  174.     
  175. }  </pre>  
  176. <p><br>  
  177. </p>  
  178. <p><br>  
  179. </p>  
  180. <p><br>  
  181. </p> 

發佈了22 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章