Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of picture size compression by springboot

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report