ImageUtils.java
1.78 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
package com.example.demo;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* Created by jack on 2018/7/2.
*/
public class ImageUtils {
public static BufferedImage zoomInImage(BufferedImage originalImage) {
int width = originalImage.getWidth();
int height = originalImage.getHeight();
int scale_width = width;
int scale_height = height;
if (width < 200) {
scale_width = 225;
}
if (height < 200) {
scale_height = 225;
}
BufferedImage newImage = new BufferedImage(scale_width, scale_height, originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0, 0, scale_width, scale_height, null);
g.dispose();
return newImage;
}
public static boolean zoomInImage(String srcPath, String newPath) {
BufferedImage bufferedImage = null;
try {
File of = new File(srcPath);
if (of.canRead()) {
bufferedImage = ImageIO.read(of);
}
} catch (IOException e) {
//TODO: 打印日志
return false;
}
if (bufferedImage != null) {
if (bufferedImage.getWidth() < 200 || bufferedImage.getHeight() < 200) {
bufferedImage = zoomInImage(bufferedImage);
}
try {
//TODO: 这个保存路径需要配置下子好一点
ImageIO.write(bufferedImage, "JPG", new File(newPath)); //保存修改后的图像,全部保存为JPG格式
} catch (IOException e) {
// TODO 打印错误信息
return false;
}
}
return true;
}
}