在后端开发的过程中,都逃不开与文件传输特别是图片的传输打交道,但是因为现在各种拍照设备发展越来越快,拍出的照片更是越来越清晰,但是照片文件的大小也是越来越大了,手机拍照小则2M大则30M这在网络传输过程中谁顶得住呀!所以在用户发布照片,后端对图像文件进行保存的过程中压缩图像文件是必不可少的一个过程。
而Thumbnails就是一个很好的图像处理工具,他把复杂的图像处理封装的很好,只需要短短的一行代码就能完成对图像的压缩。
Thumbnails支持:
- 指定大小进行缩放
- 按照比例进行缩放
- 不按照比例,指定大小进行缩放
- 旋转
- 水印
- 裁剪
- 转化图像格式
- 输出到OutputStream
- 输出到BufferedImage
- 输出到ByteArrayOutputStream(OutputStream)
- 输出到ByteArrayInputStream(InputStream)
- 输出到byte[]
Thumbnails导入依赖
1
2
3
4
5
|
< dependency >
< groupId >net.coobird</ groupId >
< artifactId >thumbnailator</ artifactId >
< version >0.4.8</ version >
</ dependency >
|
一,指定大小进行缩放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//size(宽度, 高度)
/*
* 若图片横比200小,高比300小,不变
* 若图片横比200小,高比300大,高缩小到300,图片比例不变
* 若图片横比200大,高比300小,横缩小到200,图片比例不变
* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300
*/
Thumbnails.of( "images/a380_1280x1024.jpg" )
.size( 200 , 300 )
.toFile( "c:/a380_200x300.jpg" );
Thumbnails.of( "images/a380_1280x1024.jpg" )
.size( 2560 , 2048 )
.toFile( "c:/a380_2560x2048.jpg" );
|
二,单个图片等比例缩放
1
2
3
|
File file = new File( "c:\\test.png" );
Thumbnails.of( new FileInputStream(file)).scale( 3.0 ).toFile( new File( "c:\\yyyyy.png" ));
|
3.0是一个double类型的数字,缩放比例,大于1就是变大,小于1就是缩小
三,不按照比例,指定大小进行缩放
1
2
3
4
5
|
//keepAspectRatio(false) 默认是按照比例缩放的
Thumbnails.of( "images/a380_1280x1024.jpg" )
.size( 200 , 200 )
.keepAspectRatio( false )
.toFile( "c:/a380_200x200.jpg" );
|
四,批量产生缩略图
1
2
3
|
Thumbnails.of( new File( "D:\\pics" ).listFiles()).scale( 0.2 ).outputFormat( "png" )
.toFiles(Rename.PREFIX_DOT_THUMBNAIL);
|
五,控制图片质量,图片尺寸不变
1
2
3
|
File fromPic = new File( "C:\\Users\\Administrator\\Desktop\\IdCardPositive_987136936_1531741954688.jpeg" );
File toPic = new File( "C:\\Users\\Administrator\\Desktop\\IdCardPositive_987136936_08.jpeg" );
Thumbnails.of(fromPic).scale(1f).outputQuality( 0 .25f).toFile(toPic);
|
outputQuality就是用来控制图片质量的
六,给图片加水印
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Thumbnails.of(fromPic).scale( 0.8 )
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(waterPic), 0 .5f)
.outputQuality( 0 .8f).toFile(toPic);
//watermark(位置,水印图,透明度)
Thumbnails.of( "images/a380_1280x1024.jpg" )
.size( 1280 , 1024 )
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read( new File( "images/watermark.png" )), 0 .5f)
.outputQuality( 0 .8f)
.toFile( "c:/a380_watermark_bottom_right.jpg" );
Thumbnails.of( "images/a380_1280x1024.jpg" )
.size( 1280 , 1024 )
.watermark(Positions.CENTER, ImageIO.read( new File( "images/watermark.png" )), 0 .5f)
.outputQuality( 0 .8f)
.toFile( "c:/a380_watermark_center.jpg" );
|
fromPic是原图,waterPic是水印图片,toPic是生成后的图片
七,旋转图片
1
|
Thumbnails.of(fromPic).scale( 0.5 ).rotate( 90 ).toFile(toPic);
|
八,图片裁剪
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
Thumbnails.of(fromPic).sourceRegion(Positions.CENTER, 300 , 300 ).scale( 1.0 ).toFile(toPic);
//sourceRegion()
//图片中心400*400的区域
Thumbnails.of( "images/a380_1280x1024.jpg" )
.sourceRegion(Positions.CENTER, 400 , 400 )
.size( 200 , 200 )
.keepAspectRatio( false )
.toFile( "c:/a380_region_center.jpg" );
//图片右下400*400的区域
Thumbnails.of( "images/a380_1280x1024.jpg" )
.sourceRegion(Positions.BOTTOM_RIGHT, 400 , 400 )
.size( 200 , 200 )
.keepAspectRatio( false )
.toFile( "c:/a380_region_bootom_right.jpg" );
//指定坐标
Thumbnails.of( "images/a380_1280x1024.jpg" )
.sourceRegion( 600 , 500 , 400 , 400 )
.size( 200 , 200 )
.keepAspectRatio( false )
.toFile( "c:/a380_region_coord.jpg" );
|
九,WEB输出流图片
某些应用上传的图片可能质量比较高,但是用户在列表浏览的时候,又不想原图展示,因为带宽要求较高,此时可以降低图片质量(上面提到的outputQuality),以outputstream输出流的方式response给浏览器去展示
1
2
3
4
5
6
7
|
@RequestMapping ( "/getImages" )
public void getImages(HttpServletRequest request, HttpServletResponse response) throws IOException {
Thumbnails.of( "images/a380_1280x1024.jpg" ).
scale(1f).
outputQuality( 0 .5f).
outputFormat( "jpg" ).toOutputStream(response.getOutputStream());
}
|
十,图像的格式转换
1
2
3
4
5
6
7
8
9
10
|
//outputFormat(图像格式)
Thumbnails.of( "images/a380_1280x1024.jpg" )
.size( 1280 , 1024 )
.outputFormat( "png" )
.toFile( "c:/a380_1280x1024.png" );
Thumbnails.of( "images/a380_1280x1024.jpg" )
.size( 1280 , 1024 )
.outputFormat( "gif" )
.toFile( "c:/a380_1280x1024.gif" );
|
十一,输出到BufferedImage
1
2
3
4
5
6
|
//asBufferedImage() 返回BufferedImage
BufferedImage thumbnail = Thumbnails.of( "images/a380_1280x1024.jpg" )
.size( 1280 , 1024 )
.asBufferedImage();
ImageIO.write(thumbnail, "jpg" , new File( "c:/a380_1280x1024_BufferedImage.jpg" ));
|
十二,输出到ByteArrayOutputStream(OutputStream)
1
2
3
4
5
|
ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
Thumbnails.of( "images/a380_1280x1024.jpg" ).
scale(1f).
outputQuality( 0 .5f).
outputFormat( "jpg" ).toOutputStream(thumbnailOutputStream);
|
十三,输出到ByteArrayInputStream(InputStream)
1
2
3
4
5
6
|
ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
Thumbnails.of( "images/a380_1280x1024.jpg" ).
scale(1f).
outputQuality( 0 .5f).
outputFormat( "jpg" ).toOutputStream(thumbnailOutputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
|
十三,输出到byte[]
1
2
3
4
5
6
|
ByteArrayOutputStream handlerOutputStream = new ByteArrayOutputStream();
Thumbnails.of(inputStream).
scale(1f).
outputQuality( 0 .25f).
outputFormat( "jpg" ).toOutputStream(handlerOutputStream);
byte [] bytes = handlerOutputStream.toByteArray();
|
到此这篇关于java Thumbnails 图片处理的使用的文章就介绍到这了,更多相关java Thumbnails 图片处理 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/echizao1839/article/details/114023405