图片压缩代码和实际操作页面

先编写一个小工具类

复制代码
package com.ai157.aigc.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class MyFileUtils {
    public static File convert(MultipartFile multipartFile) throws IOException {
        File file = new File(multipartFile.getOriginalFilename());
        try (InputStream is = multipartFile.getInputStream()) {
            org.apache.commons.io.FileUtils.copyInputStreamToFile(is, file);
        }
        return file;
    }
}

从前端页面上传上来,后端处理类

复制代码
package com.ai157.aigc.controller;
import com.ai157.aigc.utils.MyFileUtils;
import org.apache.commons.io.FileUtils;
import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

@RestController
public class FileSmallController {
    @PostMapping("/api/file/small")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
                                   @RequestParam("scale") Float scale,
                                   @RequestParam("fileType") String fileType
                                   ) {
        try {
            scale = scale/100;//由于我的界面上输入的是百分比数据,所以要除以100
            ByteArrayOutputStream  byteArrayOutputStream = new ByteArrayOutputStream();
            compress(MyFileUtils.convert(file),byteArrayOutputStream,scale);
            return Base64.getEncoder().encodeToString( byteArrayOutputStream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "File uploaded successfully!";
    }
   /**
    *这个是处理方法
   */
    public static void compress(File srcFile, ByteArrayOutputStream byteArrayOutputStream, float scale) {
        scale = (float) Math.sqrt(scale);
        try {
            BufferedImage srcImage = ImageIO.read(srcFile);
            int width = (int) (srcImage.getWidth() * scale);
            int height = (int) (srcImage.getHeight() * scale);
            BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            destImage.getGraphics().drawImage(srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH), 0, 0, null);
            ImageIO.write(destImage, "png", byteArrayOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意下:

1、scale = scale/100;这个为什么除以100,由于前端上传的不是小数,是百分比数值。

2、由于长度和宽度是等比缩小,如果长度是a,宽度是b,那(ax)*(bx)= scale*a*b

所以x平方等于scale。所以在计算前要开方下。

3、由于前端是以图片的方式渲染,所以以Base64的图片数据输出。

实际操作界面请访问以下页面

gpt114,伴你左右!

相关推荐
我材不敲代码1 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python
qq_333120971 小时前
C++高并发内存池的整体设计和实现思路_C 语言
java·c语言·c++
mh_f1 小时前
33.批量通过GET链接下载图片到指定文件夹下
java
金銀銅鐵1 小时前
[Java] 如何理解 class 文件中方法的 access flags?
java·后端
智研数智工坊1 小时前
SpringBoot4.0.6 + Security7.x + JWT 最新完整实战|无状态权限认证、统一异常处理、可直接落地
java·spring boot·spring security·jwt·权限认证
DIY源码阁2 小时前
JavaSwing宿舍管理系统 - MySQL版
java·数据库·mysql·eclipse
Han_han9192 小时前
递归相关题目:
java
kTR2hD1qb2 小时前
Claude Code Skill的介绍与使用
java·前端·数据库·人工智能
汤米粥2 小时前
python学习——核心语法三
java·python·学习
basketball6162 小时前
Kadane算法 C++实现
java·c++·算法