zhubaomin
2025-04-07 398a79dd0e77737d548bfaf4f8ef72d3e753aa0b
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
package com.dy.pipIrrWebFile.util;
 
import net.coobird.thumbnailator.Thumbnails;
 
import javax.imageio.ImageIO;
 
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
 
/**
 * 生成图片的缩略图
 */
public class ZipImg {
    public static InputStream zipToJpg(File file, int xSize, int ySize) throws Exception{
        return zip(file, "jpg", xSize, ySize) ;
    }
 
    public static InputStream zipToJpg(BufferedImage buf, int xSize, int ySize) throws Exception{
        return zip(buf, "jpg", xSize, ySize) ;
    }
 
    public static InputStream zipToPng(File file, int xSize, int ySize) throws Exception{
        return zip(file, "png", xSize, ySize) ;
    }
 
    public static InputStream zipToPng(BufferedImage buf, int xSize, int ySize) throws Exception{
        return zip(buf, "png", xSize, ySize) ;
    }
 
    public static void zipToFile(File file, File toFile, int xSize, int ySize) throws Exception{
        Thumbnails.of(file).size(xSize, ySize).outputQuality(0.5f).toFile(toFile);//0f-1f 质量越高
    }
 
 
    private static InputStream zip(File file, String type, int xSize, int ySize) throws Exception{
        BufferedImage bi = Thumbnails.of(file).size(xSize, ySize).outputQuality(1f).asBufferedImage();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bi, type, os);
        InputStream in = new ByteArrayInputStream(os.toByteArray());
        bi = null ;
        os = null ;
        return in ;
    }
 
    private static InputStream zip(BufferedImage buf, String type, int xSize, int ySize) throws Exception{
        BufferedImage bi = Thumbnails.of(buf).size(xSize, ySize).outputQuality(1f).asBufferedImage();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bi, type, os);
        InputStream in = new ByteArrayInputStream(os.toByteArray());
        bi = null ;
        os = null ;
        return in ;
    }
    
    public static void main(String[] args){
        File f = new File("D:/test.jpg") ;
        String name = f.getName() ;
        String path = f.getPath() ;
        System.out.println(name);
        System.out.println(path);
        
        int index = path.lastIndexOf(".") ;
        String s1 = path.substring(0, index) ;
        String s2 = path.substring(index) ;
        System.out.println(s1);
        System.out.println(s2);
        
        String newFilePath = s1 + "_" + s2 ;
        try {
            InputStream input = zipToJpg(f, 400, 400) ;
            new FileUtil().saveFile(newFilePath, input) ;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
}