图片上传并压缩源码免费下载(等比例压缩或者原尺寸压缩)-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)
江山如画的头像江山如画管理团队
上一篇 2019年6月24日 上午9:01
下一篇 2019年6月24日 上午11:13

99%的人还看了以下文章

  • python 字典的使用案例二:求平均分,并按平均分由高到低输出选手编号和最后得分

    校园好声音大赛,三位选手得分,由8位评委给出。 请根据评分表,将每们选手的得分去掉一个最高分和一个最低分后求平均分,并按照平均分由高到低输出选手编号和最后得分。 dicScores = {‘012’: [90, 94, 97, 86, 85, 89, 88, 85], ‘005’: [91, 91, 92, 98, 90, 96, 90, 95], ‘108…

    2020年1月22日
    18.9K0
  • createStatement参数详解,带参数与不带参数的区别

    Connection接口的createStatement方法,可用来创建向数据库发送SQL语句的对象,用于执行SQL语句。 常用代码: Statement stmt = con.createStatement(); Statement stmt = con.createStatement (int resultSetType,int resultSetCon…

    2018年10月23日
    9.8K0
  • 表单method方式为post或get中文乱码的解决方法

    表单method方式为post中文乱码解决方法 ■设置请求和响应的编码方式 request.setCharacterEncoding(“utf-8”); response.setCharacterEncoding(“utf-8”); 表单method方式为get中文乱码解决方法 ■治标的方法: new String(s.getBytes(”iso-8859-…

    2019年12月17日
    8.8K0
  • 第1课:1分钟了解C语言

    C语言的发展 最初程序员使用的程序设计语言是一种用二进制代码“0”和“1”形式表示的、能被计算机直接识别和执行的语言,称为机器语言。它是一种低级语言,用机器语言编写的程序不便于记忆、阅读和书写。通常不用机器语言直接编写程序。 在机器语言的基础上,设计出了汇编语言,它可以将机器语言用便于人们记忆和阅读的助记符表示,如ADD、SUB、MOV等。汇编语言适用于编写…

    2020年4月5日
    5.6K0
  • 1.Spring框架概述

    1.1、资源 1.2、Spring历史 1.3、框架特征与功能 1.4、Spring组成 1.5、Spring Boot与Spring Cloud 一、Spring框架概述 Spring是一个开源免费的框架,为了解决企业应用开发的复杂性而创建。Spring框架是一个轻量级的解决方案,可以一站式地构建企业级应用。Spring是模块化的,所以可以只使用其中需要的…

    2023年1月19日 编程开发
    7.6K0
  • Java课程设计报告-记事本源代码有流程图

    Java课程设计报告 题 目:简单记事本程序的设计年级专业:计算机科学与技术  软件工程学 号:学生姓名:指导老师: 目    录 摘要… 1 前言… 2 1需求分析… 2 1.1需求分析… 2 1.2功能设计… 3 2.概要设计… 3 2.1程序设计思路… 3 2.2程序运…

    2019年10月4日
    4.7K0

发表回复

登录后才能评论