温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】

发布时间:2020-09-11 08:53:06 来源:脚本之家 阅读:296 作者:赤砂之蝎我爱罗 栏目:编程语言

本文实例讲述了Java实现的上传并压缩图片功能。分享给大家供大家参考,具体如下:

先看效果:

原图:1.33M

Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】

处理后:27.4kb

Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】

关键代码:

package codeGenerate.util;
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("C:\\spider\\3.png"),new File("C:\\spider\\3-1.jpg"),425,638,0.7f);
    //zipImageFile(new File("C:\\spider\\2.JPG"),new File("C:\\spider\\2-2.JPG"),425,638,0.7f);
    //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;
  }
}

说明:

1、根据需求大家可以自行设置质量参数quality,到底设置成多少,可以先看下效果在取值;

2、网上通用的方法用的是jdk自带jar包中方法,我这里衍生了一下:用Graphics2D,能够同时处理jpg和png格式;

3、new Color(255,255,255)是白色,等同于WHITE,但是用WHITE 的话,Linux下某些图片会有其它色值;

4、main中的宽425和高638可以根据自己的需求自行设置,但是对于长和宽一样的,按照400(小值的值425)*400来处理;

更多java相关内容感兴趣的读者可查看本站专题:《Java图片操作技巧汇总》、《java日期与时间操作技巧汇总》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》及《Java数据结构与算法教程》。

希望本文所述对大家java程序设计有所帮助。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI