[测评系统]--测评系统核心代码库
林致杰
2022-07-25 6d42ef0db15cce06cfbd178e07287e5d54ee617d
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
package com.ots.project.tool.exam;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
@Slf4j
public class PicUtils {
    public static void main(String[] args) {
        boolean flag = PicUtils.commpressPicForScale("/Users/shawnli/Downloads/1.jpg",
                "/Users/shawnli/Downloads/2.jpg", 20, 0.5); 
    }
    
    public static boolean commpressPicForScale(String srcPath, String desPath,
                                               long desFileSize, double accuracy) {
        if ("".equals(srcPath.trim()) || "".equals(srcPath.trim())) {
            return false;
        }
        try {
            File srcFile = new File(srcPath);
            if (!srcFile.exists()) {
                return false;
            }
            
            long srcFileSizeJPG = srcFile.length();
            
            if (srcFileSizeJPG <= desFileSize * 1024) {
                byte[] b = new byte[1024];
                int a = 0;
                
                File desFile = new File(desPath);
                FileInputStream fis = new FileInputStream(srcFile);
                FileOutputStream fos = new FileOutputStream(desFile);
                while ((a = fis.read(b)) != -1) {
                    fos.write(b, 0, a);
                }
                return true;
            }
            
            Thumbnails.of(srcPath).scale(1f).toFile(desPath);
            
            commpressPicCycle(desPath, desFileSize, accuracy);
        } catch (Exception e) {
            log.error("失败的文件" + srcPath);
            log.error(e.getMessage());
            return false;
        }
        return true;
    }
    private static void commpressPicCycle(String desPath, long desFileSize,
                                          double accuracy) throws IOException {
        File srcFileJPG = new File(desPath);
        long srcFileSizeJPG = srcFileJPG.length();
        
        if (srcFileSizeJPG <= desFileSize * 1024) {
            return;
        }
        
        BufferedImage bim = ImageIO.read(srcFileJPG);
        int srcWdith = bim.getWidth();
        int srcHeigth = bim.getHeight();
        int desWidth = new BigDecimal(srcWdith).multiply(
                new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(srcHeigth).multiply(
                new BigDecimal(accuracy)).intValue();
        Thumbnails.of(desPath).size(desWidth, desHeight)
                .outputQuality(accuracy).toFile(desPath);
        commpressPicCycle(desPath, desFileSize, accuracy);
    }
    
    public static boolean commpressPicForScale(String fileOriginalPath, String filePath) {
        File fileSrcDir = new File(fileOriginalPath);
        File fileDesDir = new File(filePath);
        
        if (!fileSrcDir.isDirectory() || !fileDesDir.isDirectory()) {
            return false;
        }
        for (File file : fileSrcDir.listFiles()) {
            commpressPicForScale(file.getPath(),
                    filePath + file.getName(), 5, 0.5);
        }
        return true;
    }
}