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

  • 分享一个非常实用的连接数据库javabean

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

    2020年12月6日
    7.2K0
  • AdaGrad算法—随机梯度下降算法

    AdaGrad算法,它可以根据自变量在每个维度的梯度值的大小来调整各个维度上的学习率,从而避免统一的学习率难以适应所有维度的问题。

    2023年10月9日
    4.1K0
  • Myeclipse设置JSP视图和代码显示在同一个窗口

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

    2018年12月17日 编程开发
    5.2K0
  • OpenCV-最优秀的Python人脸识别库安装及入门教程

    OpenCV库(open source computer vision library)是一个基于BSD许可(开源)发行的跨平台计算机视觉和机器学习软件库,可以运行在Linux、Windows、Android和Mac OS操作系统上,具有跨平台特性,轻量级且高效,实现了图像处理和计算机视觉方面的很多通用算法。 OpenCV用C++语言编写,它具有C ++,P…

    2020年12月8日
    14.0K2
  • oracle 计算时间差,包含年、月、日、时、分、秒

    在做系统时,如图书管理系统、车辆管理系统、事务管理系统等,经常需要我们计算两个日期相差多少天,图书是否应该归还,事情是否完成等。 今天给大家分享oracle 中如何计算时间差! oracle时间差是以天数为单位,所以换算成年月,日 select floor(to_number(sysdate-to_date(‘2018-11-02 15:55:03’,’yy…

    2018年2月13日
    4.6K0
  • 第三章 JSP技术2:JSP的内置对象及使用

    内置对象:为简化Web页面的开发过程,JSP提供了一些内置对象,这些对象不用声明就可以在JSP页面中直接使用,是JSP引擎自动创建的Java类实例。
    request对象
    response对象
    out对象
    session对象
    application对象

    2018年2月2日
    2.8K0

发表回复

登录后才能评论