本篇内容主要讲解“springboot怎么实现图片大小压缩功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot怎么实现图片大小压缩功能”吧!
application.properties配置文件
#后端接收图片大小spring.servlet.multipart.max-file-size=50MBspring.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;/ public static byte[] imageSet(byte[] fromPath2) throws Exception { return imageChangeSize(fromPath2, 470, 635); } public static byte[] imageChangeSize(byte[] data, int max_wi, int max_he) throws Exception { ImageIO io = null; float rate = 1; int oldwi; int oldhe; int new_wi = 0; int new_he = 0; 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; 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="imagefunction 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怎么实现图片大小压缩功能”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!