forked from chenssy89/jutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtil.java
More file actions
553 lines (507 loc) · 19.4 KB
/
ImageUtil.java
File metadata and controls
553 lines (507 loc) · 19.4 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package com.JUtils.image;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* 图像处理<br>
* 对图片进行压缩、水印、伸缩变换、透明处理、格式转换操作
*
* @Author:chenssy
* @date:2015年3月19日
*/
public class ImageUtil {
public static final float DEFAULT_QUALITY = 0.2125f ;
/**
*
* 添加图片水印操作(物理存盘,使用默认格式)
*
* @param imgPath
* 待处理图片
* @param markPath
* 水印图片
* @param x
* 水印位于图片左上角的 x 坐标值
* @param y
* 水印位于图片左上角的 y 坐标值
* @param alpha
* 水印透明度 0.1f ~ 1.0f
* @param destPath
* 文件存放路径
* @throws Exception
*
*/
public static void addWaterMark(String imgPath, String markPath, int x, int y, float alpha,String destPath) throws Exception{
try {
BufferedImage bufferedImage = addWaterMark(imgPath, markPath, x, y, alpha);
ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
} catch (Exception e) {
throw new RuntimeException("添加图片水印异常");
}
}
/**
*
* 添加图片水印操作(物理存盘,自定义格式)
*
* @param imgPath
* 待处理图片
* @param markPath
* 水印图片
* @param x
* 水印位于图片左上角的 x 坐标值
* @param y
* 水印位于图片左上角的 y 坐标值
* @param alpha
* 水印透明度 0.1f ~ 1.0f
* @param format
* 添加水印后存储的格式
* @param destPath
* 文件存放路径
* @throws Exception
*
*/
public static void addWaterMark(String imgPath, String markPath, int x, int y, float alpha,String format,String destPath) throws Exception{
try {
BufferedImage bufferedImage = addWaterMark(imgPath, markPath, x, y, alpha);
ImageIO.write(bufferedImage,format , new File(destPath));
} catch (Exception e) {
throw new RuntimeException("添加图片水印异常");
}
}
/**
*
* 添加图片水印操作,返回BufferedImage对象
*
* @param imgPath
* 待处理图片
* @param markPath
* 水印图片
* @param x
* 水印位于图片左上角的 x 坐标值
* @param y
* 水印位于图片左上角的 y 坐标值
* @param alpha
* 水印透明度 0.1f ~ 1.0f
* @return
* 处理后的图片对象
* @throws Exception
*
*/
public static BufferedImage addWaterMark(String imgPath, String markPath, int x, int y, float alpha) throws Exception{
BufferedImage targetImage = null;
try {
// 加载待处理图片文件
Image img = ImageIO.read(new File(imgPath));
//创建目标图象文件
targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = targetImage.createGraphics();
g.drawImage(img, 0, 0, null);
// 加载水印图片文件
Image markImg = ImageIO.read(new File(markPath));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawImage(markImg, x, y, null);
g.dispose();
} catch (Exception e) {
throw new RuntimeException("添加图片水印操作异常");
}
return targetImage;
}
/**
*
* 添加文字水印操作(物理存盘,使用默认格式)
*
* @param imgPath
* 待处理图片
* @param text
* 水印文字
* @param font
* 水印字体信息 不写默认值为宋体
* @param color
* 水印字体颜色
* @param x
* 水印位于图片左上角的 x 坐标值
* @param y
* 水印位于图片左上角的 y 坐标值
* @param alpha
* 水印透明度 0.1f ~ 1.0f
* @param format
* 添加水印后存储的格式
* @param destPath
* 文件存放路径
* @throws Exception
*/
public static void addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha,String destPath) throws Exception{
try {
BufferedImage bufferedImage = addTextMark(imgPath, text, font, color, x, y, alpha);
ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
} catch (Exception e) {
throw new RuntimeException("图片添加文字水印异常");
}
}
/**
*
* 添加文字水印操作(物理存盘,自定义格式)
*
* @param imgPath
* 待处理图片
* @param text
* 水印文字
* @param font
* 水印字体信息 不写默认值为宋体
* @param color
* 水印字体颜色
* @param x
* 水印位于图片左上角的 x 坐标值
* @param y
* 水印位于图片左上角的 y 坐标值
* @param alpha
* 水印透明度 0.1f ~ 1.0f
* @param format
* 添加水印后存储的格式
* @param destPath
* 文件存放路径
* @throws Exception
*/
public static void addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha,String format,String destPath) throws Exception{
try {
BufferedImage bufferedImage = addTextMark(imgPath, text, font, color, x, y, alpha);
ImageIO.write(bufferedImage, format, new File(destPath));
} catch (Exception e) {
throw new RuntimeException("图片添加文字水印异常");
}
}
/**
*
* 添加文字水印操作,返回BufferedImage对象
*
* @param imgPath
* 待处理图片
* @param text
* 水印文字
* @param font
* 水印字体信息 不写默认值为宋体
* @param color
* 水印字体颜色
* @param x
* 水印位于图片左上角的 x 坐标值
* @param y
* 水印位于图片左上角的 y 坐标值
* @param alpha
* 水印透明度 0.1f ~ 1.0f
* @return
* 处理后的图片对象
* @throws Exception
*/
public static BufferedImage addTextMark(String imgPath, String text, Font font, Color color, float x, float y, float alpha) throws Exception{
BufferedImage targetImage = null;
try {
Font Dfont = (font == null) ? new Font("宋体", 20, 13) : font;
Image img = ImageIO.read(new File(imgPath));
//创建目标图像文件
targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = targetImage.createGraphics();
g.drawImage(img, 0, 0, null);
g.setColor(color);
g.setFont(Dfont);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawString(text, x, y);
g.dispose();
} catch (Exception e) {
throw new RuntimeException("添加文字水印操作异常");
}
return targetImage;
}
/**
*
*
*
* 压缩图片操作(文件物理存盘,使用默认格式)
*
* @param imgPath
* 待处理图片
* @param quality
* 图片质量(0-1之間的float值)
* @param width
* 输出图片的宽度 输入负数参数表示用原来图片宽
* @param height
* 输出图片的高度 输入负数参数表示用原来图片高
* @param autoSize
* 是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放
* @param format
* 压缩后存储的格式
* @param destPath
* 文件存放路径
*
* @throws Exception
*/
public static void compressImage(String imgPath,float quality,int width, int height, boolean autoSize,String destPath)throws Exception{
try {
BufferedImage bufferedImage = compressImage(imgPath, quality, width, height, autoSize);
ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
} catch (Exception e) {
throw new RuntimeException("图片压缩异常");
}
}
/**
*
* 压缩图片操作(文件物理存盘,可自定义格式)
*
* @param imgPath
* 待处理图片
* @param quality
* 图片质量(0-1之間的float值)
* @param width
* 输出图片的宽度 输入负数参数表示用原来图片宽
* @param height
* 输出图片的高度 输入负数参数表示用原来图片高
* @param autoSize
* 是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放
* @param format
* 压缩后存储的格式
* @param destPath
* 文件存放路径
*
* @throws Exception
*/
public static void compressImage(String imgPath,float quality,int width, int height, boolean autoSize,String format,String destPath)throws Exception{
try {
BufferedImage bufferedImage = compressImage(imgPath, quality, width, height, autoSize);
ImageIO.write(bufferedImage, format, new File(destPath));
} catch (Exception e) {
throw new RuntimeException("图片压缩异常");
}
}
/**
*
* 压缩图片操作,返回BufferedImage对象
*
* @param imgPath
* 待处理图片
* @param quality
* 图片质量(0-1之間的float值)
* @param width
* 输出图片的宽度 输入负数参数表示用原来图片宽
* @param height
* 输出图片的高度 输入负数参数表示用原来图片高
* @param autoSize
* 是否等比缩放 true表示进行等比缩放 false表示不进行等比缩放
* @return
* 处理后的图片对象
* @throws Exception
*/
public static BufferedImage compressImage(String imgPath,float quality,int width, int height, boolean autoSize)throws Exception{
BufferedImage targetImage = null;
if(quality<0F||quality>1F){
quality = DEFAULT_QUALITY;
}
try {
Image img = ImageIO.read(new File(imgPath));
//如果用户输入的图片参数合法则按用户定义的复制,负值参数表示执行默认值
int newwidth =( width > 0 ) ? width : img.getWidth(null);
//如果用户输入的图片参数合法则按用户定义的复制,负值参数表示执行默认值
int newheight = ( height > 0 )? height: img.getHeight(null);
//如果是自适应大小则进行比例缩放
if(autoSize){
// 为等比缩放计算输出的图片宽度及高度
double Widthrate = ((double) img.getWidth(null)) / (double) width + 0.1;
double heightrate = ((double) img.getHeight(null))/ (double) height + 0.1;
double rate = Widthrate > heightrate ? Widthrate : heightrate;
newwidth = (int) (((double) img.getWidth(null)) / rate);
newheight = (int) (((double) img.getHeight(null)) / rate);
}
//创建目标图像文件
targetImage = new BufferedImage(newwidth,newheight,BufferedImage.TYPE_INT_RGB);
Graphics2D g = targetImage.createGraphics();
g.drawImage(img, 0, 0, newwidth, newheight, null);
//如果添加水印或者文字则继续下面操作,不添加的话直接返回目标文件----------------------
g.dispose();
} catch (Exception e) {
throw new RuntimeException("图片压缩操作异常");
}
return targetImage;
}
/**
* 图片黑白化操作(文件物理存盘,使用默认格式)
*
* @param bufferedImage
* 处理的图片对象
* @param destPath
* 目标文件地址
* @throws Exception
*
*/
public static void imageGray(String imgPath, String destPath)throws Exception{
imageGray(imgPath, imageFormat(imgPath), destPath);
}
/**
* 图片黑白化操作(文件物理存盘,可自定义格式)
*
* @param bufferedImage
* 处理的图片对象
* @param format
* 图片格式
* @param destPath
* 目标文件地址
* @throws Exception
*
*/
public static void imageGray(String imgPath,String format, String destPath)throws Exception{
try {
BufferedImage bufferedImage = ImageIO.read(new File(imgPath));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
bufferedImage = op.filter(bufferedImage, null);
ImageIO.write(bufferedImage, format , new File(destPath));
} catch (Exception e) {
throw new RuntimeException("图片灰白化异常");
}
}
/**
* 图片透明化操作(文件物理存盘,使用默认格式)
*
* @param imgPath
* 图片路径
* @param destPath
* 图片存放路径
* @throws Exception
*/
public static void imageLucency(String imgPath,String destPath)throws Exception{
try {
BufferedImage bufferedImage = imageLucency(imgPath);
ImageIO.write(bufferedImage, imageFormat(imgPath), new File(destPath));
} catch (Exception e) {
throw new RuntimeException("图片透明化异常");
}
}
/**
* 图片透明化操作(文件物理存盘,可自定义格式)
*
* @param imgPath
* 图片路径
* @param format
* 图片格式
* @param destPath
* 图片存放路径
* @throws Exception
*/
public static void imageLucency(String imgPath,String format,String destPath)throws Exception{
try {
BufferedImage bufferedImage = imageLucency(imgPath);
ImageIO.write(bufferedImage, format, new File(destPath));
} catch (Exception e) {
throw new RuntimeException("图片透明化异常");
}
}
/**
* 图片透明化操作返回BufferedImage对象
*
* @param imgPath
* 图片路径
* @return
* 透明化后的图片对象
* @throws Exception
*/
public static BufferedImage imageLucency(String imgPath)throws Exception{
BufferedImage targetImage = null;
try {
//读取图片
BufferedImage img = ImageIO.read(new FileInputStream(imgPath));
//透明度
int alpha = 0;
//执行透明化
executeRGB(img, alpha);
targetImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = targetImage.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
} catch (Exception e) {
throw new RuntimeException("图片透明化执行异常");
}
return targetImage;
}
/**
* 执行透明化的核心算法
*
* @param img
* 图片对象
* @param alpha
* 透明度
* @throws Exception
*/
public static void executeRGB(BufferedImage img, int alpha) throws Exception{
int rgb = 0;//RGB值
//x表示BufferedImage的x坐标,y表示BufferedImage的y坐标
for(int x=img.getMinX();x<img.getWidth();x++){
for(int y=img.getMinY();y<img.getHeight();y++){
//获取点位的RGB值进行比较重新设定
rgb = img.getRGB(x, y);
int R =(rgb & 0xff0000 ) >> 16 ;
int G= (rgb & 0xff00 ) >> 8 ;
int B= (rgb & 0xff );
if(((255-R)<30) && ((255-G)<30) && ((255-B)<30)){
rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
img.setRGB(x, y, rgb);
}
}
}
}
/**
* 图片格式转化操作(文件物理存盘)
*
* @param imgPath
* 原始图片存放地址
* @param format
* 待转换的格式 jpeg,gif,png,bmp等
* @param destPath
* 目标文件地址
* @throws Exception
*/
public static void formatConvert(String imgPath, String format, String destPath)throws Exception{
try {
BufferedImage bufferedImage = ImageIO.read(new File(imgPath));
ImageIO.write(bufferedImage, format, new File(destPath));
} catch (IOException e) {
throw new RuntimeException("文件格式转换出错");
}
}
/**
* 图片格式转化操作返回BufferedImage对象
*
* @param bufferedImage
* BufferedImage图片转换对象
* @param format
* 待转换的格式 jpeg,gif,png,bmp等
* @param destPath
* 目标文件地址
* @throws Exception
*/
public static void formatConvert(BufferedImage bufferedImag, String format, String destPath)throws Exception{
try {
ImageIO.write(bufferedImag, format, new File(destPath));
} catch (IOException e) {
throw new RuntimeException("文件格式转换出错");
}
}
/**
* 获取图片文件的真实格式信息
*
* @param imgPath
* 图片原文件存放地址
* @return
* 图片格式
* @throws Exception
*/
public static String imageFormat(String imgPath)throws Exception{
String[] filess = imgPath.split("\\\\");
String[] formats = filess[filess.length - 1].split("\\.");
return formats[formats.length - 1];
}
}