[Java] 批量图片压缩 →→→→→进入此内容的聊天室

来自 , 2021-02-28, 写在 Java, 查看 121 次.
URL http://www.code666.cn/view/677e0972
  1. package com.cnc.wsad.taskgenerator.frame;
  2.  
  3. import java.awt.Image;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.image.AffineTransformOp;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.IOException;
  9.  
  10. import javax.imageio.ImageIO;
  11. import javax.swing.Icon;
  12. import javax.swing.ImageIcon;
  13. public   class  picturetest  {
  14.        
  15.         public static void getFixedIcon(String filePath, int width, int height) throws Exception{  
  16.             File f = new File(filePath);  
  17.             BufferedImage bi = ImageIO.read(f);  
  18.             double wRatio = (new Integer(width)).doubleValue() / bi.getWidth(); //宽度的比例  
  19.             double hRatio = (new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例  
  20.             Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //设置图像的缩放大小  
  21.             AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,hRatio),null);   //设置图像的缩放比例  
  22.             image = op.filter(bi,null);  
  23.             int lastLength = filePath.lastIndexOf(".");    
  24.             String subFilePath = filePath.substring(0,lastLength);  //得到图片输出路径  
  25.             String fileType = filePath.substring(lastLength);  //图片类型  
  26.             File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType);  
  27.             Icon ret = null;  
  28.             try{  
  29.              ImageIO.write((BufferedImage)image, "jpg", zoomFile);  
  30.              ret = new ImageIcon(zoomFile.getPath());  
  31.             }catch (Exception e){  
  32.              e.printStackTrace();  
  33.             }  
  34.         }  
  35.          
  36.         public static void getfiled(String path){
  37.                 File file = new File(path);
  38.               File [] files = file.listFiles();
  39.               for (int i = 0; i < files.length; i++)
  40.               {
  41.                   File file1 = files[i];
  42.                                 try {
  43.                                         int height = (ImageIO.read(file1)).getHeight(null);
  44.                                     int width = (ImageIO.read(file1)).getWidth(null);
  45.                                    
  46.                                     String tts = path+"/"+file1.getName();   //根据后缀判断
  47.                                     System.out.println(tts+"||"+height);
  48.                                   getFixedIcon(tts,width,height);
  49.                                  
  50.                                 } catch (IOException e) {
  51.                                         e.printStackTrace();
  52.                                 } catch (Exception e) {
  53.                                         e.printStackTrace();
  54.                                 }
  55.               }
  56.         }
  57.        
  58.   public static void main(String arg[])  {
  59.           getfiled("f:/游玩");
  60.   }
  61. }
  62.  

回复 "批量图片压缩"

这儿你可以回复上面这条便签

captcha