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

  • 跟永哥学MVC:Jsp+Servlet+JavaBean开发后台登录程序

    跟永哥学MVC:Jsp+Servlet+JavaBean开发后台登录程序,125建站网原创,转载请务必注明出版。 Jsp+Servlet+JavaBean开发后台登录程序-实现一 login.html(视图) <form action=”loginServlet” method=”post”> 用户名: <input type=”text”…

    2018年2月22日
    14.6K0
  • MVC详解:模型(Model)-视图(View)-控制器(Controller)

    MVC(模型-视图-控制结构)是软件开发中常用的一种架构模式。它强制性的将输入、处理和输出分开。使应用程序被分成三个核心部件:模型 (Model)、视图(View)和控制(Controller)。它们各自处理自己的任务,有效地分离存储数据和展示数据功能模块以降低它们之间的耦合度。 MVC体系结构: 模型层主要负责保存和访问业务数据,执行业务逻辑和操作。这一层…

    2020年2月23日
    7.0K0
  • jsp写mysql数据库出现中文乱码

    今天用jsp做个图片书管理系统,向mysql数据库中存中文的时候显示乱码,如图书名: web???? 修改页面是http://localhost:8080/library/book?action=bookModifyQuery&ID=14 根据中文乱码的处理方法: 表单method方式为post或get中文乱码的解决方法 jsp:include包含h…

    2020年8月22日
    4.3K0
  • 500 Internal Server Error 错误原因及解决方法(图)

    500 属于服务器内部错误。如果其他网站可以打开,就这一网站打不开,那是该网站的服务器出了问题,跟你的电脑无关,如果其他网页都打不开,你就试着用工具修复一下浏览器。 500内部服务器错误的一般原因是IIS服务器无法解析ASP代码。 如:ASP语法出错、ACCESS数据库连接语句出错,文件引用与包含路径出错、使用了服务器不支持的组件如FSO等。 如果在Wind…

    2018年7月2日
    32.5K0
  • 跟永哥学MVC:jsp+javabean+servlet实现求圆的面积

    上一节课我们通过一个案例三种实现,教你理解Jsp、javabean、Servlet(精),今天我们仍然通过同一个案例求圆的面积,使用MVC:jsp+javabean+servlet来实现,深入理解MVC及Jsp、javabean、Servlet的分工和使用。 r5.jsp <form action=”servletCircle” Method=”pos…

    2018年2月22日
    7.3K0
  • 中文分词-逆向最大匹配法 “SyntaxWarning: “is“ with a literal. Did you mean “==“?”

    def cutB(sentence,dictB): result = [] sentenceLen = len(sentence) maxDictB = max([len(word) for word in dictB]) while sentenceLen > 0: word = ” for i in range(maxDictB, 0, -1):…

    2023年10月3日
    3.8K0

发表回复

登录后才能评论