java画缩率图

1、控制器代码

private int imgWidth=150;//缩率图宽度
private int imgHeight=150;//缩率图宽度

//上传图片
    @PostMapping("/imgOtherUpload")
    public ResponseResult uploadOtherImg(@RequestParam("multipartFile") MultipartFile multipartFile,Integer checkUserId) {
        try {

            if (multipartFile.isEmpty() || StringUtils.isBlank( multipartFile.getOriginalFilename() )) {
                return new ResponseResult( CommonCode.INVALID_PARAM );
            }
            String contentType = multipartFile.getContentType();
            if (!contentType.contains( "image" )) {
                return new ResponseResult( CommonCode.INVALID_PARAM );
            }
            String root_fileName = multipartFile.getOriginalFilename();
            logger.info( "上传图片:name={},type={}", root_fileName, contentType );

            String date = DateFormatUtils.format( new Date(), "/yyyy/MM/dd/" );
            String directoryName = "other";
            String  filePath = location + "/"+directoryName  + date;


            logger.info( "图片保存路径={}", filePath );
            String suff = root_fileName.substring( root_fileName.lastIndexOf( "." ) );
            //原图
            String uuid = UUID.randomUUID().toString();
            String file_name_max = uuid+"_max" + suff;
            try {
                FileUtils.saveFile( multipartFile, filePath, file_name_max );
            } catch (IOException e) {
                e.printStackTrace();
            }
            //缩率图
            String file_name_min = uuid+"_min" + suff;
            //
            FileUtils.thumbnail( multipartFile,filePath,file_name_min,imgWidth,imgHeight );

            HashMap<String, String> map = new HashMap<>();
            map.put( "img_path", "/"+directoryName+date + file_name_min );
            map.put( "img_show", domain +"/"+directoryName+ date + file_name_min );
            return new ResponseResult( CommonCode.SUCCESS, map );

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult( CommonCode.FAIL );
        }

    }

2、缩率图方法

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;


/**
     * 保存缩率图
     * @param multipartFile 图片文件
     * @param path          保存的路径
     * @param fileName      文件名
     * @param sWidth        缩率图宽度
     * @param sHeight       缩率图高度
     */
    public static void thumbnail(MultipartFile multipartFile, String path, String fileName,int sWidth,int sHeight ) {
        OutputStream oStream = null;
        try {
            String des = path + fileName;
            oStream = new FileOutputStream( des );
            //ImageIO获取图片流信息
            Image image = ImageIO.read( multipartFile.getInputStream() );
            int width = image.getWidth( null ); //获取原图宽度
            int height = image.getHeight( null );//获取原图高度
            int wrate = width / sWidth;    //宽度缩略图
            int hrate = height / sHeight;//高度缩略图
            int rate = 0;
            if (wrate > hrate) {//宽度缩略图比例大于高度缩略图,使用宽度缩略图
                rate = wrate;
            } else {
                rate = hrate;
            }
            //计算缩略图最终的宽度和高度
            int newWidth = width / rate;
            int newHeight = height / rate;

            BufferedImage bufferedImage = new BufferedImage( newWidth, newHeight, BufferedImage.TYPE_INT_RGB );
            //图片缩略图实现
            bufferedImage.getGraphics().drawImage( image.getScaledInstance( newWidth, newHeight, image.SCALE_SMOOTH ), 0, 0, null );
            //*image/jpeg
            String imageType = multipartFile.getContentType().substring( multipartFile.getContentType().indexOf( "/" ) + 1 );
            ImageIO.write( bufferedImage, imageType, oStream );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                oStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值