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
package com.yanzuoguang.util;
import net.coobird.thumbnailator.Thumbnails;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 图片处理程序
*
* @author 颜佐光
*/
public class ImageHelper {
public static final float DEFAULT_SIZE = 1f;
public static final float DEFAULT_QUALITY = 1f;
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public static void compressPicBySystem(String srcFilePath, String descFilePath,
float quality, float size) throws IOException {
// 设置默认值
if (size == 0) {
size = 1;
}
if (quality == 0) {
quality = 1;
}
File input = new File(srcFilePath);
BufferedImage image = ImageIO.read(input);
// 压缩图片大小
if (size < 1) {
int toWidth = (int) (image.getWidth() * size);
int toHeight = (int) (image.getHeight() * size);
BufferedImage buffImage = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
buffImage.getGraphics().drawImage(image.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH),
0, 0, null);
image = buffImage;
}
// 先指定Output,才能调用writer.write方法
File output = new File(descFilePath);
// 指定写图片的方式为 jpg
ImageWriter writer = null;
OutputStream out = null;
ImageOutputStream ios = null;
try {
writer = ImageIO.getImageWritersByFormatName("jpg").next();
out = new FileOutputStream(output);
ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
// 指定压缩方式为MODE_EXPLICIT
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// param.set
// 压缩程度,参数quality是取值0~1范围内
param.setCompressionQuality(quality);
}
// 调用write方法,向输入流写图片
writer.write(null, new IIOImage(image, null, null), param);
} finally {
if (out != null) {
out.close();
}
if (ios != null) {
ios.close();
}
if (writer != null) {
writer.dispose();
}
}
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath) throws IOException {
compressPic(srcFilePath, descFilePath, DEFAULT_QUALITY, DEFAULT_SIZE);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @param quality 压缩程度,参数quality是取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality) throws IOException {
compressPic(srcFilePath, descFilePath, quality, DEFAULT_SIZE);
}
/**
* 压缩图片
*
* @param srcFilePath 来源路径
* @param descFilePath 目标路径
* @param quality 质量程度,取值0~1范围内
* @param size 大小程度,取值0~1范围内
* @throws IOException
*/
public static void compressPic(String srcFilePath, String descFilePath, float quality, float size) throws IOException {
Thumbnails
// 来源目录
.of(srcFilePath)
// 按比例缩小
.scale(size)
// 质量压缩
.outputQuality(quality)
// 目标文件
.toFile(descFilePath);
}
}