用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

批量图片压缩

2013-08-19 作者: wcj举报

[java]代码库

package com.cnc.wsad.taskgenerator.frame;
 
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import  java.io.File;
import java.io.IOException;
 
import  javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public   class  picturetest  {
     
    public static void getFixedIcon(String filePath, int width, int height) throws Exception{ 
        File f = new File(filePath);  
        BufferedImage bi = ImageIO.read(f); 
        double wRatio = (new Integer(width)).doubleValue() / bi.getWidth(); //宽度的比例 
        double hRatio = (new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例 
        Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小 
        AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,hRatio),null);   //设置图像的缩放比例 
        image = op.filter(bi,null); 
        int lastLength = filePath.lastIndexOf(".");    
        String subFilePath = filePath.substring(0,lastLength);  //得到图片输出路径 
        String fileType = filePath.substring(lastLength);  //图片类型 
        File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType); 
        Icon ret = null
        try
         ImageIO.write((BufferedImage)image, "jpg", zoomFile);  
         ret = new ImageIcon(zoomFile.getPath());  
        }catch (Exception e){ 
         e.printStackTrace(); 
        }  
    
       
    public static void getfiled(String path){
        File file = new File(path);
          File [] files = file.listFiles();
          for (int i = 0; i < files.length; i++)
          {
              File file1 = files[i];
                try {
                    int height = (ImageIO.read(file1)).getHeight(null);
                    int width = (ImageIO.read(file1)).getWidth(null);
                     
                    String tts = path+"/"+file1.getName();   //根据后缀判断
                    System.out.println(tts+"||"+height);
                      getFixedIcon(tts,width,height);
                      
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
          }
    }
     
  public static void main(String arg[])  {
      getfiled("f:/游玩");
  }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...