图片上传并压缩源码免费下载(等比例压缩或者原尺寸压缩)-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 有哪些优势?为什么学 Python ?

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

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

    2018年10月18日 编程开发
    8.7K1
  • 推荐!学Python编程买什么样的电脑?(电脑配置清单)

    推荐!学Python编程买什么样的电脑?(电脑配置清单)推荐!学Python编程买什么样的电脑?(电脑配置清单)推荐!学Python编程买什么样的电脑?(电脑配置清单)推荐!学Python编程买什么样的电脑?(电脑配置清单)

    推荐购买台式机,性价比高于笔记本电脑。 当然对不差钱的可以买笔记本电脑,毕竟方便携带。 对于笔记本电脑: 推荐购买标准电压CPU的电脑(例如:第12代i7-12700H),H表示标准电压。 内存建议16G或以上 C盘建议固态硬盘256g以上 D盘建议1T以上(建议采用”内置“”固态硬盘+机械硬盘“的计算机,固态硬盘速度极快,用于运行系统和软件,机械硬盘空间大…

    2023年1月26日 编程开发
    10.9K0
  • python 初学者练手上机实操三

    一、题目:键盘输入三角形的三边,求三角形的周长。 1、新建一个triangle.py文件 2、编写程序。 3、调试程序。 4、排除错误。 二、题目:导入turtle包(import turtle),绘制边长为100的正方形。 要求: 1、新建一个“turtle2.py”文件 2、编写程序。 3、调试程序。 4、排除错误。 三、题目:打印诗“悯农” 要求: 1…

    2023年5月5日
    25.9K0
  • 【深度学习】:3分钟入门Dropout层

    深度神经网(DNN)中经常会存在一个常见的问题:模型只学会在训练集上分类(过拟合现象),dropout就是为了减少过拟合而研究出的一种方法。

    2023年1月15日
    8.6K0
  • Ubantu为应用程序添加桌面图标

    Ubantu为应用程序添加桌面图标Ubantu为应用程序添加桌面图标Ubantu为应用程序添加桌面图标Ubantu为应用程序添加桌面图标

    按要求配置应用程序参数,为应用程序添加桌面图标,实现系统快捷启动。 Ubuntu桌面的左边(默认在左边)有一个启动器,类似于window的任务栏。在Ubuntu18中叫dock(船坞),有时候又叫收藏夹。本文统一叫做启动器 在使用 Ubuntu 操作系统进行开发过程中,由于Ubuntu 系统其操作方式主要通过命令行终端进行交互,故启动软件也一般通过终端键入应…

    2022年9月1日 编程开发
    9.0K0
  • 简!修改Jupyter 默认打开目录的方法

    简!修改Jupyter 默认打开目录的方法简!修改Jupyter 默认打开目录的方法简!修改Jupyter 默认打开目录的方法简!修改Jupyter 默认打开目录的方法

    1、启动cmd,执行以下命令,查看 jupyter 配置文件路径 C:Users41588>jupyter notebook –generate-configWriting default config to: C:Users41588.jupyterjupyter_notebook_config.py 2、找到配置文件 jupyter_n…

    2023年7月19日 编程开发
    12.0K0

发表回复

登录后才能评论