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

  • 实用sql查询语句详解1:给列取别名、查询部分行、多列排序

    SQL(Structured Query Language)是用于访问和处理数据库的标准计算机语言,是所有数据库查询的语言,无论是高级查询还是低级查询,SQL查询语句的需求都是最常用的。 文章介绍了包括查询全部的行和列、给列取别名、查询部分行、多列排序、使用函数查询等。 SQL数据查询-SELECT语句基本结构 语句语法简单归纳为: SELECT selec…

    2018年2月5日
    11.3K0
  • plt.xlabel(‘产品名称’)报错‘str’object is not callable的解决方法

    运行居然报错了… 提示plt.xlabel(‘产品名称’)这一行有错误 报错信息 ‘str’object is not callable plt.xlabel(‘产品名称’)报错‘str’object is not callable的解决方法 重新运行x轴 描述信息终于出来了。 以防下次忘记,还是记录下来了。

    2022年8月13日
    8.2K0
  • itbegin编程教学云课堂使用介绍

    进入https://www.itbegin.com/apps    点击右上角 登录 登录后,点击院校通–院校 点击进入个人中心 点击“web开发技术课堂”,进入相应课程 点击:我的预习,可以看到预习的知识点,时间要求。请同学们一定要在结束时间前完成任务! 点击查看,可以看到需要预习的知识点,预习以观察,查看效果为主。 如要求:观察各标签的使用及…

    2018年3月14日
    9.5K0
  • python 集合的使用,案例详解

    集合的定义: 1.不同元素组成 2.无序 3.集合中的元素必须是不可变类型 创建集合 s = {1,2,3,4,5,6,7,8} >>> set_test = set(‘hello’) >>> set_test {‘h’, ‘l’, ‘e’, ‘o’}  # 由此可见集合中的元素不可重复,都是不同的 集合运算 集合之间也可…

    2020年1月22日
    18.4K0
  • Undefined variable: file python3不再支持file函数

    python处理文件时,使用file函数,提示错误:Undefined variable: file 反复查看语法,没有问题啊,最后查找到原因: python3不再支持file函数 替代方法:可以用open代替file 函数。

    2019年3月29日
    9.4K0
  • 动态网站开发技术asp、asp.net、php、jsp比较

    asp、asp.net、php、jsp技术简介 ASP 全称为Active Server Pages(中文译名为活动服务器页面),是微软公司推出的用于Web应用服务的一种编程技术.采用的脚本语言: VBScript 和JavaScript。 ASP.NET 微软公司很快公布了其宏伟的“Windows.NET”计划,发布了成为下一代网络服务框架的NGWS,同时…

    2018年3月15日
    3.8K0

发表回复

登录后才能评论