1
zj
2025-04-01 610c76c7d87d0140a21bb10d644597f615d9d8c5
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package util;
 
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
public class ImageCut {
    /**
     * 
     * 缩放图像
     * 
     * @param srcImageFile源图像文件地址
     * 
     * @param result缩放后的图像地址
     * 
     * @param scale缩放比例
     * 
     * @param flag缩放选择
     *            :true 放大; false 缩小;
     */
 
    public static void scale(String srcImageFile, String result, String type,double scale,
 
    boolean flag) {
        try {
            BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件
 
            int width = src.getWidth(); // 得到源图宽
 
            int height = src.getHeight(); // 得到源图长
 
            if (flag) {// 放大
 
                width = Double.valueOf((width * scale)).intValue();
 
                height = Double.valueOf((height * scale)).intValue();
 
            } else {// 缩小
 
                width = Double.valueOf((width / scale)).intValue();
 
                height = Double.valueOf((height / scale)).intValue();
 
            }
 
            Image image = src.getScaledInstance(width, height,
 
            Image.SCALE_DEFAULT);
 
            BufferedImage tag = new BufferedImage(width, height,
 
            BufferedImage.TYPE_INT_RGB);
 
            Graphics g = tag.getGraphics();
 
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
 
            g.dispose();
 
            ImageIO.write(tag, type, new File(result));// 输出到文件流
 
        } catch (IOException e) {
 
            e.printStackTrace();
 
        }
 
    }
 
    /**
     * 
     * 图像切割
     * 
     * @param srcImageFile源图像地址
     * 
     * @param descDir切片目标文件夹
     * 
     * @param destWidth目标切片宽度
     * 
     * @param destHeight目标切片高度
     */
 
    public static void cut(String srcImageFile, String descDir, int destWidth,
 
    int destHeight) {
 
        try {
 
            Image img;
 
            ImageFilter cropFilter; // 读取源图像
 
            BufferedImage bi = ImageIO.read(new File(srcImageFile));
 
            int srcWidth = bi.getHeight(); // 源图宽度
 
            int srcHeight = bi.getWidth(); // 源图高度
 
            if (srcWidth > destWidth && srcHeight > destHeight) {
 
                Image image = bi.getScaledInstance(srcWidth, srcHeight,
 
                Image.SCALE_DEFAULT);
 
                destWidth = 200; // 切片宽度
 
                destHeight = 150; // 切片高度
 
                int cols = 0; // 切片横向数量
 
                int rows = 0; // 切片纵向数量
 
                // 计算切片的横向和纵向数量
 
                if (srcWidth % destWidth == 0) {
 
                    cols = srcWidth / destWidth;
 
                } else {
 
                    cols = (int) Math.floor(srcWidth / destWidth) + 1;
 
                }
 
                if (srcHeight % destHeight == 0) {
 
                    rows = srcHeight / destHeight;
 
                } else {
 
                    rows = (int) Math.floor(srcHeight / destHeight) + 1;
 
                }
 
                // 循环建立切片
 
                // 改进的想法:是否可用多线程加快切割速度
 
                for (int i = 0; i < rows; i++) {
 
                    for (int j = 0; j < cols; j++) {
 
                        // 四个参数分别为图像起点坐标和宽高
 
                        // 即: CropImageFilter(int x,int y,int width,int height)
 
                        cropFilter = new CropImageFilter(j * 200, i * 150,
 
                        destWidth, destHeight);
 
                        img = Toolkit.getDefaultToolkit().createImage(
 
                        new FilteredImageSource(image.getSource(),
 
                        cropFilter));
 
                        BufferedImage tag = new BufferedImage(destWidth,
 
                        destHeight, BufferedImage.TYPE_INT_RGB);
 
                        Graphics g = tag.getGraphics();
 
                        g.drawImage(img, 0, 0, null); // 绘制缩小后的图
 
                        g.dispose();
 
                        // 输出为文件
 
                        ImageIO.write(tag, "JPEG", new File(descDir
 
                        + "pre_map_" + i + "_" + j + ".jpg"));
 
                    }
 
                }
 
            }
 
        } catch (Exception e) {
 
            e.printStackTrace();
 
        }
 
    }
 
    // 图像类型转换GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
 
    public static void convert(String source, String result) {
 
        try {
 
            File f = new File(source);
 
            f.canRead();
 
            f.canWrite();
 
            BufferedImage src = ImageIO.read(f);
 
            ImageIO.write(src, "JPG", new File(result));
 
        } catch (Exception e) {
 
            // TODO Auto-generated catch block
 
            e.printStackTrace();
 
        }
 
    }
 
    // 彩色转为黑白
 
    public static void gray(String source, String result) {
 
        try {
 
            BufferedImage src = ImageIO.read(new File(source));
 
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
 
            ColorConvertOp op = new ColorConvertOp(cs, null);
 
            src = op.filter(src, null);
 
            ImageIO.write(src, "JPEG", new File(result));
 
        } catch (IOException e) {
 
            e.printStackTrace();
 
        }
 
    }
 
    public static void main(String[] args) {
        double s = 1024;
        int e = 500;
        double scale = s/e;
         String src = "E:\\apache-tomcat-6.0.37\\wtpwebapps\\mcp0917\\upload\\images\\2c9287a341ca74320141ca76181e0001\\5453797376338493.jpg";
         scale(src,"E:\\apache-tomcat-6.0.37\\wtpwebapps\\mcp0917\\upload\\images\\2c9287a341ca74320141ca76181e0001\\5453797376338493.jpg","jpg",scale,false);
 
    }
 
}