图片上传并压缩源码免费下载(等比例压缩或者原尺寸压缩)-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%的人还看了以下文章

  • Pycharm Django项目 NameError: name ‘os’ is not defined

    Pycharm Djarngo项目报错 NameError: name ‘os’ is not defined 原因:这里调用了os模块,但是文件头并没引用os模块解决办法:在settings.py文件头加上 import os

    2024年12月2日
    1.1K0
  • python 初学者练手上机实操五-循环语句练习

    一、题目:定义一个名称为numList的列表[1,5,9,8,12,43],使用for循环 遍历该列表,输出包含的元素 1、新建一个“for.py”文件。 2、编写程序。 3、调试程序。 4、排除错误。 二、、题目:定义一个名称为numTuple的元级(1,5,9,8,12,43),使用for循环 遍历该元组,输出包含的元素 三、分别使用for循环和whil…

    2023年5月26日
    15.8K1
  • Python数据分析入门实战一:统计分析用户学习数据

    Python数据分析要求: 使用 Python 基础知识分析用户学习数据 json 文件,并从文件中统计出中指定的数据项。 用户学习数据 json 文件下载: http://labfile.oss.aliyuncs.com/courses/764/user_study.json user_study.json 文件部分内容展示如下: {“minutes”: …

    2022年2月5日
    12.2K0
  • python 循环语句的应用:水仙花数判断及爱因斯坦阶梯编程

    一、水仙花数判断程序 1.任务内容: 水仙花数是一个三位整数,如153是一个水仙花数,是因为该数的百位的立方、十位的立方、个位的立方和等于该数本身,如下所示: 2.程序编写要求: 使用for语句完成; 统计水仙花数个数的值保存到变量中,要求自动进行统计 •输出结果如下所示: 153 是水仙数370 是水仙数371 是水仙数407 是水仙数三位数中有4个水仙数…

    2022年5月11日
    10.7K1
  • 输入python显示不是内部命令的原因及解决方法

    输入python显示不是内部命令的原因及解决方法输入python显示不是内部命令的原因及解决方法输入python显示不是内部命令的原因及解决方法输入python显示不是内部命令的原因及解决方法

    问题原因:没有将python的安装路径添加到环境变量中。 解决方法: 首先在桌面上右键点击“此电脑”,选择“属性”,弹出系统界面选择“高级系统设置”,进入系统属性界面后在“高级”选项中选中“环境变量”。 然后在“系统变量”中找到变量Path,双击Path变量进入编辑界面。 接着在编辑环境变量对话框中点击“新建”,添加Python的安装路径,之后一直点确定即可…

    2023年5月18日 编程开发
    6.3K1
  • JSP分页思想2—核心代码

    一、通过order by id desc limit ?,? 控制当前页面显示的记录 显示第几页,共几页,上一页 下一页 <% int PageSize=2; //一页显示的记录数 int RowCount=0; //记录总数 int PageCount=0; //总页数 int intPage; //待显示页码 int i;//循环变量 String…

    2019年10月30日
    18.3K0

发表回复

登录后才能评论