ke
2024-05-13 d31e928394ad711a86a3d93caa6470b1abb96a39
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package cn.gistack.sm.skPanorama.utils;
 
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
 
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.core.Info;
import org.im4java.core.InfoException;
import org.im4java.process.ProcessStarter;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
 
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
 
import static java.awt.SystemColor.info;
 
public class Img4JavaUtil {
 
    static {
        //判断是否是windows系统
        if(OSinfo.isWindows()) {
            System.out.println("------------ START 初始化ImageMagickPath -------------");
            ProcessStarter.setGlobalSearchPath("D:/im4java/ImageMagick-7.1.1-Q16-HDRI");
            System.out.println("------------ END 初始化ImageMagickPath -------------");
        } else if (OSinfo.isLinux()) {
            System.out.println("------------ START 初始化ImageMagickPath -------------");
//            ProcessStarter.setGlobalSearchPath("D:/im4java/ImageMagick-7.1.1-Q16-HDRI");
            System.out.println("------------ END 初始化ImageMagickPath -------------");
        }
    }
    /**
     * 裁剪图片
     * @param inImgPath 源图片地址
     * @param outImgPath 目标图片地址
     * @param width 宽
     * @param height 高
     * @param x 起点横坐标
     * @param y 起点纵坐标
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void cropImg(String inImgPath,String outImgPath,Integer width,Integer height,Integer x,Integer y)
        throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //宽  高 起点横坐标 起点纵坐标
        operation.crop(width, height, x, y);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }
 
    /**
     * 裁剪图片
     * @param inImgPath 源图片地址
     * @param outImgPath 目标图片地址
     * @param width 宽
     * @param height 高
     * @param x 起点横坐标
     * @param y 起点纵坐标
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
//    public void cropImg(String inImgPath,String outImgPath,Integer width,Integer height,Integer x,Integer y)
//        throws IOException, InterruptedException, IM4JavaException{
//        ConvertCmd cmd = new ConvertCmd();
//        IMOperation operation = new IMOperation();
//        operation.addImage(inImgPath);
//        //宽  高 起点横坐标 起点纵坐标
//        operation.crop(width, height, x, y);
//        operation.addImage(outImgPath);
//        cmd.run(operation);
//    }
 
    /**
     * 裁剪中心化图片
     * @param inImgPath 源图片地址
     * @param outImgPath 目标图片地址
     * @param width 宽
     * @param height 高
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public void cropCenterImg(String inImgPath,String outImgPath,Integer width,Integer height) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
 
        Info info = new Info(inImgPath);
        Integer init_width = info.getImageWidth();
        Integer init_height = info.getImageHeight();
 
        // 如果裁剪的宽高都大于原始图片宽高 则先等比放大再裁剪
        if(width>init_width || height>init_height){
            // 等比放大
            Integer temp_width = width;
            if(init_height<init_width){
                // 如果原图宽大于高 则以高为准放大
                temp_width = null;
            }
            operation.resize(temp_width, height);
        }
        operation.addImage(inImgPath);
        //宽  高 起点横坐标 起点纵坐标
        operation.gravity("center");
        operation.crop(width, height, 0, 0);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }
 
    /**
     * 得到图片的信息
     * @throws InfoException
     */
    public static String getImgInfo(String inImgPath) throws InfoException{
        Info info = new Info(inImgPath);
//        System.out.println(info.getImageHeight());
//        System.out.println(info.getImageWidth());
        return info.getImageWidth()+"x"+info.getImageHeight();
    }
 
    /**
     * 得到图片的信息
     * @throws InfoException
     */
    public static HashMap<String,Integer> getImgInfoRT( File imageFile) throws  IOException {
        HashMap stringHashMap = new HashMap<String,Integer>();
        try {
            ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageFile);
            final Iterator<ImageReader> readers = ImageIO.getImageReaders(imageInputStream);
            if(readers.hasNext()){
                ImageReader reader = readers.next();
                reader.setInput(imageInputStream,true);
                int width = reader.getWidth(0);
                int height = reader.getHeight(0);
 
                stringHashMap.put("width",width);
                stringHashMap.put("height",height);
                imageInputStream.close();
 
            }
        } catch (IOException e) {
            System.err.println("IOException "+e.getMessage());
        }
 
        return stringHashMap;
    }
 
    /**
     * 等比缩放图片
     * @param inImgPath
     * @param outImgPath
     * @param width 宽度不为空时 以宽度为准,为空时以高度为准(不能同时为空)
     * @param height
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void resizeImg(String inImgPath,String outImgPath,Integer width,Integer height) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //等比缩放图片
        operation.resize(width, height);//高度为null则按宽度缩放
        operation.addImage(outImgPath);
        cmd.run(operation);
    }
 
 
    /**
     * 等比缩放   (使用像素平均缩小)
     * @param inImgPath
     * @param outImgPath
     * @param width
     * @param height
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void scaleImg(String inImgPath,String outImgPath,Integer width,Integer height) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //缩略图
        operation.scale(width,height);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }
 
    /**
     * 把原图裁剪成正方形
     * @param inImgPath
     * @param operation
     * @return
     * @throws IM4JavaException
     */
    public static IMOperation getCenterSquare(String inImgPath,IMOperation operation)throws IM4JavaException{
        Info info = new Info(inImgPath);
        Integer init_width = info.getImageWidth();
        Integer init_height = info.getImageHeight();
        if (init_width > init_height) {
            init_width = init_height;
        } else if (init_width < init_height) {
            init_height = init_width;
        }
        operation.gravity("center");
        operation.crop(init_width, init_height,0,0);
        return operation;
    }
 
 
    /**
     * 缩略图 (专门用于将非常大的图像缩小为小缩略图)
     * @param inImgPath
     * @param outImgPath
     * @param width
     * @param height
     * @param editType = c 取中心化缩略图,否则取原等比缩略图
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public void thumbnailImg(String inImgPath,String outImgPath,Integer width,Integer height,String editType) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //取中心化或者原图同比
        if(editType!=null && editType.equals("c")) {
            //分析宽高 先原图等比生成缩略图(缩略规则按原图大的一边为准取值)
            Info info = new Info(inImgPath);
            Integer init_width = info.getImageWidth();
            Integer init_height = info.getImageHeight();
            double width_v = (double)init_width/width;
            double height_v = (double)init_height/height;
            //原图是正方形
            /*if(init_width == init_height){
                operation.thumbnail(width,height);
                //原图是宽大于高的长方形
            }else */if(init_width >= init_height){
                if (width_v >= height_v) {
                    operation.thumbnail(null,height);
                } else if (width_v < height_v) {
                    operation.thumbnail(width,null);
                }
                //原图是高大于宽的长方形
            }else if(init_width < init_height){if (width_v > height_v) {
                operation.thumbnail(null,height);
            } else if (width_v <= height_v) {
                operation.thumbnail(width,null);
            }
            }
 
            //再中心化截图
            operation.gravity("center");
            operation.crop(width, height,0,0);
        }else{
            operation.thumbnail(width,height);
        }
        operation.addImage(outImgPath);
        cmd.run(operation);
    }
 
    /**
     * 旋转图片
     * @param inImgPath
     * @param outImgPath
     * @param x 旋转角度
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public void rotateImg(String inImgPath,String outImgPath,Double x) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        operation.rotate(x);
        operation.addImage(outImgPath);
        cmd.run(operation);
    }
 
    /**
     * 缩略图 (原图画质缩略)
     * @param inImgPath
     * @param outImgPath
     * @param width
     * @param height
     * @param editType
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public void sampleImg(String inImgPath,String outImgPath,Integer width,Integer height,String editType) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        //取中心化或者原图同比
        if(editType!=null && editType.equals("center")) {
            getCenterSquare(inImgPath,operation);
        }
        operation.sample(width,height);
//        operation.quality(5.0); //设置生成图片质量
        operation.addImage(outImgPath);
        cmd.run(operation);
    }
 
    /**
     * 将图片变成黑白图片
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public void monochrome(String inImgPath,String outImgPath) throws IOException, InterruptedException, IM4JavaException{
        ConvertCmd cmd = new ConvertCmd();
        IMOperation operation = new IMOperation();
        operation.addImage(inImgPath);
        operation.monochrome();
        operation.addImage(outImgPath);
        cmd.run(operation);
    }
 
    /**
     * 给图片加水印
     * @param: srcPath源图片路径
     */
    public static void addImgText(String inImgPath,String outImgPath,String str)throws IOException, InterruptedException, IM4JavaException{
        IMOperation operation = new IMOperation();
        operation.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8")
            .draw("text 5,5 "+str);
        operation.encoding("UTF-8");
        operation.addImage();
        operation.addImage();
        ConvertCmd convert = new ConvertCmd();
        convert.run(operation, inImgPath,outImgPath);
    }
 
 
    /**
     * 创建文件名称
     * @param folder
     * @param fileName
     * @return
     */
    public static File createFile(String folder, String fileName) {
        File serverFile;
        String suffix = getSuffix(fileName);
        String destFileName = null;
        long timeMillis = System.currentTimeMillis();
        int i = 1;
        do {
            if (suffix.length() == 0) {
                destFileName = String.format("%s_%d", timeMillis, i++);
            } else {
                destFileName = String.format("%s_%d.%s", timeMillis, i++, suffix);
            }
            serverFile = new File(folder, destFileName);
        } while (serverFile.exists());
        serverFile.getParentFile().mkdirs();
        return serverFile;
    }
 
    /**
     * 生成图片名称(根据旧的文件类型)
     * @param rootPath 文件目录地址
     * @param oldFileName 文件相对路径
     * @param type 操作类型 c :中心化、o :原比例、自定义
     * @return
     */
    public  String  createFileName(String rootPath,String oldFileName,int width,int height,String type) {
 
        // 初始化目录
        String pathName = oldFileName.substring(0,oldFileName.lastIndexOf("/"));
        String newFilePath = rootPath + pathName;
 
        File dirFile = new File(newFilePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
 
        // 生成文件名
        String imgName =oldFileName.substring(oldFileName.lastIndexOf("/"));
        String a = imgName.substring(0,imgName.lastIndexOf("."));
        String b = imgName.substring(imgName.lastIndexOf("."));
 
        return  newFilePath + a + "_"+ width + "x"+ height +"_" + type + b;
    }
 
 
    /**
     * 截取文件名称
     * @param filePath
     * @return
     */
    public static String getSuffix(String filePath){
        return filePath.substring(filePath.lastIndexOf("."));
    }
 
 
    /**
     * main方法测试
     * @param args
     * @throws IOException
     * @throws InterruptedException
     * @throws IM4JavaException
     */
    public static void main(String[] args) throws IOException, InterruptedException, IM4JavaException{
        String inImgPath = "D:\\Documents\\Pictures\\1.jpg";
        String outImgPath = "D:\\Documents\\Pictures\\2.jpg";
        Img4JavaUtil test = new Img4JavaUtil();
        // 中心化缩略图(常用)
        test.thumbnailImg(inImgPath,outImgPath,400,400,"c");
        // 裁剪图片
//        test.cropImg(inImgPath,outImgPath+"裁剪-300x300.jpg",300,300,0,0);
        // 获取图片信息
//        test.getImgInfo(inImgPath);
        // 等比缩放
//        test.resizeImg(inImgPath,outImgPath+"等比缩放-w300.jpg",300,null);
        // 旋转图片
//        test.rotateImg(inImgPath,outImgPath+"旋转45.jpg",45.0);
        // 转黑白图片
//        test.monochrome(inImgPath,outImgPath+"转黑白.jpg");
        // 加水印
//        test.addImgText(inImgPath,outImgPath+"加水印.jpg","CCTV-100");
        // 原图质量缩放
//        test.scaleImg(inImgPath,outImgPath+"缩略图scale.jpg",100,null);
        // 自动中心化裁剪
//        test.cropCenterImg(inImgPath,outImgPath+"中心.jpg",400,300);
 
    }
}