Helius
2020-12-15 2914588a65371a3ce43f678cde0a26cd8da26611
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.matrix.system.hive.plugin.util;
 
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
 
/**
 * 
 * @description 照片工具类       
 * @author Matrix-J
 * @data 2015年8月6日 下午7:16:18
 */
public class ImageUtil {
 
 
 
 
    /**
     * 把图片文件压缩为JPG文件
     * @description 
     * @data 2015年8月6日 下午7:17:59 
     * @author Administrator
    ·* @author JYY    
     * @param file 源文件
     * @param newHeight 新宽度
     * @param newWidth 新高度
     * @param outputDir 输出目录
     * @param outputFileName  输出文件名称
     */
    public static void compressionImg(File file, int newHeight, int newWidth,
            String outputDir, String outputFileName) {
        try {
 
            Image img = ImageIO.read(file);
            // // 判断图片格式是否正确
            BufferedImage tag = new BufferedImage(newWidth, newHeight,
                    BufferedImage.TYPE_INT_RGB);
            /*
             * Image.SCALE_SMOOTH 的缩略算生成缩略图片的平滑度优先级比速度生成的图片质量比较好 但度慢
             */
            tag.getGraphics().drawImage(
                    img.getScaledInstance(newWidth, newHeight,
                            Image.SCALE_SMOOTH), 0, 0, null);
            FileOutputStream out = new FileOutputStream(outputDir
                    + outputFileName);
            // JPEGImageEncoder可用于其他图片类型的转
            ImageIO.write(tag, "jpg", out);
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
 
    
 
    
 
    
    
 
  
 
   
}