图片上传并压缩源码免费下载(等比例压缩或者原尺寸压缩)-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的正确指南!

    Python是一门新手友好、功能强大、高效灵活的编程语言。 然而很多同学在学习过程中,并没有找到正确的方式,这样不仅浪费了大量的时间与精力,也对学习的兴趣有一定打击。 125网页设计整理了一些初学者学习的几大误区分享给大家,帮助同学们更好地学习Python。 急于求成 很多对学习Python有兴趣的同学们,可能在刚开始学习时没有找准学习方式。大家只是一味地买…

    2022年8月13日
    3.9K0
  • python 初学者练手上机实操四

    1.用*打印一个如下所示的图形。(10分) * * * * * * * * * * print(‘*’) print(‘* *’) print(‘* * *’) print(‘* * * *’) 要求分别使用for和while语句实现 导入turtle包(impo…

    2023年5月5日
    6.9K0
  • Tomcat8启动一闪而过、Tomcat8启动闪退的解决办法(精)

    Tomcat8启动一闪而过、Tomcat8启动闪退的解决办法(精)Tomcat8启动一闪而过、Tomcat8启动闪退的解决办法(精)Tomcat8启动一闪而过、Tomcat8启动闪退的解决办法(精)Tomcat8启动一闪而过、Tomcat8启动闪退的解决办法(精)

    下载了Tomcat8,在CMD命令下输入命令:startup,Tomcat8启动一闪而过,测试http://localhost:8080/ 无法打开,网上找了很多文章都没解决,后来折腾出来了,特写教程分享给大家! Tomcat8.0 解压缩完,运行tomcat8.exe屏幕一闪就没了,运行tomcat8w.exe弹出个筐:  Unable to open t…

    2019年4月8日 编程开发
    24.1K0
  • Myeclipse设置JSP视图和代码显示在同一个窗口

    Myeclipse设置JSP视图和代码显示在同一个窗口Myeclipse设置JSP视图和代码显示在同一个窗口Myeclipse设置JSP视图和代码显示在同一个窗口Myeclipse设置JSP视图和代码显示在同一个窗口

    Myeclipse开发时,想同时查看页面及代码视图,如上图,而默认只能看到代码。 Myeclipse设置JSP页面和代码显示在同一个窗口的方法有两种。 一、在JSP页面上右击-open with-other… 在弹出的Editor selection窗口中选择web page editor,就可以了。 二、选择 window-preferences,如下图,…

    2018年12月17日 编程开发
    5.7K0
  • 实用sql查询语句详解2:高级查询

    上篇文章《实用sql查询语句详解1:给列取别名、查询部分行、多列排序》介绍了简单查询语句,这节课给大家讲解基本条件查询、集合函数、分组查询、子查询、并集和交集。 基本条件查询 比较运算符:>,>=,<,<=,=,!= between a and b,in(a,b,c),not exists,is null,like ‘%_’,or,a…

    2018年2月5日
    6.7K0
  • 第四章 数据库应用开发案例2

    本章重点讲述以下内容:
    4.4 应用JDBC调用存储过程
    4.5 数据源与连接池技术
    4.6 数据库程序开发案例

    2018年2月7日
    2.7K0

发表回复

登录后才能评论