图片上传并压缩源码免费下载(等比例压缩或者原尺寸压缩)-java

项目开发时,需要手机拍照,然后上传图片,因为项目记录数非常多,每条记录需要3张照片,而手机相机越来越好,分辨率也高,就要压缩后再上传。

中国网页设计今天分享的图片上传并压缩方法支持等比例压缩或者原尺寸压缩两种。

可自行设置图片质量参数quality,能够同时处理jpg和png格式,也可把PNG转jpg或jpg转PNG。

宽度和高度可以根据项目实际需求自行设置,需要等比例缩放的话,可只设置width或height另一个为0即可。

图片上传并压缩源码免费下载(等比例压缩或者原尺寸压缩)-java

压缩前图片图片上传并压缩源码免费下载(等比例压缩或者原尺寸压缩)-java

压缩后图片

package imageZip;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class ImageZipUtil {  
 
 public static void main(String[] args) {
  //zipWidthHeightImageFile(new File("d:\\3.png"),new File("d:\\3-1.jpg"),400,500,0.6f);		
  zipImageFile(new File("d:\\3.png"),new File("d:\\3-1.jpg"),400,0,0.6f);		
  //zipImageFile(new File("C:\\spider\\3.jpg"),new File("C:\\spider\\3-3.jpg"),425,638,0.7f);		
  System.out.println("ok");
 }
  
    /** 
     * 根据设置的宽高等比例压缩图片文件<br> 先保存原文件,再压缩、上传 
     * @param oldFile  要进行压缩的文件 
     * @param newFile  新文件 
     * @param width  宽度 //设置宽度时(高度传入0,等比例缩放) 
     * @param height 高度 //设置高度时(宽度传入0,等比例缩放) 
     * @param quality 质量 
     * @return 返回压缩后的文件的全路径 
     */  
    public static String zipImageFile(File oldFile,File newFile, int width, int height,float quality) {  
        if (oldFile == null) {  
            return null;  
        }  
        try {  
            /** 对服务器上的临时文件进行处理 */  
            Image srcFile = ImageIO.read(oldFile);  
            int w = srcFile.getWidth(null);  
            int h = srcFile.getHeight(null);  
            double bili;  
            if(width>0){  
                bili=width/(double)w;  
                height = (int) (h*bili);  
            }else{  
                if(height>0){  
                    bili=height/(double)h;  
                    width = (int) (w*bili);  
                }  
            }  
            
            String srcImgPath = newFile.getAbsoluteFile().toString();
            System.out.println(srcImgPath);
            String subfix = "jpg";
    		subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());
 
    		BufferedImage buffImg = null; 
    		if(subfix.equals("png")){
    			buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    		}else{
    			buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    		}
 
    		Graphics2D graphics = buffImg.createGraphics();
    		graphics.setBackground(new Color(255,255,255));
    		graphics.setColor(new Color(255,255,255));
    		graphics.fillRect(0, 0, width, height);
    		graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);  
 
    		ImageIO.write(buffImg, subfix, new File(srcImgPath));  
  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return newFile.getAbsolutePath();  
    }  
  
    /** 
     * 按设置的宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传 
     * @param oldFile  要进行压缩的文件全路径 
     * @param newFile  新文件 
     * @param width  宽度 
     * @param height 高度 
     * @param quality 质量 
     * @return 返回压缩后的文件的全路径 
     */  
 public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height,float quality) {  
        if (oldFile == null) {  
            return null;  
        }  
        String newImage = null;  
        try {  
            /** 对服务器上的临时文件进行处理 */  
            Image srcFile = ImageIO.read(oldFile);            
            String srcImgPath = newFile.getAbsoluteFile().toString();
            System.out.println(srcImgPath);
            String subfix = "jpg";
    		subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());
 
    		BufferedImage buffImg = null; 
    		if(subfix.equals("png")){
    			buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    		}else{
    			buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    		}
 
    		Graphics2D graphics = buffImg.createGraphics();
    		graphics.setBackground(new Color(255,255,255));
    		graphics.setColor(new Color(255,255,255));
    		graphics.fillRect(0, 0, width, height);
    		graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);  
 
    		ImageIO.write(buffImg, subfix, new File(srcImgPath));  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return newImage;  
    }  
}

 

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/3553.html

(0)
江山如画的头像江山如画管理团队
免费网站打包APP,网址打包APP教程 – HBuilder
上一篇 2019年6月24日 上午9:01
编写第一个Python程序——输出HelloWorld并运行
下一篇 2019年6月24日 上午11:13

99%的人还看了以下文章

  • python 递归函数使用示例,求两个整数的最大公约数(欧几里得算法)

    用于计算两个整数的最大公约数的递归算法称为欧几里得算法,其计算原理依赖于定理: 两个整数的最大公约数等于其中较小的那个数和两数相除余数的最大公约数。 公式为:gcd(a,b)=gcd(b, a mod b) 递归函数使用示例, def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) prin…

    2020年1月31日
    10.9K0
  • 上机实战六 Servlet的开发 

      建议学时:6 2学时 一、编写Servlet,FirstServlet.java 通过URL访问该Servlet,显示这是我的第一个Servlet。 二、使用servlet实现求圆的面积,在r4.jsp页面提交表单,servlet负责接收用户请求、计算并显示圆的面积。 具体页面效果如下: 三、jsp+javabean+servlet实现求三角形的面积和周…

    2018年11月13日
    11.5K0
  • 如何设计单元测试用例,单元测试快速入门教程四

    测试人员在实际工作中根据不同覆盖要求设计面向代码的单元测试用例,运行测试用例后至少应实现如下覆盖需求: 对程序模块的所有独立的执行路径至少覆盖一次; 对所有的逻辑判定,真假两种情况至少覆盖一次; 在循环的边界和运行界限内执行循环体; 测试内部数据结构的有效性等。 至少应设计覆盖如下需求的基于功能的单元测试用例: 测试程序单元的功能是否实现; 测试程序单元性能…

    2018年4月18日
    4.9K0
  • python 集合使用案例:选修课统计

    相关阅读:python 集合的使用,案例详解 本学期学校共开设了3门选修课,一个班有25位学生,选修的情况如下: 选修1号课程的同学有: set1 = {‘张三’, ‘李四’, ‘王五’, ‘马六’, ‘赵七’, ‘钱八’} 选修2号课程的同学有: set2 = {‘姬一’, ‘孙必’, ‘周冲’, ‘王五’, ‘方向’, ‘张玉’} 选修3号课程的同学有:…

    2020年1月22日
    8.9K0
  • 程序设计基础(C语言)—教学设计、教案

    教学设计——程序设计基础 教学基本信息 课程名称 程序设计基础 性质 专业基础课 学分 3 学时 48 题目 数据类型 专业年级 软件工程专业一年级 教材 书名:C程序设计(第五版) 出版社:清华大学出版社    出版日期: 2017年8月 教学背景分析 一、学习内容分析: 本节课要介绍的知识点——数据类型比较简单,但都是概念。对于这些陌生的、枯燥的纯概念性…

    2020年4月10日
    14.3K0
  • IntelliJ IDEA 设置代码提示或自动补全的快捷键 (附IntelliJ IDEA常用快捷键)

    IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N  查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如get,set方法,构造函数等)Ctrl+E或者Alt+Shift+C  最近更改的代码Ctrl+R 替换文本Ctrl+F …

    2020年8月20日
    7.6K0

发表回复

登录后才能评论