springboot实现图片大小压缩功能

2022-07-15,,,

本文实例为大家分享了springboot实现图片大小压缩的具体代码,供大家参考,具体内容如下

application.properties配置文件

#后端接收图片大小
spring.servlet.multipart.max-file-size=50mb
spring.servlet.multipart.max-request-size=50mb

java工具类

import org.slf4j.logger;
import org.slf4j.loggerfactory;
import javax.imageio.imageio;
import java.awt.image.bufferedimage;
import java.awt.image.imageobserver;
import java.io.bytearrayinputstream;
import java.io.bytearrayoutputstream;
import java.io.fileinputstream;
import java.io.inputstream;
/
/**
     * 请求调用方法
     * gb
     *
     * @param frompath1 byte直接文件图片
     * @return 剪切后的byte文件
     */
    public static byte[] imageset(byte[] frompath1) throws exception {
        return imagechangesize(frompath1, 470, 635);
    }

    /**
     * @param data   byte类型图片
     * @param max_wi 图片修改后最大的宽
     * @param max_he 图片修改后最大的高
     * @return
     * @throws exception
     * @description 更改图片内容的大小 byte【】 类型
     */
    public static byte[] imagechangesize(byte[] data, int max_wi, int max_he) throws exception {
        imageio io = null;
        float rate = 1;
        /*图片的原始宽 120*/
        int oldwi;
        /*图片的原始高 120*/
        int oldhe;
        /*图片修改后的宽 0*/
        int new_wi = 0;
        /*图片修改后的高 0*/
        int new_he = 0;
        /*拿到byte图片*/
        inputstream is = new bytearrayinputstream(data);
        bufferedimage bufimg = imageio.read(is);
        /*图片的原始宽度*/
        oldwi = bufimg.getwidth();
        /*图片的原始高度*/
        oldhe = bufimg.getheight();
        //
        rate = (float) oldwi / (float) oldhe;
        /*如果图片的原宽大于最大宽度,并且原高小于等于最大高度。则证明图片过宽了,将图片宽度设置为最大宽度,此时需要等比例减小高度*/
        if (oldwi > max_wi && oldhe <= max_he) {
            new_wi = max_wi;
            new_he = new float((float) new_wi / rate).intvalue();
            /*如果图片的原宽和原高都大于或者都小于其所对应的最大值,则以任意一方为主(此处以最大高度为主)*/
        } else if (oldwi >= max_wi && oldhe >= max_he || oldwi <= max_wi && oldhe <= max_he) {
            new_he = max_he;
            new_wi = new float(new_he * rate).intvalue();
            /*如果图片的原宽小于于最大宽度,并且原高大于等于最大高度。则证明图片过高了,将图片宽度设置为最大高度,此时需要等比例减小宽度*/
        } else if (oldwi <= max_wi && oldhe > max_he) {
            new_he = max_he;
            new_wi = new float(new_he * rate).intvalue();
        }
//        system.err.println("原宽度:" + oldwi + "原高度:" + oldhe + "_" + rate);
        /*开始改变大小*/
        imageobserver ser = null;
        bufferedimage bf = new bufferedimage(new_wi, new_he, bufferedimage.type_int_rgb);
        bf.getgraphics().drawimage(bufimg, 0, 0, new_wi, new_he, null);
//        system.err.println("新宽度:" + bf.getwidth() + "-" + "新高度:" + bf.getheight());
        bytearrayoutputstream out = new bytearrayoutputstream();
        //转换编码格式jpeg
        imageio.write(bf, "jpeg", out);
        byte[] re = out.tobytearray();
        //logger.info("【图片剪切】| 图片原大小={}kb | 压缩后大小={}kb", (data.length / 1024), (re.length / 1024));
        return re;
    }

使用

controller

@requestmapping(value = "/getimage", method = requestmethod.post)
@responsebody
    public jsondata getidcart(@requestparam(value = "front", required = false) multipartfile front) throws exception {
        byte[] idcartfront;
        /**
         *  图片字节
         *  判断压缩 >300kb就压缩到300kb以下 300000k=300kb 设置宽高px--->转小kb
         */
        if (front.getsize() > size_defualt) {
            idcartfront = imageutils.imageset(front.getbytes());
        } else {
            idcartfront = front.getbytes();
        }

页面

<!--身份证正面-->
<div class="photo" id="photo" enctype="multipart/form-data">
         <input type="file" multiple="multiple" accept="image/*"
            onchange="getimg(event,'photo',1)" enctype="multipart/form-data" id="face" required/>
</div>

js

/** 获取照片 */
function getimg(e, eid, next) {
    var imgfile = e.target.files[0];
    var newimgfile = window.webkiturl.createobjecturl(imgfile);
    document.getelementbyid("" + eid + "").style.backgroundimage = "url(" + newimgfile + ")";

    if (next == 1) {
        front = imgfile;
    } else if (next == 2) {
        back = imgfile;
    } else if (next == 3) {
        face = imgfile;
    }
}
//ajax:
************************
//将file文件转为formdata,ajax传到后台
    formdata = new formdata();
    formdata.append('front', front);
  
               url: url,
            type: 'post',
            cache: false,
            data: formdata,
            processdata: false,
            contenttype: false,

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《springboot实现图片大小压缩功能.doc》

下载本文的Word格式文档,以方便收藏与打印。