935090232@qq.com
2021-01-06 7b9cb4464cf7a6db758214c5681b0ecdf01e3db4
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.matrix.system.hive.plugin.util;
 
import com.matrix.core.tools.DateUtil;
import sun.misc.BASE64Decoder;
 
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
 
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();
        }
    }
 
 
    public static String base64ToFile(String base64Str, String savePath, String fileName) {
        String fileDir = DateUtil.dateToString(new Date(), DateUtil.DATE_FORMAT_NO_SPLITE_DD);
 
        File dir = new File(savePath + fileDir);
        if (!dir.isDirectory()) {
            dir.mkdir();
        }
 
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        String filePathAndName = savePath + fileDir + File.separator + fileName;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bfile = new byte[0];
        try {
            bfile = decoder.decodeBuffer(base64Str);
            file = new File(filePathAndName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return filePathAndName;
    }
    
 
    
 
    
    
 
  
 
   
}