java将图片压缩到指定大小以内并保存图片
1、压缩方法:
/*** 通过BufferedImage图片流调整图片大小* 指定压缩后长宽*/public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_AREA_AVERAGING);BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);return outputImage;}/*** 通过BufferedImage图片流调整图片大小* @param originalImage* @param reduceMultiple 缩小倍数* @return* @throws IOException*/public static BufferedImage resizeImage(BufferedImage originalImage, float reduceMultiple) throws IOException {int width = (int) (originalImage.getWidth() * reduceMultiple);int height = (int) (originalImage.getHeight() * reduceMultiple);Image resultingImage = originalImage.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);return outputImage;}/*** 压缩图片到指定大小* @param srcImgData* @param reduceMultiple 每次压缩比率* @return* @throws IOException*/public static byte[] resizeImage(byte[] srcImgData, float reduceMultiple) throws IOException {BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));int width = (int) (bi.getWidth() * reduceMultiple); // 源图宽度int height = (int) (bi.getHeight() * reduceMultiple); // 源图高度Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.setColor(Color.RED);g.drawImage(image, 0, 0, null); // 绘制处理后的图g.dispose();ByteArrayOutputStream bOut = new ByteArrayOutputStream();ImageIO.write(tag, "JPEG", bOut);return bOut.toByteArray();}/*** BufferedImage图片流转byte[]数组*/public static byte[] imageToBytes(BufferedImage bImage) {ByteArrayOutputStream out = new ByteArrayOutputStream();try {ImageIO.write(bImage, "jpg", out);} catch (IOException e) {e.printStackTrace();}return out.toByteArray();}/*** byte[]数组转BufferedImage图片流*/private static BufferedImage bytesToBufferedImage(byte[] ImageByte) {ByteArrayInputStream in = new ByteArrayInputStream(ImageByte);BufferedImage image = null;try {image = ImageIO.read(in);} catch (IOException e) {e.printStackTrace();}return image;}
2、保存方法:
/*** 保存照片文件** @param path 文件绝对路径* @param file 文件数据*/public void saveFile(String path, MultipartFile file) {if (file != null && !file.isEmpty()) {//保存文件到对应位置File dir = new File(path);if (!dir.getParentFile().exists()) {dir.getParentFile().mkdirs();}// 压缩到小于指定文件大小200kdouble targetSize = 200 * 1024;try {//从MultipartFile 中获取 byte[]byte[] bytes = file.getBytes();logger.info("头像图片压缩前大小:[{}]", bytes.length);while (bytes.length > targetSize) {float reduceMultiple = 0.5f;bytes = resizeImage(bytes, reduceMultiple);}logger.info("头像图片压缩后大小:[{}]", bytes.length);BufferedImage newImage = bytesToBufferedImage(bytes);//图像缓冲区图片保存为图片文件(文件不存在会自动创建文件保存,文件存在会覆盖原文件保存)ImageIO.write(newImage, "jpg", new File(path));} catch (IOException e) {//抛出异常}}}
java将图片压缩到指定大小以内并保存图片相关推荐
- 怎么把图片压缩到30k以内?如何图片压缩到指定大小?
在公务员考试报名时,需要提交个人证件照片,通常都会要求照片大小不能超过30kb,否则会导致上传失败.那么怎么把图片压缩到30k以内呢?想要把图片压缩到30kb就需要用到压缩图的图片压缩指定大小(图片压 ...
- 图片如何缩小到100k?怎么把图片压缩到指定大小?
有时候在平台上传图片会规定大小100k,图片如何缩小到100k呢?可以使用压缩图的图片压缩到指定大小功能,十分的方便一键操作就可以将图片压缩到100k以内,来看看具体操作步骤吧. 1.打开压缩图点击图 ...
- 图片不超过200kb怎么调整?一分钟学会图片压缩到指定大小
很多网站为了保证页面的流畅度会限制图片上传的kb大小,所以我们需要把图片压缩到指定大小(图片压缩到指定大小 图片压缩大小至指定kb以下-压缩图),那么有没有比较快捷的方法可以自定义压缩图片大小呢?今天 ...
- 如何将图片压缩到指定大小?压缩图片大小的方法
很多报名平台都对图片大小有固定的数值要求,所以不少小伙伴来问,怎么压缩图片200k以下(图片压缩到指定大小 图片压缩大小至指定kb以下-压缩图)?想要把图片压缩指定大小就要用到一些比较专业的工具,今天 ...
- JS将base64图片压缩至指定大小
base64图片压缩至指定大小 在开发中,通常在上传图片时,由于各种限制,需要将上传的图片压缩到某一大小范围内才能上传.在此提供以下方法实现该需求,复制可用. /*** 压缩图片到指定大小* @par ...
- 图片怎么压缩到200k以内?图片压缩怎么指定大小?
在某些情况下,我们需要将图片压缩到指定(https://www.yasuotu.com/imagesize)的大小,例如200k以内,在本文中,我们将用一个特别简单的方法来将图片压缩到200k以内的 ...
- 图片大小如何调整到10k?图片压缩到指定大小怎么弄?
在现代社交网络和在线平台上,上传的图片大小都会有限制,过大的图像会导致性能问题.加载时间延长和带宽消耗,所以图片压缩指定大小(https://www.yasuotu.com/imagesize)就成了 ...
- iOS图片压缩成指定大小
在iOS中涉及一些图片显示和处理问题,在实际中碰到过修改账户头像时上传头像出现 error = Error Domain=com.alamofire.error.serialization.respo ...
- 将图片压缩至指定大小Kb(Thumbnailator)
放代码前,先唠叨唠叨,舒缓一下我的郁闷之情. 今天下午改一个后台管理系统的需求,要求上传的图片要压缩到300kb,感觉不难,就开搞. 先撩拨一下度娘,搞点货出来瞅瞅,不复杂就干脆搬砖算了. 嗯,度娘不 ...
- C# 图片压缩(指定大小压缩和指定尺寸压缩)
一按大小压缩测试代码: 一测试效果: 一主要代码: /// <summary>/// 压缩图片至200 Kb以下/// </summary>/// <param name ...
最新文章
- Hadoop源代码分析 - MapReduce(转载)
- Java第一个程序(CMD环境)
- python 功能 代码_让你的Python代码实现类型提示功能
- memcached的最佳实践方案(转)
- [YTU]_2435 ( C++ 习题 输出日期时间--友元函数)
- Linux parted分区工具使用示例
- python中的sort,sorted
- linux mysql etc inid_Linux下mysql基本操作
- idea插件安装在哪个目录_从零开始编写自己需要的IntelliJ IDEA 插件
- WireShark下载:官网、源码
- 实时采集Canal快速入门
- 数据库系统原理——数据库编程
- 国际产品经理资格认证NPDP 2017
- 计算广告学-学习笔记
- WIN10 Wifi已连接无internet完美解决办法
- Android netd 简单分析和配置iptable
- 为什么要报考系统架构设计师考试
- python菜鸟教程 | print功能
- win7的Par虚拟机版优化(parellel desktop)
- 涂鸦 opengl简单应用1