相关文章推荐
小百科
›
ImgThumbWeb/src/main/java/com/imgthumb/utils/ImgThumbUtil.java at master · zrunker/ImgThumbWeb · Git
不要命的日记本
7 月前
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Files
master
Breadcrumbs
ImgThumbWeb
/
src
/
main
/
java
/
com
/
imgthumb
/
utils
/
ImgThumbUtil.java
Blame
Blame
Latest commit
History
History
670 lines (634 loc) · 23.7 KB
master
Breadcrumbs
ImgThumbWeb
/
src
/
main
/
java
/
com
/
imgthumb
/
utils
/
ImgThumbUtil.java
Top
File metadata and controls
Code
Blame
670 lines (634 loc) · 23.7 KB
Raw
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
package com.imgthumb.utils;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.imgthumb.enums.ImageStyleEnum;
import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;
/**
* thumbnailator图片处理类
*
* @author 邹峰立
*/
public class ImgThumbUtil {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static ImgThumbUtil imgThumbUtil;
public static ImgThumbUtil getInstance() {
if (imgThumbUtil == null) {
imgThumbUtil = new ImgThumbUtil();
}
return imgThumbUtil;
}
/**
* JDK压缩图片BufferedImage质量
*
* @param bufferedImage 原图BufferedImage流
* @param quality 输出的图片质量,范围:0.0~1.0,1为最高质量。
* @param imgStyle 目标文件类型
* @return
* @throws IOException
*/
public byte[] zoomBufferedImageByQuality(BufferedImage bufferedImage, float quality) {
ImageOutputStream imageOutputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
ImageWriter writer = null;
// 获取压缩后的btye
byte[] tempByte = null;
try {
// 得到指定Format图片的writer
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");// 得到迭代器
writer = (ImageWriter) iter.next(); // 得到writer
// 得到指定writer的输出参数设置(ImageWriteParam)
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); // 设置可否压缩
iwp.setCompressionQuality(quality); // 设置压缩质量参数
iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
ColorModel colorModel = ColorModel.getRGBdefault();
// 指定压缩时使用的色彩模式
iwp.setDestinationType(new ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
// IIOMetadata imageMetaData = writer.getDefaultImageMetadata(new ImageTypeSpecifier(bufferedImage), iwp);
// 开始打包图片,写入byte[]
byteArrayOutputStream = new ByteArrayOutputStream(); // 取得内存输出流
IIOImage iIamge = new IIOImage(bufferedImage, null, null);
// 此处因为ImageWriter中用来接收write信息的output要求必须是ImageOutput
imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
// 通过ImageIo中的静态方法,得到byteArrayOutputStream的ImageOutput
writer.setOutput(imageOutputStream);
writer.write(null, iIamge, iwp);
tempByte = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (imageOutputStream != null) {
try {
imageOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
writer.dispose();
}
}
return tempByte;
}
/**
* GIF压缩质量,尺寸不变
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param imgStyle 目标文件类型
* @param quality 输出的图片质量,范围:0.0~1.0,1为最高质量。
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
* @throws IOException
*/
public void zoomGifByQuality(String imagePath, String imgStyle, float quality, String outputPath) {
ByteArrayInputStream in = null;
try {
// 防止图片后缀与图片本身类型不一致的情况
outputPath = outputPath + "." + imgStyle;
// GIF需要特殊处理
GifDecoder decoder = new GifDecoder();
int status = decoder.read(imagePath);
if (status != GifDecoder.STATUS_OK) {
throw new IOException("read image " + imagePath + " error!");
}
// LZW算法压缩,拆分一帧一帧的压缩之后合成
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(outputPath);// 设置合成位置
encoder.setRepeat(decoder.getLoopCount());// 设置GIF重复次数
int frameCount = decoder.getFrameCount();// 获取GIF有多少个frame
for (int i = 0; i < frameCount; i++) {
encoder.setDelay(decoder.getDelay(i));// 设置GIF延迟时间
BufferedImage bufferedImage = decoder.getFrame(i);
// 利用java SDK压缩BufferedImage
byte[] tempByte = zoomBufferedImageByQuality(bufferedImage, quality);
in = new ByteArrayInputStream(tempByte);
BufferedImage zoomImage = ImageIO.read(in);
encoder.addFrame(zoomImage);// 合成
}
encoder.finish();
File outFile = new File(outputPath);
BufferedImage image = ImageIO.read(outFile);
ImageIO.write(image, outFile.getName(), outFile);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* GIF压缩尺寸大小
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param imgStyle 目标文件类型
* @param width 目标文件宽
* @param height 目标文件高
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
* @throws IOException
*/
public void zoomGifBySize(String imagePath, String imgStyle, int width, int height, String outputPath) {
try {
// 防止图片后缀与图片本身类型不一致的情况
outputPath = outputPath + "." + imgStyle;
// GIF需要特殊处理
GifDecoder decoder = new GifDecoder();
int status = decoder.read(imagePath);
if (status != GifDecoder.STATUS_OK) {
throw new IOException("read image " + imagePath + " error!");
}
// LZW算法压缩,拆分一帧一帧的压缩之后合成
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(outputPath);
encoder.setRepeat(decoder.getLoopCount());
for (int i = 0; i < decoder.getFrameCount(); i++) {
encoder.setDelay(decoder.getDelay(i));// 设置播放延迟时间
BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流
BufferedImage zoomImage = new BufferedImage(width, height, bufferedImage.getType());
Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Graphics gc = zoomImage.getGraphics();
gc.setColor(Color.WHITE);
gc.drawImage(image, 0, 0, null);
encoder.addFrame(zoomImage);
}
encoder.finish();
File outFile = new File(outputPath);
BufferedImage image = ImageIO.read(outFile);
ImageIO.write(image, outFile.getName(), outFile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取原图的宽
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @return
*/
public int getImageWidth(String imagePath) {
int width = 0;
try {
BufferedImage image = ImageIO.read(new File(imagePath));
width = image.getWidth();
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return width;
}
/**
* 获取原图的高
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @return
*/
public int getImageHeight(String imagePath) {
int height = 0;
try {
BufferedImage image = ImageIO.read(new File(imagePath));
height = image.getHeight();
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return height;
}
/**
* 输出到OutputStream
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 原图片宽
* @param height 原图片高
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
* @return
*/
public OutputStream imagePathToOutputStream(String imagePath, int width, int height, String outputPath) {
OutputStream os = null;
try {
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// toOutputStream(流对象)
os = new FileOutputStream(outputPath);
Thumbnails.of(imagePath).size(width, height).toOutputStream(os);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return os;
}
/**
* 输出到BufferedImage
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 原图片宽
* @param height 原图片高
* @param imageStyle 目标文件类型
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
* @return
*/
public BufferedImage imagePathToBufferedImage(String imagePath, int width, int height, ImageStyleEnum imageStyle, String outputPath) {
BufferedImage thumbnail = null;
try {
// 获取文件需要转换的格式
String imgStyle = "jpg";
if (imageStyle != null) {
imgStyle = imageStyle.getStateInfo();
}
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath) + "." + imageStyle;
// toBufferedImage
thumbnail = Thumbnails.of(imagePath).size(width, height).asBufferedImage();
ImageIO.write(thumbnail, imgStyle, new File(outputPath));
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return thumbnail;
}
/**
* 图片尺寸不变,修改图片文件类型
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param imageStyle 目标文件类型
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void modifyImgStyle(String imagePath, ImageStyleEnum imageStyle, String outputPath) {
try {
// 获取文件需要转换的格式
String imgStyle = "jpg";
if (imageStyle != null) {
imgStyle = imageStyle.getStateInfo();
}
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 进行格式转换
// outputFormat:输出的图片格式。注意使用该方法后toFile()方法不要再含有文件类型的后缀了,否则会生成 imagename.jpg.jpg 的图片。
Thumbnails.of(imagePath).scale(1f).outputFormat(imgStyle).toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 图片尺寸不变,修改图片文件类型
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 原图片宽
* @param height 原图片高
* @param imageStyle 目标文件类型
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void modifyImgStyle2(String imagePath, int width, int height, ImageStyleEnum imageStyle, String outputPath) {
try {
// 获取文件需要转换的格式
String imgStyle = "jpg";
if (imageStyle != null) {
imgStyle = imageStyle.getStateInfo();
}
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 进行格式转换
Thumbnails.of(imagePath).size(width, height).outputFormat(imgStyle).toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 图片尺寸不变,压缩图片文件大小(质量)。
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param quality 输出的图片质量,范围:0.0~1.0,1为最高质量。
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public boolean compressImageByQuality(String imagePath, float quality, String outputPath) {
boolean bool = false;
try {
// 获取文件后缀
String imgStyle = StringUtil.getFileSuffix(imagePath);
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 压缩文件,Thumbnails对png,gif之类的不可压缩图片处理并不好
if ("png".equals(imgStyle.toLowerCase())) {// png压缩
// File file = new File(imagePath);
// BufferedImage bufferedImage = ImageIO.read(file);// 获取BufferedImage流
// quality = quality * 2.2 > 1 ? 1 : quality * 2.2f;// 设置压缩质量参数,png大约等于2.2*jpeg
// byte[] tempByte = zoomBufferedImageByQuality(bufferedImage, quality);// JDK压缩图片质量
// // 创建输出文件
// File outFile = new File(outputPath + "." + imgStyle);
// FileOutputStream fos = new FileOutputStream(outFile);
// fos.write(tempByte);
// fos.close();
// if (!outFile.exists()) {
// return false;
// }
Thumbnails.of(imagePath).scale(1f).outputQuality(quality).outputFormat("jpg").toFile(outputPath);
modifyImgStyle(outputPath + ".jpg", ImageStyleEnum.PNG, outputPath);
// 删除临时文件
File tempFile = new File(outputPath + ".jpg");
if (tempFile.exists()) {
tempFile.delete();
}
} else if ("gif".equals(imgStyle.toLowerCase())) {// gif压缩
quality = quality / 8;// 重新计算压缩质量值,这里只是取一个大约值,具体计算规则不清楚
zoomGifByQuality(imagePath, imgStyle, quality, outputPath);
} else {// jpg.jpeg
Thumbnails.of(imagePath).scale(1f).outputQuality(quality).outputFormat(imgStyle).toFile(outputPath);
}
bool = true;
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return bool;
}
/**
* 图片尺寸不变,压缩图片文件大小(质量),输出jpg。
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param quality 输出的图片质量,范围:0.0~1.0,1为最高质量。
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public boolean compressImageToJpgByQuality(String imagePath, float quality, String outputPath) {
boolean bool = false;
try {
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 压缩文件,Thumbnails对png,gif之类的不可压缩图片处理并不好
Thumbnails.of(imagePath).scale(1f).outputQuality(quality).outputFormat(ImageStyleEnum.stateInfoOf("JPG")).toFile(outputPath);
bool = true;
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return bool;
}
/**
* 压缩至指定图片尺寸(例如:宽400 高300),不保持图片比例
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 原图片宽
* @param height 原图片高
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void compressImageNoKeepRatioBySize(String imagePath, int width, int height, String outputPath) {
try {
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 压缩文件
Thumbnails.of(imagePath).forceSize(width, height).toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 压缩至指定图片尺寸(例如:宽400 高300),保持图片比例
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 原图片宽
* @param height 原图片高
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void compressImageKeepRatioBySize(String imagePath, int width, int height, String outputPath) {
try {
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 压缩文件
Thumbnails.of(imagePath).size(width, height).keepAspectRatio(true).toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 压缩至原图片的百分之多少
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param scale 缩放比例
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public boolean compressImageByRatio(String imagePath, double scale, String outputPath) {
boolean bool = false;
try {
BufferedImage image = ImageIO.read(new File(imagePath));
if (image != null) {
// 获取文件后缀
String imgStyle = StringUtil.getFileSuffix(imagePath);
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath) + "." + imgStyle;
// 压缩文件
Thumbnails.of(image).scale(scale).toFile(outputPath);
bool = true;
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return bool;
}
/**
* 中心裁剪至指定图片尺寸(例如:宽400 高300),保持图片不变形,多余部分裁剪掉
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 裁剪区域宽
* @param height 裁剪区域高
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void cropImageByCenter(String imagePath, int width, int height, String outputPath) {
try {
// 获取原图的宽和高
BufferedImage image = ImageIO.read(new File(imagePath));
Builder<BufferedImage> builder = null;
int imageWidth = image.getWidth();
int imageHeitht = image.getHeight();
// 计算比例,沿中心裁剪
if ((float) height / width != (float) imageWidth / imageHeitht) {
if (imageWidth > imageHeitht) {
image = Thumbnails.of(imagePath).height(height).asBufferedImage();
} else {
image = Thumbnails.of(imagePath).width(width).asBufferedImage();
}
builder = Thumbnails.of(image).sourceRegion(Positions.CENTER, width, height).size(width, height);
} else {
builder = Thumbnails.of(image).size(width, height);
}
// 获取文件后缀
String imgStyle = StringUtil.getFileSuffix(imagePath);
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
builder.outputFormat(imgStyle).toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 裁剪
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 裁剪区域宽
* @param height 裁剪区域高
* @param position 裁剪位置
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void cropImage(String imagePath, int width, int height, Position position, String outputPath) {
try {
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 裁剪
Thumbnails.of(imagePath)
.sourceRegion(position, width, height)
.size(width, height)
.keepAspectRatio(false)
.toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 裁剪2
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 裁剪区域宽
* @param height 裁剪区域高
* @param x 裁剪位置x坐标
* @param y 裁剪位置y坐标
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void cropImage2(String imagePath, int width, int height, int x, int y, String outputPath) {
try {
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 裁剪
Thumbnails.of(imagePath)
.sourceRegion(x, y, width, height)
.size(width, height)
.keepAspectRatio(false)
.toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 旋转
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param angle 旋转角度
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void rotateImage(String imagePath, double angle, String outputPath) {
try {
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 旋转
Thumbnails.of(imagePath).scale(1f).rotate(angle).toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
/**
* 水印
*
* @param imagePath 原图片路径地址,如:F:\\a.png
* @param width 原图片宽
* @param height 原图片高
* @param position 裁剪位置
* @param watermarkImgPath 水印图片路径地址,如:F:\\b.png
* @param opacity 水印图片透明度 0~1f
* @param outputPath 输出文件路径(不带后缀),如:F:\\b,默认与原图片路径相同,为空时将会替代原文件
*/
public void watermarkImage(String imagePath, int width, int height, Position position, String watermarkImgPath, float opacity, String outputPath) {
try {
// 设置输出路径(不带后缀)
if (StringUtil.isEmpty(outputPath))
outputPath = imagePath;
outputPath = StringUtil.getFilePathNoSuffix(outputPath);
// 水印
Thumbnails.of(imagePath)
.size(width, height)
.watermark(position, ImageIO.read(new File(watermarkImgPath)), opacity)
.outputQuality(1f)
.toFile(outputPath);
} catch (IOException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
public static void main(String[] args) {
// modifyImgStyle("F:\\b.png", ImageStyleEnum.JPEG, "F:\\b");
// modifyImgStyle("F:\\b.png", ImageStyle.PNG, null);
// modifyImgStyle2("F:\\b.png", 1920, 400, ImageStyle.PNG, null);
// compressImageByQuality("F:\\b.png", 0.25f, "F:\\c");
// compressImageNoKeepRatioBySize("F:\\b.png", 400, 400, "F:\\d");
// compressImageKeepRatioBySize("F:\\b.png", 400, 400, "F:\\e");
// compressImageByRatio("F:\\b.png", 0.5, "F:\\bd");
// cropImage("F:\\c.png", 500, 200, null);
// rotateImage("F:\\bd.png", -90, null);
// watermarkImage("F:\\b.png", 1920, 400, "F:\\e.png", 0.5f, "F:\\bc");
// cropImage("F:\\b.png", 400, 400, Positions.CENTER, "F:\\cc");
getInstance().compressImageByQuality("F:\\efd.png", 0.8f, "F:\\efdd.png");
}
推荐文章