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

  • ADODB.Connection ���� ‘800a0e7a’ win7 IIS运行ASP常见问题及解决方法

    在win7系统通过 IIS运行ASP出现以下错误提示: ADODB.Connection 错误 ‘800a0e7a’ 很多人根据错误提示,去查数据库连接代码,其实数据库的连接代码没有错误。 出现ADODB.Connection 错误 ‘800a0e7a’的 原因 是64位Windows7操作系统中,IIS7应用程序池默认没有启用32位应用程序,而我们连接AC…

    2018年6月1日
    6.1K0
  • 什么是 Python ?Python 有哪些优势?为什么学 Python ?

    什么是 Python ?Python 有哪些优势?为什么学 Python ?什么是 Python ?Python 有哪些优势?为什么学 Python ?什么是 Python ?Python 有哪些优势?为什么学 Python ?什么是 Python ?Python 有哪些优势?为什么学 Python ?

    Python 是一种通用的脚本开发语言,比其他编程语言更加简单、易学,其面向对象特性甚至比 Java、C#、.NET 更加彻底,因此非常适合快速开发。Python 在软件质量控制、开发效率、可移植性、组件集成、库支持等方面均处于先进地位。

    2018年10月18日 编程开发
    8.3K1
  • Pandas读取excel:Excel file format cannot be determined解决方法

    Pandas读取excel时报错,excel表格不能被指定,是什么原因? 这个问题我搞了很久,最后终于搞明白了,网上各种什么utf-8呀,格式化或者另存都不行,我都试过了。 Excel file format cannot be determined解决方法 首先要确定excel已经放在项目目录下,路径是正确的。 一、然后确保安装了所需要的模块 pip in…

    2022年9月11日
    19.1K0
  • 分享一个非常实用的连接数据库javabean

    连接mysql数据库的javabean,修改url数据库连接字符串和驱动程序加载代码,即可成为通用的数据库连接BEAN。 package com.common; import java.sql.*; public class DataBaseConn { public  Connection conn = null; public void createCo…

    2020年12月6日
    8.3K0
  • JSP制作后台登录页面:login.jsp

    设计数据库employee,manager表,字段:user_id (主键,自增1,int类型),user_name, password 创建数据源:employee_dsn 制作登录页面login.jsp 登录页面 login.jsp代码: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transition…

    2020年12月6日
    6.9K0
  • Pillow-优秀的Python图像处理库安装及入门教程

    Pillow库是Python 图像处理库(Python image library)的一个派生分支,提供了广泛的文件格式(BMP,PNG,JPEG等)支持,提供基本的图像处理能力,如: 图像存储、图像显示、改变图像大小,旋转图像,图像格式转换,色场空间转换,图像增强,直方图处理,插值和滤波等等。 比起OpenCV库的图像处理,功能有限,但函数使用非常方便,大…

    2020年12月8日
    4.8K0

发表回复

登录后才能评论