java图片压缩imageio-Java压缩图片()报错
java图片压缩比为1

java压缩图片,按照比例进行压缩
public static void main(String[] args) {
try {
//图片所在路径
BufferedImage templateImage = (new File("C:\\Users\\晏丁丁\\Pictures\\图片1.png"));
//原始图片的长度和宽度
int height = ();
int width = ();
//通过比例压缩
float scale = 0.5f;
//通过固定长度压缩
/*int doWithHeight = 100;
int dowithWidth = 300;*/
//压缩之后的长度和宽度
int doWithHeight = (int) (scale * height);
int dowithWidth = (int) (scale * width);
BufferedImage finalImage = new BufferedImage(dowithWidth, doWithHeight, _INT_RGB);
().drawImage((dowithWidth, doWithHeight, _SMOOTH), 0, 0, null);
//图片输出路径,以及图片名
FileOutputStream fileOutputStream = new FileOutputStream("D:/image/");
JPEGImageEncoder encoder = (fileOutputStream);
(finalImage);
();
} catch (IOException e) {
e.printStackTrace();
}
}
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
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
文章知
Java压缩图片()报错

BufferedImage sourceImg =(new FileInputStream(new File(toPath)));
或者
BufferedImage sourceImg =(new File(toPath));
求助java压缩图片存储大小的方法

可以使用Draw这个类,通过改变像素来改变存储大小,实例如下:
public static boolean compressPic(String srcFilePath, String descFilePath) throws IOException {
File file = null;
BufferedImage src = null;
FileOutputStream out = null;
ImageWriter imgWrier;
ImageWriteParam imgWriteParams;
// 指定写图片的方式为 jpg
imgWrier = ("jpg").next();
imgWriteParams = new (
null);
// 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
(_EXPLICIT);
// 这里指定压缩的程度,参数qality是取值0~1范围内,
((float) 1);
(_DISABLED);
ColorModel colorModel =(new File(srcFilePath)).getColorModel();// ();
// 指定压缩时使用的色彩模式
// (new (
// colorModel, (16, 16)));
(new (
colorModel, (16, 16)));
try {
if (isBlank(srcFilePath)) {
return false;
} else {
file = new File(srcFilePath);(());
src = (file);
out = new FileOutputStream(descFilePath);
();
// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
// OutputStream构造
((out));
// 调用write方法,就可以向输入流写图片
(null, new IIOImage(src, null, null),
imgWriteParams);
();
();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean isBlank(String string) {
if (string == null || () == 0 || ().equals("")) {
return true;
}
return false;
}
-
果盘绘画图片大全简单创作方法快速制作技巧2024-01-02 16:57:54
-
小年插画图片创作方法快速制作技巧2024-01-02 16:57:20
-
小年绘画作品制作技巧2024-01-02 16:55:45
-
小年插画图片创作方法快速制作技巧2024-01-02 16:55:02
-
立春插画图片如何制作2024-01-02 16:54:19
-
立春插画图片创作方法快速制作技巧2024-01-02 16:53:32






扫描二维码添加客服微信
扫描二维码关注