当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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

先看效果:

原图:1.33m

处理后:27.4kb

关键代码:

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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网