首页 > 其他分享 >图片上传压缩处理

图片上传压缩处理

时间:2023-09-21 15:34:44浏览次数:28  
标签:String 缩放 int 压缩 jpg 上传 public 图片


    解决用户上传图片后,按照用户规定的尺寸大小或者按照图片比例,对图片进行压缩。

    自己试写的工具类,写的时候考虑了几个关键点:

    1、图片格式

    JAVA的API很好,com.sun.image.codec.jpeg.JPEGCodec和com.sun.image.codec.jpeg.JPEGImageEncoder 这两个类基本上自动解决了类型转换的问题,可以正常实现bmp转jpg、png转jpg、gif转jpg但是暂时还没有解决gif转gif的功能。

    2、画面质量的问题

    BufferedImage tag = new BufferedImage((int)newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

    // Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢

    tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);

    3、压缩速度

    测试36MB的bmp图片(8192*6144)压缩成(160*120)的jpg的5KB图片,只需要2-3秒的时间。批量处理100张(1027*768)的bmp图像,转换成(120*80)的jpg图片总共只需要17秒。

    4、根据用户喜好选择压缩模式

    按比例或者按规定尺寸

    //compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))

 

以下是源码,如觉得可以更加完善的话,希望大家可以提供点意见!


Java代码


1. /** 
2.  *  缩略图实现,将图片(jpg、bmp、png、gif等等)真实的变成想要的大小 
3.  */
4. package
5.   
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14.   
15. /******************************************************************************* 
16.  * 缩略图类(通用) 本java类能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换。 具体使用方法 
17.  * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) 
18.  */
19. public class
20. private File file = null; // 文件对象  
21. private String inputDir; // 输入图路径 
22. private String outputDir; // 输出图路径 
23. private String inputFileName; // 输入图文件名 
24. private String outputFileName; // 输出图文件名 
25. private int outputWidth = 100; // 默认输出图片宽 
26. private int outputHeight = 100; // 默认输出图片高 
27. private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放) 
28. public CompressPicDemo() { // 初始化变量 
29. "";     
30. "";     
31. "";     
32. "";     
33. 100;     
34. 100;     
35.      }    
36. public void
37. this.inputDir = inputDir;     
38.      }    
39. public void
40. this.outputDir = outputDir;     
41.      }    
42. public void
43. this.inputFileName = inputFileName;    
44.      }    
45. public void
46. this.outputFileName = outputFileName;     
47.      }    
48. public void setOutputWidth(int
49. this.outputWidth = outputWidth;     
50.      }    
51. public void setOutputHeight(int
52. this.outputHeight = outputHeight;     
53.      }    
54. public void setWidthAndHeight(int width, int
55. this.outputWidth = width;    
56. this.outputHeight = height;     
57.      }    
58.         
59. /*  
60.       * 获得图片大小  
61.       * 传入参数 String path :图片路径  
62.       */
63. public long
64. new
65. return
66.      }   
67.         
68. // 图片处理  
69. public
70. try
71. //获得源文件  
72. new
73. if
74. return "";     
75.              }    
76.              Image img = ImageIO.read(file);    
77. // 判断图片格式是否正确  
78. if (img.getWidth(null) == -1) {    
79. " can't read,retry!" + "<BR>");     
80. return "no";     
81. else
82. int newWidth; int
83. // 判断是否是等比缩放  
84. if (this.proportion == true) {     
85. // 为等比缩放计算输出的图片宽度及高度  
86. double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;     
87. double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;     
88. // 根据缩放比率大的进行缩放控制  
89. double
90. int) (((double) img.getWidth(null)) / rate);     
91. int) (((double) img.getHeight(null)) / rate);     
92. else
93. // 输出的图片宽度  
94. // 输出的图片高度  
95.                  }    
96. new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);     
97.                    
98. /* 
99.                  * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 
100.                  * 优先级比速度高 生成的图片质量比较好 但速度慢 
101.                  */
102. 0, 0, null);    
103. new
104. // JPEGImageEncoder可适用于其他图片类型的转换  
105.                 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);    
106.                 encoder.encode(tag);    
107.                 out.close();    
108.              }    
109. catch
110.              ex.printStackTrace();    
111.          }    
112. return "ok";     
113.     }    
114. public
115. // 输入图路径  
116. this.inputDir = inputDir;     
117. // 输出图路径  
118. this.outputDir = outputDir;     
119. // 输入图文件名  
120. this.inputFileName = inputFileName;     
121. // 输出图文件名 
122. this.outputFileName = outputFileName;     
123. return
124.     }    
125. public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean
126. // 输入图路径  
127. this.inputDir = inputDir;     
128. // 输出图路径  
129. this.outputDir = outputDir;     
130. // 输入图文件名  
131. this.inputFileName = inputFileName;     
132. // 输出图文件名  
133. this.outputFileName = outputFileName;     
134. // 设置图片长宽 
135.         setWidthAndHeight(width, height);    
136. // 是否是等比缩放 标记  
137. this.proportion = gp;     
138. return
139.     }    
140.        
141. // main测试  
142. // compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) 
143. public static void
144. new
145. "输入的图片大小:" + mypic.getPicSize("e:\\1.jpg")/1024 + "KB");     
146. int count = 0; // 记录全部图片压缩所用时间 
147. for (int i = 0; i < 100; i++) {     
148. int start = (int) System.currentTimeMillis();   // 开始时间  
149. "e:\\", "e:\\test\\", "1.jpg", "r1"+i+".jpg", 120, 120, true);     
150. int end = (int) System.currentTimeMillis(); // 结束时间  
151. int re = end-start; // 但图片生成处理时间  
152. "第" + (i+1) + "张图片压缩处理使用了: " + re + "毫秒");     
153. "输出的图片大小:" + mypic.getPicSize("e:\\test\\r1"+i+".jpg")/1024 + "KB");     
154.         }   
155. "总共用了:" + count + "毫秒");     
156.     }    
157.  }

标签:String,缩放,int,压缩,jpg,上传,public,图片
From: https://blog.51cto.com/u_16230604/7554805

相关文章

  • NET6/Framework 封装邮件发送纯文本/HTML/HTML+图片/附件
    NugetRuntime:Net6MailKit4.2.0MimeKit4.2.0发送纯文本{varbodyBuilder=newBodyBuilder();bodyBuilder.TextBody="这是一封纯文本邮件";message.Body=bodyBuilder.ToMessageBody();}{vartextPart=newTextPart("plain")......
  • JAVA应用XFire框架来实现WebServie的大文件传输功能之二(上传)
    xml文件:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://xfire.codehaus.org/config/1.0"><service><name>HelloWorldService</name><namespace>http://localhost:8090......
  • Java大文件上传(秒传、分片上传、断点续传)
    一、秒传秒传就是不传,实现逻辑就是看数据库或者缓存里是否已经有这个文件了,有了,直接从已有的文件去拿就可以了(返回文件地址)。这里判断是否是相同文件,要用到信息摘要算法,详情可以参考:一文读懂当前常用的加密技术体系。信息摘要算法常常被用来保证信息的完整性,防止信息在传输过程中......
  • MySQL压缩包安装问题记录Can't connect to MySQL server on localhost (10061)解决方
    本文章向大家介绍MySQL问题记录--Can'tconnecttoMySQLserveronlocalhost(10061)解决方法,主要包括MySQL问题记录--Can'tconnecttoMySQLserveronlocalhost(10061)解决方法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下......
  • 力扣6.N 字形变换(压缩矩阵)
    将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z字形排列。比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:PAHNAPLSIIGYIR之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"。请......
  • asp 上传
    化境ASP无组件上传类-upload_5xsoft使用手册2.0what'snew1.使用了网友“梁无惧”提供的高效的处理方式,上传速度可提高一倍以上,可上传更大的文件。2.添加了form方法和file方法,把原来的form集和file改为objForm和objFile,避免了若上传时没......
  • 如何将typora的文件上传到博客园
    参考:https://www.cnblogs.com/HeroZhang/p/17534095.htmlhttps://www.cnblogs.com/HeroZhang/p/17534095.html......
  • Java实战:大文件分片上传与断点续传策略及其实际应用
    在许多应用场景中,处理大型文件上传可能成为开发人员面临的一项挑战。在网络环境不稳定,或者文件体积过大的情况下,传统的文件上传方式可能会出现问题。这时,文件分片上传和断点续传技术就显得至关重要。本文将向您展示如何使用Java实现这两种技术,并探讨其主要应用场景。文件分片上传......
  • 【HarmonyOS】一文教你如何通过内存图片方式使用image组件加载网络图片资源
    【关键字】内存图片方式、image组件、网络图片资源、api6、服务卡片1、写在前面之前写过一篇元服务卡片的开发指导,有需求的可以参考以下文章:【HarmonyOS】低代码开发之FA卡片开发流程在2.6初始化卡片部分,我们实现了加载网络资源的图片,但是直接使用image组件加载网络资源似乎在新版......
  • 【HarmonyOS】一文教你如何通过内存图片方式使用image组件加载网络图片资源
    ​【关键字】内存图片方式、image组件、网络图片资源、api6、服务卡片 1、写在前面之前写过一篇元服务卡片的开发指导,有需求的可以参考以下文章:【HarmonyOS】低代码开发之FA卡片开发流程在2.6初始化卡片部分,我们实现了加载网络资源的图片,但是直接使用image组件加载网络资......