Java代码实现对图片压缩大小

这是一个Java代码示例,用于压缩图片的大小。通过指定新的宽度和高度,程序使用Graphics2D和ImageIO进行图像处理和输出,实现了图片的尺寸压缩。
摘要由CSDN通过智能技术生成

原文地址:http://blog.csdn.net/personbing/article/details/50790472

  1. package com.wxb.demo;  
  2.   
  3. import java.awt.Graphics2D;  
  4. import java.awt.Image;  
  5. import java.awt.image.BufferedImage;  
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileNotFoundException;  
  9. import java.io.IOException;  
  10. import java.util.HashMap;  
  11. import java.util.Map;  
  12.   
  13. import javax.imageio.ImageIO;  
  14.   
  15. /** 
  16.  * @author wxb 
  17.  * @date 2011-01-14  
  18.  * @versions 1.0 图片压缩工具类 提供的方法中可以设定生成的 
  19.  * 缩略图片的大小尺寸等 
  20.  */  
  21. public class PictureChangeSize {  
  22.       
  23.       
  24.     public static void main(String args[]) {  
  25.         try {  
  26.             Map<Integer, String> map = readfile("E:/中华精品网/网站页面/网站页面源码/中华文化精品网2.14/中华文化精品网/images"null);  
  27.             for (int i = 0; i < map.size(); i++) {  
  28.                 System.out.println(map.get(i) + " ==" + i);  
  29.                 System.out.println();  
  30.                 String oldpath = map.get(i);  
  31.                 compressImage(map.get(i), "E:/中华精品网/demo/_" + i + ".png"10075);  
  32.             }  
  33.         } catch (Exception ex) {  
  34.         }  
  35.         System.out.println("ok");  
  36.     }  
  37.       
  38.       
  39.     /**  
  40.      * 图片文件读取  
  41.      * @param srcImgPath  
  42.      * @return  
  43.      */  
  44.     private static BufferedImage InputImage(String srcImgPath) {  
  45.         BufferedImage srcImage = null;  
  46.         try {  
  47.             FileInputStream in = new FileInputStream(srcImgPath);  
  48.             srcImage = javax.imageio.ImageIO.read(in);  
  49.         } catch (IOException e) {  
  50.             System.out.println("读取图片文件出错!" + e.getMessage());  
  51.             e.printStackTrace();  
  52.         }  
  53.         return srcImage;  
  54.     }  
  55.   
  56.     /** 
  57.      * * 将图片按照指定的图片尺寸压缩 
  58.      * @param srcImgPath :源图片路径 
  59.      * @param outImgPath  :输出的压缩图片的路径  
  60.      * @param new_w :压缩后的图片宽 
  61.      * @param new_h :压缩后的图片高 
  62.      */  
  63.     public static void compressImage(String srcImgPath, String outImgPath,  
  64.             int new_w, int new_h) {  
  65.         BufferedImage src = InputImage(srcImgPath);  
  66.         disposeImage(src, outImgPath, new_w, new_h);  
  67.     }  
  68.   
  69.     /** 
  70.      * 指定长或者宽的最大值来压缩图片  
  71.      * @param srcImgPath :源图片路径  
  72.      * @param outImgPath :输出的压缩图片的路径  
  73.      * @param maxLength :长或者宽的最大值 
  74.      */  
  75.     public static void compressImage(String srcImgPath, String outImgPath,  
  76.             int maxLength) {  
  77.         // 得到图片  
  78.         BufferedImage src = InputImage(srcImgPath);  
  79.         if (null != src) {  
  80.             int old_w = src.getWidth();  
  81.             // 得到源图宽  
  82.             int old_h = src.getHeight();  
  83.             // 得到源图长  
  84.             int new_w = 0;  
  85.             // 新图的宽  
  86.             int new_h = 0;  
  87.             // 新图的长  
  88.             // 根据图片尺寸压缩比得到新图的尺寸  
  89.             if (old_w > old_h) {  
  90.                 // 图片要缩放的比例  
  91.                 new_w = maxLength;  
  92.                 new_h = (int) Math.round(old_h * ((float) maxLength / old_w));  
  93.             } else {  
  94.                 new_w = (int) Math.round(old_w * ((float) maxLength / old_h));  
  95.                 new_h = maxLength;  
  96.             }  
  97.             disposeImage(src, outImgPath, new_w, new_h);  
  98.         }  
  99.     }  
  100.   
  101.     /**  
  102.      * 处理图片 
  103.      * @param src  
  104.      * @param outImgPath  
  105.      * @param new_w  
  106.      * @param new_h  
  107.      */  
  108.     private synchronized static void disposeImage(BufferedImage src,  
  109.             String outImgPath, int new_w, int new_h) {  
  110.         // 得到图片  
  111.         int old_w = src.getWidth();  
  112.         // 得到源图宽  
  113.         int old_h = src.getHeight();  
  114.         // 得到源图长  
  115.         BufferedImage newImg = null;  
  116.         // 判断输入图片的类型  
  117.         switch (src.getType()) {  
  118.         case 13:  
  119.             // png,gifnewImg = new BufferedImage(new_w, new_h,  
  120.             // BufferedImage.TYPE_4BYTE_ABGR);  
  121.             break;  
  122.         default:  
  123.             newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);  
  124.             break;  
  125.         }  
  126.         Graphics2D g = newImg.createGraphics();  
  127.         // 从原图上取颜色绘制新图  
  128.         g.drawImage(src, 00, old_w, old_h, null);  
  129.         g.dispose();  
  130.         // 根据图片尺寸压缩比得到新图的尺寸  
  131.         newImg.getGraphics().drawImage(  
  132.                 src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 00,  
  133.                 null);  
  134.         // 调用方法输出图片文件  
  135.         OutImage(outImgPath, newImg);  
  136.     }  
  137.   
  138.     /** 
  139.      * 将图片文件输出到指定的路径,并可设定压缩质量  
  140.      * @param outImgPath  
  141.      * @param newImg  
  142.      * @param per 
  143.      */  
  144.     private static void OutImage(String outImgPath, BufferedImage newImg) {  
  145.         // 判断输出的文件夹路径是否存在,不存在则创建  
  146.         File file = new File(outImgPath);  
  147.         if (!file.getParentFile().exists()) {  
  148.             file.getParentFile().mkdirs();  
  149.         }// 输出到文件流  
  150.         try {  
  151.             ImageIO.write(newImg,  
  152.                     outImgPath.substring(outImgPath.lastIndexOf(".") + 1),  
  153.                     new File(outImgPath));  
  154.         } catch (FileNotFoundException e) {  
  155.             e.printStackTrace();  
  156.         } catch (IOException e) {  
  157.             e.printStackTrace();  
  158.         }  
  159.     }  
  160.   
  161.     public static Map<Integer, String> readfile(String filepath,  
  162.             Map<Integer, String> pathMap) throws Exception {  
  163.         if (pathMap == null) {  
  164.             pathMap = new HashMap<Integer, String>();  
  165.         }  
  166.   
  167.         File file = new File(filepath);  
  168.         // 文件  
  169.         if (!file.isDirectory()) {  
  170.             pathMap.put(pathMap.size(), file.getPath());  
  171.   
  172.         } else if (file.isDirectory()) { // 如果是目录, 遍历所有子目录取出所有文件名  
  173.             String[] filelist = file.list();  
  174.             for (int i = 0; i < filelist.length; i++) {  
  175.                 File readfile = new File(filepath + "/" + filelist[i]);  
  176.                 if (!readfile.isDirectory()) {  
  177.                     pathMap.put(pathMap.size(), readfile.getPath());  
  178.   
  179.                 } else if (readfile.isDirectory()) { // 子目录的目录  
  180.                     readfile(filepath + "/" + filelist[i], pathMap);  
  181.                 }  
  182.             }  
  183.         }  
  184.         return pathMap;  
  185.     }  
  186. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值