1、序 在实际项目中,有时为了响应速度,难免会对一些高清图片进行一些处理,比如图片压缩之类的,而其中压缩可能就是最为常见的。最近,阿粉就被要求实现这个功能,原因是客户那边嫌速度过慢。借此机会,阿粉今儿就给大家介绍一些一下我做这个功能时使用的Thumbnailator库。 Thumbnailator是一个优秀的图片处理的Google开源Java类库,专门用来生成图像缩略图的,通过很简单的API调用即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量。可毫不夸张的说,它是一个处理图片十分棒的开源框架。 支持:图片缩放,区域裁剪,水印,旋转,保持比例。 Thumbnailator官网:https:code。google。compthumbnailator 有了这玩意,就不用在费心思使用ImageIOAPI,Java2DAPI等等来生成缩略图了。 废话少说,直接上代码,先来看一个最简单的例子:2、代码示例2。1。新建一个springboot项目2。2。引入依赖thumbnailatordependencygroupIdnet。coobirdgroupIdthumbnailatorartifactIdversion0。4。8versiondependency2。3。controller 主要实现了如下几个接口作为测试:RestControllerpublicclassThumbnailsController{ResourceprivateIThumbnailsServicethumbnailsS指定大小缩放paramresourceparamwidthparamheightreturnGetMapping(changeSize)publicStringchangeSize(MultipartFileresource,intwidth,intheight){returnthumbnailsService。changeSize(resource,width,height);}指定比例缩放paramresourceparamscalereturnGetMapping(changeScale)publicStringchangeScale(MultipartFileresource,doublescale){returnthumbnailsService。changeScale(resource,scale);}添加水印watermark(位置,水印,透明度)paramresourceparampparamshuiyinparamopacityreturnGetMapping(watermark)publicStringwatermark(MultipartFileresource,Positionsp,MultipartFileshuiyin,floatopacity){returnthumbnailsService。watermark(resource,Positions。CENTER,shuiyin,opacity);}图片旋转rotate(度数),顺时针旋转paramresourceparamrotatereturnGetMapping(rotate)publicStringrotate(MultipartFileresource,doublerotate){returnthumbnailsService。rotate(resource,rotate);}图片裁剪paramresourceparampparamwidthparamheightreturnGetMapping(region)publicStringregion(MultipartFileresource,Positionsp,intwidth,intheight){returnthumbnailsService。region(resource,Positions。CENTER,width,height);}}3、功能实现 其实引入了这个Thumbnailator类库后,代码其实很少,因为我们只需要按照规则调用其API来实现即可。就个人而言,挺喜欢这种API的方式,简洁,易懂,明了。3。1指定大小缩放指定大小缩放若图片横比width小,高比height小,放大若图片横比width小,高比height大,高缩小到height,图片比例不变若图片横比width大,高比height小,横缩小到width,图片比例不变若图片横比width大,高比height大,图片按比例缩小,横为width或高为heightparamresource源文件路径paramwidth宽paramheight高paramtofile生成文件路径publicstaticvoidchangeSize(Stringresource,intwidth,intheight,Stringtofile){try{Thumbnails。of(resource)。size(width,height)。toFile(tofile);}catch(IOExceptione){e。printStackTrace();}} 测试: 3。2指定比例缩放 指定比例缩放scale(),参数小于1,缩小;大于1,放大 paramresource源文件路径 paramscale指定比例 paramtofile生成文件路径 publicstaticvoidchangeScale(Stringresource,doublescale,Stringtofile){ try{ Thumbnails。of(resource)。scale(scale)。toFile(tofile); }catch(IOExceptione){ e。printStackTrace(); } } 测试: 3。3添加水印添加水印watermark(位置,水印,透明度)paramresource源文件路径paramp水印位置paramshuiyin水印文件路径paramopacity水印透明度paramtofile生成文件路径publicstaticvoidwatermark(Stringresource,Positionsp,Stringshuiyin,floatopacity,Stringtofile){try{Thumbnails。of(resource)。scale(1)。watermark(p,ImageIO。read(newFile(shuiyin)),opacity)。toFile(tofile);}catch(IOExceptione){e。printStackTrace();}} 测试: 3。4图片旋转 图片旋转rotate(度数),顺时针旋转 paramresource源文件路径 paramrotate旋转度数 paramtofile生成文件路径 publicstaticvoidrotate(Stringresource,doublerotate,Stringtofile){ try{ Thumbnails。of(resource)。scale(1)。rotate(rotate)。toFile(tofile); }catch(IOExceptione){ e。printStackTrace(); } } 测试: 3。5图片裁剪图片裁剪sourceRegion()有多种构造方法,示例使用的是sourceRegion(裁剪位置,宽,高)paramresource源文件路径paramp裁剪位置paramwidth裁剪区域宽paramheight裁剪区域高paramtofile生成文件路径publicstaticvoidregion(Stringresource,Positionsp,intwidth,intheight,Stringtofile){try{Thumbnails。of(resource)。scale(1)。sourceRegion(p,width,height)。toFile(tofile);}catch(IOExceptione){e。printStackTrace();}} 测试: 说明:1。keepAspectRatio(booleanarg0)图片是否按比例缩放(宽高比保持不变)默认true2。outputQuality(floatarg0)图片质量3。outputFormat(Stringarg0)格式转换小结 值得注意的是,若png、gif格式图片中含有透明背景,使用该工具压缩处理后背景会变成黑色,这是Thumbnailator的一个bug,预计后期版本会解决。 代码地址:公众号回复【源码仓库】获得最后说两句(求关注) 最近大家应该发现微信公众号信息流改版了吧,再也不是按照时间顺序展示了。这就对阿粉这样的坚持的原创小号主,可以说非常打击,阅读量直线下降,正反馈持续减弱。 所以看完文章,哥哥姐姐们给阿粉来个在看吧,让阿粉拥有更加大的动力,写出更好的文章,拒绝白嫖,来点正反馈呗。 如果想在第一时间收到阿粉的文章,不被公号的信息流影响,那么可以给Java极客技术设为一个星标。 最后感谢各位的阅读,才疏学浅,难免存在纰漏,如果你发现错误的地方,由于本号没有留言功能,还请你在后台留言指出,我对其加以修改。