1.第一种方法返回byte数组,用于直接把流存放在数据库


public class Demo {public static void main(String[] args) {File f = new File("D:\1.jpg");try {InputStream is = new FileInputStream(f);ByteArrayOutputStream os = new ByteArrayOutputStream();byte[] b = resizeImage(is,os,1000,"png");} catch (Exception e) {e.printStackTrace();}}/*** resizeImage:(等比例压缩图片文件大小)* is  文件输入流* os  字节数组输出流在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组缓冲区中* size 新图片宽度* format 新图片格式*/public static byte[] resizeImage(InputStream is,ByteArrayOutputStream os,int size,String format) throws IOException{BufferedImage prevImage = ImageIO.read(is);double width = prevImage.getWidth();double height = prevImage.getHeight();double percent = size/width;int newWidth = (int)(width*percent);int newHeight = (int)(height*percent);BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);Graphics graphics = image.createGraphics();graphics.drawImage(prevImage, 0, 0, newWidth,newHeight,null);ImageIO.write(image, format, os);is.close();return os.toByteArray(); }
}

2.第二种返回压缩后的图片

public class Demo {public static void main(String[] args) {File f = new File("D:\\1.jpg");File f1 = new File("D:\\2.jpg");try {InputStream is = new FileInputStream(f);OutputStream os = new FileOutputStream(f1);resizeImage(is,os,500,"jpg");} catch (Exception e) {e.printStackTrace();}}/*** resizeImage:(等比例压缩图片文件大小)* is  文件输入流* os  字节数组输出流在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组缓冲区中* size 新图片宽度* format 新图片格式*/public static void resizeImage(InputStream is,OutputStream os,int size,String format) throws IOException{BufferedImage prevImage = ImageIO.read(is);double width = prevImage.getWidth();double height = prevImage.getHeight();double percent = size/width;int newWidth = (int)(width*percent);int newHeight = (int)(height*percent);BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);Graphics graphics = image.createGraphics();graphics.drawImage(prevImage, 0, 0, newWidth,newHeight,null);ImageIO.write(image, format, os);os.flush();is.close();os.close();}
}

等比例压缩图片文件大小相关推荐

  1. python等比例压缩图片_python(PIL)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图详解...

    #coding:utf-8 ''' python图片处理 @author:fc_lamp @blog:http://fc-lamp.blog.163.com/ ''' import Image as ...

  2. 压缩图片大小的java代码_java按比例压缩图片的源代码,用java如何把图片处理到指定大小...

    [要分析某个网页中的代码构成,需要某个结点下的内容.用此原始方法可以得到整个网页的源码.其实更简单的方法是使用 WebClient 或 HtmlUtil 等开源方式 .public class Ht ...

  3. 如何压缩图片文件大小到20K,一招搞定

    如何压缩图片文件大小到20K,想要将图片文件压缩到20K就需要用到压缩软件来进行操作,很多人不知道怎么操作压缩软件,下面小编就为大家分享一下压缩软件压缩图片到20K的方法. 操作软件:迅捷压缩软件 1 ...

  4. 使用Thumbnails等比例压缩图片

    如何使用Thumbnails等比例压缩图片 我是用MultipartFile来接收文件的 具体思路 直接上代码 我是用MultipartFile来接收文件的 最近项目中有一个需要是需要把上传的图片进行 ...

  5. php后台图片压缩_php实现等比例压缩图片

    /** * desription 压缩图片 * @param sting $imgsrc 图片路径 * @param string $imgdst 压缩后保存路径 */ public function ...

  6. PHP 按一定比例压缩图片,保持清晰度

    图片压缩是我们日常开发中经常使用的操作,在如今需求很多的情况往往,上传的一张图片会被压缩成不同比例的图片,每次去操作也是一件非常繁琐的事情,于是进行了封装了一个压缩图片的操作类,希望大家遇到后,不用再 ...

  7. python等比例压缩图片_python图片等比例压缩

    class ImageCompressUtil(object): # 等比例压缩 def resizeImg(self, **args): try: args_key = {'ori_img': '' ...

  8. java无损压缩图片以及按照比例压缩图片

    需求: 有一个列表加载封面图片 , 只做一个封面显示 , 无实际观察意义,但是图片的原图很大 , 造成资源加载浪费,所以按照比例压缩文件 import com.sun.image.codec.jpeg ...

  9. bitmap compress 图片压缩 图片文件大小处理

    android中图片加载到内存中所占空间大小计算:分辨率 height*width*一个像素所占空间大小 解析:decode时指定解码方式为ARGB_8888 代表用8位表示透明度(A),8位表示红色 ...

  10. 怎么压缩图片文件大小?小白都能学会的方法分享

    大家在上传图片时,是不是会经常遇到图片文件过大无法发送的情况?这时候我们经常需要想方设法的把图片裁剪成合适的大小或者反复下载保存来降低清晰度,达到可以上传的要求. 可是这样一番折腾之后,图片也失去了原 ...

最新文章

  1. 帖子删除显示帖子名称?
  2. 短篇连载(3),在校园里做比赛的那些日子,以及彻底告别钟爱的电子竞技事业...
  3. Service xxx does not have a SELinux domain defined
  4. 进程控制块PCB(进程描述符)
  5. Cassandra1.2文档学习(12)—— hint机制
  6. 《用户故事与敏捷方法》阅读笔记三
  7. Android Camera详解
  8. numpy中的argpartition
  9. 20151212Jquery 工具函数代码备份
  10. [label][responsive-web-design]网页响应测试各种尺寸的工具
  11. HigherHRNet代码复现问题集(assert isinstance(orig, torch.nn.Module))
  12. EfficientDet:目标识别领域的 EfficientNet
  13. PPT修行之路(二)-素材库
  14. windows自动化操作——程序员必备
  15. 百度图片推广怎么做(百度图片推广操作方法)
  16. consul kv迁移
  17. Facebook的普惠金融数字货币Libra
  18. 基于控制台的c语言游戏常用操作
  19. XTU OJ 1218 A+B VIII
  20. 分支限界法经典问题——FIFO、LIFO、LC、带时限的作业排序、15谜问题

热门文章

  1. JS动态添加Html
  2. python 解析pys文件,并将其写入txt和excel文件
  3. 利用EXCEL进行数据爬虫
  4. 分区副本重分配、数据迁移、副本扩缩容
  5. Trickbot 年度版本变化情况
  6. 温度记录仪,基础版,刚学习c时写的
  7. 如何将明细数据自动分类生成汇总报表
  8. lodop 条码类型说明
  9. 江南春新年围炉夜谈:如何破解增长焦虑?
  10. pytorch转为onnx格式,以及加载模型的params和GFLOPs方法