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

先编写一个小工具类

复制代码
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,伴你左右!

相关推荐
梵得儿SHI3 小时前
Java 反射机制核心类详解:Class、Constructor、Method、Field
java·开发语言·反射·class·constructor·java反射·java反射机制
m0_736927043 小时前
想抓PostgreSQL里的慢SQL?pg_stat_statements基础黑匣子和pg_stat_monitor时间窗,谁能帮你更准揪出性能小偷?
java·数据库·sql·postgresql
Jabes.yang3 小时前
Java面试大作战:从缓存技术到音视频场景的探讨
java·spring boot·redis·缓存·kafka·spring security·oauth2
Query*3 小时前
Java 设计模式——适配器模式进阶:原理深挖、框架应用与实战扩展
java·设计模式·适配器模式
Sirens.3 小时前
Java核心概念:抽象类、接口、Object类深度剖析
java·开发语言·github
Meteors.3 小时前
23种设计模式——中介者模式 (Mediator Pattern)详解
java·设计模式·中介者模式
望获linux4 小时前
【实时Linux实战系列】使用 u-trace 或 a-trace 进行用户态应用剖析
java·linux·前端·网络·数据库·elasticsearch·操作系统
焰火19994 小时前
[Java]基于Spring的轻量级定时任务动态管理框架
java·后端
Seven974 小时前
Springboot 常见面试题汇总
java·spring boot
程序员阿鹏4 小时前
49.字母异位词分组
java·开发语言·leetcode