文章首发于:clawhub.club


ImageIO:


JDK自带的图片处理类,处理的质量还不错,但是网上都说会发生OOM。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 按照固定宽高原图压缩
*
* @param img 源图片文件
* @param width 宽
* @param height 高
* @param out 输出流
* @throws IOException the io exception
*/
public static void thumbnail(File img, int width, int height, OutputStream out) throws IOException {
BufferedImage bufferedImage = ImageIO.read(img);
Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = tag.getGraphics();
graphics.setColor(Color.RED);
// 绘制处理后的图
graphics.drawImage(image, 0, 0, null);
graphics.dispose();
ImageIO.write(tag, "JPEG", out);
}

GraphicsMagick:

—java
是个优秀的图片处理库,官网,需要先安装,再使用。

例子:

maven管理依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.im4java/im4java -->
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>
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
/**
* 根据尺寸缩放图片[等比例缩放:参数height为null,按宽度缩放比例缩放;参数width为null,按高度缩放比例缩放]
*
* @param imagePath 源图片路径
* @param newPath 处理后图片路径
* @param width 缩放后的图片宽度
* @param height 缩放后的图片高度
* @param quality 压缩质量(0-100)
* @throws InterruptedException the interrupted exception
* @throws IOException the io exception
* @throws IM4JavaException the im 4 java exception
*/
public static void zoomImage(String imagePath, String newPath, Integer width, Integer height, Double quality) throws InterruptedException, IOException, IM4JavaException {
IMOperation op = new IMOperation();
op.addImage(imagePath);
if (width == null) {
// 根据高度缩放图片
op.resize(null, height);
} else if (height == null) {
// 根据宽度缩放图片
op.resize(width);
} else {
op.resize(width, height);
}
op.quality(quality);
op.addImage(newPath);
ConvertCmd convert = new ConvertCmd(true);
// linux下不要设置此值,不然会报错
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
convert.setSearchPath("C:\\Program Files\\GraphicsMagick-1.3.31-Q16");
}
convert.run(op);
}

OpenCV:

—java
这个家伙的名称就比较亮了,公司又专门搞这个的,我只是简单的用用,java中也有工具包直接操作openCV,但是使用起来还是没有C++操作起来快。

maven依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv -->
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.4.3-1.4.3</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 压缩图片
*
* @param srcImagePath 源图片路径
* @param desImagePath 目标路径
* @param scale 压缩率
*/
public static void thumbnail(String srcImagePath, String desImagePath, double scale) {
//读取图像到矩阵中,取灰度图像
Mat src = Imgcodecs.imread(srcImagePath);
//复制矩阵进入dst
Mat dst = src.clone();
Imgproc.resize(src, dst, new Size(src.width() * scale, src.height() * scale));
Imgcodecs.imwrite(desImagePath, dst);
}

Thumbnails:


这个是谷歌的一个包,使用起来很方便。

maven依赖

1
2
3
4
5
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 压缩图片
*
* @param srcImagePath 源图片路径
* @param desImagePath 目标路径
* @param scale 压缩率
* @throws IOException the io exception
*/
public static void thumbnail(String srcImagePath, String desImagePath, double scale) throws IOException {
Thumbnails.of(srcImagePath).scale(scale).toFile(desImagePath);
}