Java 实现图片等比例压缩

Java实现等比例压缩

1. 使用Java源生类实现

2. 使用第三方插件(Thumbnailator)

开发步骤:

  1. 创建工程
  2. 编写页面
  3. 编写Controller
  4. 编写业务逻辑

  • HTML代码
<!DOCTYPE HTML>
<html>
<head>
<title>Java实现图片等比例压缩</title>
</head>

<body>
    
    <form action="uploadFile/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="fileNamePath" />
        <input type="submit" value="提交" />
    </form>
</body>
</html>

 

  • 控制器
@RequestMapping("upload")
    public String thumbnail(@RequestParam("fileNamePath") CommonsMultipartFile file, HttpSession session, Map<String, Object> map) {

        // 图片保存的的文件名称
        String uploadPath = "images";
        // 项目发布的路径
        String realUploadPath = session.getServletContext().getRealPath(uploadPath);
     
        String imgeUrl = "";
        String thumbnailImageUrl = "";

        // 上传图片
        imgeUrl = uploadService.uploadImage(file, uploadPath, realUploadPath);
        // 压缩图片
        thumbnailImageUrl = thumbnailService.thumbnail(file, uploadPath, realUploadPath);

        map.put("imgeUrl", imgeUrl);
        map.put("thumbnailImageUrl", thumbnailImageUrl);
        return "showImage";
    }

 

  • 业务逻辑
    • 上传图片
  @Override
  public String uploadImage(CommonsMultipartFile file, String uploadPath, String realUploadPath) {
        InputStream is = null;
        OutputStream os = null;

        try {

            is = file.getInputStream();
            String des = realUploadPath + File.separator + file.getOriginalFilename();

            os = new FileOutputStream(des);

            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) > 0) {
                os.write(buffer);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return uploadPath + File.separator + file.getOriginalFilename();
    }

 

    • Thumbnailator 等比例压缩图片
  public static final int WINDTH = 68;
   public static final int HEIGHT = 68;

    public String thumbnail(CommonsMultipartFile file, String uploadPath, String realUploadPath) {

        try {
            String des = realUploadPath + "/thum_" + file.getOriginalFilename();
            Thumbnails.of(file.getInputStream()).size(WINDTH, HEIGHT).toFile(des);
        } catch (IOException e) {
            e.printStackTrace();
        }
    return uploadPath + "/thum_" + file.getOriginalFilename();
}

 

 

    • Java源生等比例压缩图片
public String thumbnail(CommonsMultipartFile file, String uploadPath, String realUploadPath) {
        OutputStream os = null;

        String desc = realUploadPath + "/thum_" + file.getOriginalFilename();
        try {
            os = new FileOutputStream(desc);

            Image image = ImageIO.read(file.getInputStream());
            int width = image.getWidth(null);// 获得原图的宽度
            int height = image.getHeight(null);// 获得原图的高度

            int rete1 = width / WIDTH;// 宽度缩略比例
            int rete2 = height / HEIGHT;// 高度缩略比例

            int rete = 0;
            if (rete1 > rete2) {
                rete = rete1;
            } else {
                rete = rete2;
            }
            // 计算缩略图最总的宽度和高度
            int newWidth = width / rete;
            int newHeight = height / rete;

            BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            bufferedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, image.SCALE_SMOOTH), 0, 0, null);

            String imageType = file.getContentType().substring(file.getContentType().indexOf("/") + 1);
            ImageIO.write(bufferedImage, imageType, os);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return uploadPath + "/thum_" + file.getOriginalFilename();
    }

 

附件一:Maven添加Thumbnailatorjar  

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

 附件二:项目实例代码

  https://git.oschina.net/hxxiaodao/Thumbnail.git

 

posted @ 2017-07-21 16:12  hxdeng  阅读(2964)  评论(0编辑  收藏  举报