1
zj
2025-06-18 69d7ae376a58c399c97ee42e5ff7a13860cb2b7e
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
103
104
105
106
107
108
109
110
111
112
113
package util;
 
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
public class ImgTool {
    //private BufferedImage subImg;
 
    /**
     * 截图
     * 
     * @param srcPath
     * @param startX
     * @param startY
     * @param width
     * @param height
     */
    /*public void cut(String srcPath, int startX, int startY, int width,
            int height) {
        try {
            BufferedImage bufImg = ImageIO.read(new File(srcPath));
            int w = bufImg.getWidth();
            int h = bufImg.getHeight();
            //如果宽比高大,并且宽大于400,裁减的坐标等比例放大
            if(w>=h && w>400){
                startX = startX*w/400;
                startY = startY*w/400;
                width = width*w/400;
                height = height*w/400;
            }
            //如果宽比高小,并且高大于400,裁减的坐标等比例放大
            if(w<h && h>400){
                startX = startX*h/400;
                startY = startY*h/400;
                width = width*h/400;
                height = height*h/400;
            }
            subImg = bufImg.getSubimage(startX, startY, width, height);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
 
    /**
     * 保存截图。
     * 
     * @param bufImg
     * @param imgType
     * @param tarPath
     */
    public void save(String imgType, String srcPath, String tarPath, int startX, int startY, int width, int height, int maxWidth) {
        try {
            BufferedImage bufImg = ImageIO.read(new File(srcPath));
            int oldWidth = bufImg.getWidth();
            int oldHieght = bufImg.getHeight();
            int w = bufImg.getWidth();
            int h = bufImg.getHeight();
            //如果宽比高大,并且宽大于maxWidth,裁减的坐标等比例放大
            if(w>=h && w>maxWidth){
                startX = startX*w/maxWidth;
                startY = startY*w/maxWidth;
                width = width*w/maxWidth;
                if(width>oldWidth){
                    width = oldWidth;
                }
                height = height*w/maxWidth;
                if(height>oldHieght){
                    height = oldHieght;
                }
            }
            //如果宽比高小,并且高大于maxWidth,裁减的坐标等比例放大
            if(w<h && h>maxWidth){
                startX = startX*h/maxWidth;
                startY = startY*h/maxWidth;
                width = width*h/maxWidth;
                if(width>oldWidth){
                    width = oldWidth;
                }
                height = height*h/maxWidth;
                if(height>oldHieght){
                    height = oldHieght;
                }
            }
            
            BufferedImage subImg = bufImg.getSubimage(startX, startY, width, height);
            
            File file = new File(tarPath);
            if(!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            ImageIO.write(subImg, imgType, file);
            
            //宽如果大于640,则按比例缩减
            if(subImg.getWidth()>640){
                double scale = (double)subImg.getWidth()/640;
                width = (int) (width/scale);
                height = (int) (height/scale);
                BufferedImage tempImg = new BufferedImage(width, height,
                        BufferedImage.TYPE_INT_RGB);
                tempImg.getGraphics().drawImage(
                        subImg.getScaledInstance(width, height,
                                Image.SCALE_FAST), 0, 0, null);
                ImageIO.write(tempImg, imgType,file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}