上传图片保存原始图,并且生成缩率图

使用:awt生成缩率图

代码如下:

package cn.j0.app.shuoba.service;

    import cn.j0.app.shuoba.entity.UserNote;
    import cn.j0.app.shuoba.mapper.UserNoteMapper;
    import cn.j0.app.shuoba.util.FileUtil;
    import org.apache.ibatis.annotations.Param;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.springframework.web.multipart.MultipartFile;

    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    /**
     * FileName: ActivityService
     * Author:   xuan zongjun
     * Date:     2019/5/5 11:22
     * Description: 用户笔记业务层
     */

    @Service
    public class UserNoteService {
        //缩率图的宽和高
        public static final int WIDTH = 100;
        public static final int HEIGHT = 100;

        @Autowired
        private UserNoteMapper userNoteMapper;

        @Value("${domain.upload}")
        private String showImgPath;

        /**
         * 添加
         *
         * @param file   图片
         * @param userId 用户ID
         * @param appId  产品ID
         * @return
         */
        public Object add(MultipartFile file, String userId, String appId) {
            Map<String, Object> resultMap = new HashMap<>();
            try {
                UserNote userNote = new UserNote();
                userNote.setUserId( userId );
                userNote.setAppId( appId );
                userNote.setCreateTime( new Date() );
                //大图地址
                String imageType = file.getContentType().substring( file.getContentType().indexOf( "/" ) + 1 );
                String noteImgUrl = FileUtil.saveFileAndFilePath( file, "noteImg" );
                String saveImg = noteImgUrl.substring( noteImgUrl.indexOf( "noteImg" ) - 1 );
                userNote.setNoteImgUrl(saveImg);
                //小图地址
                File imgFile = new File( noteImgUrl );
                String noteImgUrlSmall = createThumbnail( imgFile, noteImgUrl, imageType );
                String saveImgSmall = noteImgUrlSmall.substring( noteImgUrlSmall.indexOf( "noteImg" ) - 1 );
                userNote.setNoteImgUrlSmall( saveImgSmall );
                userNoteMapper.add( userNote );

                resultMap.put( "code", 1 );
                resultMap.put( "message", "保存成功" );
            } catch (Exception ex) {
                resultMap.put( "code", -1 );
                resultMap.put( "message", "保存失败" );
            }

            return resultMap;
        }

        /**
         * //生成缩率图
         * @param file          缩率图文件
         * @param noteImgUrl   文件的地址
         * @param imageType     文件的类型
         * @return
         */
        private String createThumbnail(File file, String noteImgUrl, String imageType) {
            OutputStream oStream = null;
            String noteImgUrlSmall = "";
            try {
                //缩略图的的文件名
                String ext = noteImgUrl.substring( noteImgUrl.lastIndexOf( "." ) );
                String uploadFileName = noteImgUrl.substring( 0, noteImgUrl.lastIndexOf( "." ) );
                noteImgUrlSmall = uploadFileName + "_small" + ext;
                oStream = new FileOutputStream( noteImgUrlSmall );

                BufferedImage image = ImageIO.read( file );
                int width = image.getWidth( null ); //获取原图宽度
                int height = image.getHeight( null );//获取原图高度
                int wrate = width / WIDTH;    //宽度缩略图
                int hrate = height / HEIGHT;//高度缩略图
                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
                ImageIO.write( bufferedImage, imageType, oStream );

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    oStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return noteImgUrlSmall;
        }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值