java实现图片压缩
经验证,以下代码能压缩40-50%
code
复制代码
1
2
3
4
5
6
7
8<!-- thumbnailator图片压缩需要依赖 --> <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
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
138import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import javax.imageio.ImageIO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.coobird.thumbnailator.Thumbnails; public class ImageUtil { private final static Logger logger = LoggerFactory.getLogger(ImageUtil.class); private final static int WAIT_NUM = 30; /** * 方式1 * 指定图片宽度和高度和压缩比例对图片进行压缩 * * @param imgsrc 源图片地址 * @param imgdist 目标图片地址 */ public static String reduceImg(String imgsrc, String imgdist) { logger.info("图片压缩开始"); FileOutputStream out = null; try { File srcfile = new File(imgsrc); // 检查图片文件是否存在 if (!srcfile.exists()) { logger.info("文件不存在"); return null; } int[] results = getImgWidthHeight(srcfile); int widthdist = results[0]; int heightdist = results[1]; // 开始读取文件并进行压缩 Image src = ImageIO.read(srcfile); // 构造一个类型为预定义图像类型之一的 BufferedImage BufferedImage tag = new BufferedImage(widthdist, heightdist, BufferedImage.TYPE_INT_RGB); // 这边是压缩的模式设置 tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null); //创建文件输出流 out = new FileOutputStream(imgdist); //将图片按JPEG压缩,保存到out中 String formatName = imgdist.substring(imgdist.lastIndexOf(".") + 1); ImageIO.write(tag, formatName, out); out.flush(); } catch (Exception ef) { ef.printStackTrace(); } finally { try { if (out != null) { //关闭文件输出流 out.close(); } } catch (Exception e) { e.printStackTrace(); } } logger.info("图片压缩结束"); return imgdist; } /** * 获取图片宽度和高度 * * @param file 图片路径 * @return 返回图片的宽度 */ public static int[] getImgWidthHeight(File file) { InputStream is = null; BufferedImage src = null; int result[] = {0, 0}; try { // 获得文件输入流 is = new FileInputStream(file); // 从流里将图片写入缓冲图片区 src = ImageIO.read(is); // 得到源图片宽 result[0] = src.getWidth(null); // 得到源图片高 result[1] = src.getHeight(null); } catch (Exception ef) { ef.printStackTrace(); } finally { try { if (is != null) { is.close(); //关闭输入流 } } catch (Exception e) { e.printStackTrace(); } } return result; } /** * 方式2 * @param imgsrc 源目标文件 * @param imgdist 生成目标文件 * @return */ public static String thumbnailatorReduceImg(String imgsrc, String imgdist) { try { int countFlag = 1; logger.info("图片压缩开始"); Thumbnails.of(imgsrc).scale(1).outputQuality(0.6).toFile(imgdist); // 循环等待 while (countFlag < WAIT_NUM) { try { countFlag++; Thread.sleep(150); File file = new File(imgdist); if (file.exists()) { break; } } catch (Exception e) { e.printStackTrace(); } } logger.info("图片压缩结束"); } catch (Exception e) { e.printStackTrace(); } return imgdist; } public static void main(String[] args) { String scrPath = "C:\Users\dell\Desktop\model\test\9d6d5d0d-f179-43ce-a3a5-bc58db650918.png"; String toPath = "C:\Users\dell\Desktop\model\test\copy.jpg"; File srcfile = new File(scrPath); File distfile = new File(toPath); System.out.println("压缩前图片大小:" + srcfile.length()); //reduceImg(scrPath, toPath); thumbnailatorReduceImg(scrPath, toPath); System.out.println("压缩后图片大小:" + distfile.length()); } }
最后
以上就是虚心蓝天最近收集整理的关于java实现图片压缩java实现图片压缩的全部内容,更多相关java实现图片压缩java实现图片压缩内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复