Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the function of picture size compression by springboot

Shulou Source: shulou.com Published: 2022-05-31 20:11:22 09月30日 Update

This article mainly explains "springboot how to achieve image size compression function," interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "springboot how to realize picture size compression function"!

application.properties configuration file

#Backend receive image size spring.servlet.multipart.max-file-size=50 MB spring.servlet.multipart.max-request-size=50MB

java tools

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;//** * Request method invocation * gb * * @param fromPath2 byte direct file image * @return Cut byte file */ public static byte[] imageSet(byte[] fromPath2) throws Exception { return imageChangeSize(fromPath2, 470, 635); } /** * @param data byte type picture * @param max_wi Maximum width after image modification * @param max_he Maximum height after picture modification * @return * @throws Exception * @Description Change the size of the image content byte [] Type */ public static byte[] imageChangeSize(byte[] data, int max_wi, int max_he) throws Exception { ImageIO io = null; float rate = 1; /* Original width of picture 120*/ int oldwi; /* Original height of picture 120*/ int oldhe; /* Image width after modification 0*/ int new_wi = 0; /* Image modified high 0*/ int new_he = 0; /* Get byte picture */ InputStream is = new ByteArrayInputStream(data); BufferedImage bufImg = ImageIO.read(is); /* Original width of image */ oldwi = bufImg.getWidth(); /* Original height of image */ oldhe = bufImg.getHeight(); // rate = (float) oldwi / (float) oldhe; /* If the original width of the image is greater than the maximum width and the original height is less than or equal to the maximum height. Then prove that the picture is too wide, set the picture width to the maximum width, and then reduce the height by equal proportion */ if (oldwi > max_wi && oldhe = max_wi && oldhe >= max_he ||oldwi to small kb */ if (front.getSize() > SIZE_DEFUALT) { IdCartFront = ImageUtils.imageSet(front.getBytes()); } else { IdCartFront = front.getBytes(); }

page

Tags: Picture width size maximum height original file function content method type cut learning practical deeper interest background picture content byte practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi Huawei Xiaomi NVidia