gpt4 book ai didi

Java 实现图片压缩的两种方法

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Java 实现图片压缩的两种方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

问题背景.

典型的情景:Nemo社区中,用户上传的图片免不了要在某处给用户做展示.

如用户上传的头像,那么其他用户在浏览该用户信息的时候,就会需要回显头像信息了.

用户上传的原图可能由于清晰度较高而体积也相对较大,考虑用户流量带宽,一般而言我们都不会直接体积巨大的原图直接丢给用户让用户慢慢下载.

这时候通常我们会在服务器对图片进行压缩,然后把压缩后的图片内容回显给用户.

压缩方案:

这里主要找了两个java中常用的图片压缩工具库:Graphics和Thumbnailator.

1、Graphics:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
  * compressImage
  *
  * @param imageByte
  *      Image source array
  * @param ppi
  * @return
  */
public static byte [] compressImage( byte [] imageByte, int ppi) {
     byte [] smallImage = null ;
     int width = 0 , height = 0 ;
 
     if (imageByte == null )
         return null ;
 
     ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte);
     try {
         Image image = ImageIO.read(byteInput);
         int w = image.getWidth( null );
         int h = image.getHeight( null );
         // adjust weight and height to avoid image distortion
         double scale = 0 ;
         scale = Math.min(( float ) ppi / w, ( float ) ppi / h);
         width = ( int ) (w * scale);
         width -= width % 4 ;
         height = ( int ) (h * scale);
 
         if (scale >= ( double ) 1 )
             return imageByte;
 
         BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
         buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0 , 0 , null );
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         ImageIO.write(buffImg, "png" , out);
         smallImage = out.toByteArray();
         return smallImage;
 
     } catch (IOException e) {
         log.error(e.getMessage());
         throw new RSServerInternalException( "" );
     }
}

重点在于:

?
1
2
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0 , 0 , null );

2、Thumbnailator

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
  * compressImage
  *
  * @param path
  * @param ppi
  * @return
  */
public static byte [] compressImage(String path, int ppi) {
     byte [] smallImage = null ;
 
   try {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     Thumbnails.of(path).size(ppi, ppi).outputFormat( "png" ).toOutputStream(out);
     smallImage = out.toByteArray();
     return smallImage;
   } catch (IOException e) {
     log.error(e.getMessage());
     throw new RSServerInternalException( "" );
   }
}

实际测试中,批量的情境下,后者较前者更快一些.

以上就是Java 实现图片压缩的两种方法的详细内容,更多关于Java 图片压缩的资料请关注我其它相关文章! 。

原文链接:https://www.link-nemo.com/u/10001/post/227553 。

最后此篇关于Java 实现图片压缩的两种方法的文章就讲到这里了,如果你想了解更多关于Java 实现图片压缩的两种方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com