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

  • 全!最详细的mysql新手安装教程

    全!最详细的mysql新手安装教程全!最详细的mysql新手安装教程全!最详细的mysql新手安装教程全!最详细的mysql新手安装教程

    数据库排名:Oracle,mysql和 Microsoft SQL Server 仍占据前三名。 Mysql是什么? mysql是一个关系型数据库软件,由瑞典MySQL AB公司开发,目前属于Oracle公司。 为什么使用mysql? 1)mysql是开源的,所以你不需要支付额外的费用就能使用。 2)mysql支持大型的数据库。可以处理拥有上千万条记录的大型…

    2023年1月28日 编程开发
    7.4K0
  • Python+ 人工智能软件工程师要学习哪些课程?

    1. Python 软件开发基础 2. Python 高级编程 3. Python 全栈式 WEB 工程师 4. Python 爬虫工程师、大数据分析工程师、人工智能工程师

    2018年12月27日
    15.8K0
  • 第一个Tkinter程序

    Tkinter(Python GUI编程)从入门到精通(一) 上一篇文章介绍什么是Tkinter?tkinter的优缺点,tkinter的开发工具。 今天我们在上一篇的基础上,开始给主窗口添加文本、按钮,并给按钮绑定一个事件,点击按钮弹出一个消息窗口。 创建label label=tk.Label(root,text=”hello GUI&#82…

    2022年8月25日
    3.4K0
  • python 函数,字典,列表使用综合实例(经典)

    现有一字典: dict1 = {’01’: [67, 88, 45], ’02’: [97, 68, 85], ’03’: [97, 98, 95], ’04’: [67, 68, 45], } 存放着学生的学号和成绩。成绩列表中的3个数据分别是学生的语文、数学、英语成绩。 要求: 1.编写函数,返回每门成绩均大于等于85的学生的学号。 dict1 = {‘…

    2020年1月31日
    13.6K0
  • idea不识别@webServlet注解,javax.servlet.htttp 找不到的解决方法

    idea不识别@webServlet注解,javax.servlet.htttp 找不到的解决方法idea不识别@webServlet注解,javax.servlet.htttp 找不到的解决方法idea不识别@webServlet注解,javax.servlet.htttp 找不到的解决方法idea不识别@webServlet注解,javax.servlet.htttp 找不到的解决方法

    在servlet3.0以后,web.xml中对Servlet配置,可以通过@WebServlet注解配置.下面是@WebServlet的属性列表: 属性名 类型 描述 name String 指定Servlet 的 name 属性,等价于 <servlet-name>。如果没有显式指定,则该 Servlet 的取值即为类的全限定名。 value …

    2020年8月22日 编程开发
    19.1K0
  • 如何设置mysql自动更新创建时间和更新时间

    做项目时,希望:新增记录时,mysql自动将系统的当前时间set到创建时间和更新时间这两个字段中。更新记录时,mysql只update更新时间字段的时间。 设置mysql自动更新创建时间和更新时间的方法: 找到表中创建时间和更新时间的字段,将其修改为下列代码。创建时间字段 ‘creat_time’ timestamp NULL DEFAULT CURRENT…

    2018年12月24日
    9.6K0

发表回复

登录后才能评论