Thumbnailator处理图片
读取源图
of(String... files)
of(File... files)
of(InputStream... inputStreams)
of(URL... urls)
输出文件
toFile(String outFilepath)
toFile(File outFile)
toOutputStream(OutputStream os)
缩放
size(int width,int height)
width(int width)
height(int height)
forceSize(int width, int height)
scale(double scaleWidth, double scaleHeight)
scale(double scale)
裁剪
sourceRegion(int x, int y, int width, int height)
sourceRegion(Position position, int width, int height)
sourceRegion(Position position, Size size)
sourceRegion(Region sourceRegion)
sourceRegion(Rectangle region) crop(Position position)
crop(Position position)
覆盖
allowOverwrite(boolean allowOverwrite)
输出格式
outputFormat(String format)
输出质量
outputQuality(float quality)
水印
watermark(BufferedImage image)
watermark(BufferedImage image, float opacity)
watermark(Position position, BufferedImage image, float opacity)
watermark(Watermark w)
旋转
rotate(double angle)
读取源图
of(String... files)
本地的字符串路径,写的可以传递多个但是我试了多个会报错,最好只用一个。
Thumbnails.of("C:\\Users\\86152\\Desktop\\1\\原图.png").scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
of(File... files)
将字符串抽象成File也可以。
Thumbnails.of(new File("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
of(InputStream... inputStreams)
从输入流读取源文件。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
of(URL... urls)
从网络流读取源文件。
Thumbnails.of(new URL("https://images0.cnblogs.com/blog/651487/201410/191023445446341.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
输出文件
toFile(String outFilepath)
将图片输出到本地,接收字符串路径。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
toFile(File outFile)
将图片输出到文件。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).toFile(new File("C:\\Users\\86152\\Desktop\\1\\1.jpg"));
toOutputStream(OutputStream os)
将图片输出到输出流。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).toOutputStream(new FileOutputStream("C:\\Users\\86152\\Desktop\\1\\1.jpg"));
缩放
size(int width,int height)
size(int width,int height)会对图片进行缩放,会改变图片的分辨率随之图片的大小也会改变,但它并不会严格按照我们输入的width,height进行缩放,因为这样图片的比例可能不对称,最终的结果会稍微的微调。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).size(500, 500).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
width(int width)
width(int width)和size(int width,int height)的区别是它允许你单独指定width然后它会自动根据width自适应的对height进行缩放。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).width(100).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
height(int height)
height(int height)和width(int width)的区别很明显,它指定height,width会自动根据height自适应的缩放。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).height(100).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
forceSize(int width, int height)
forceSize(int width, int height)跟size(int width,int height)的区别是它会严格按照参数width,height对图片进行缩放。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).forceSize(111,222).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
scale(double scaleWidth, double scaleHeight)
scale(double scaleWidth, double scaleHeight)根据比例对图片进行缩放,它是严格按照参数比例的,如果是1则是原图,如果大于1则分辨率也会增大。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(0.1d, 0.5).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
scale(double scale)
scale(double scale)和scale(double scaleWidth, double scaleHeight)的别是,指定一个参数就可以代表width和height。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(0.1d).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
裁剪
sourceRegion(int x, int y, int width, int height)
sourceRegion(int x, int y, int width, int height)四个参数分别是指定从原图片的X轴,Y轴,向右取width的宽,height的高开始裁剪,裁剪的区域就是新的图片。
出现以下错误,缺少size,也就是说当指定了需要裁剪的图片区域后,要先变成一个新的图片必须找到一个容器存放,可以选择缩放里的几个函数。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).sourceRegion(0, 0, 1920, 1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
从下图看缩略图原图和这个图片其实差不多的包括的它们的分辨率,但它的大小其实是改变了。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).sourceRegion(0, 0, 1920, 1080).size(1920, 1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
这次我们将输出的width,height改成100,100。图片的分辨率和大小都改变了。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).sourceRegion(0, 0, 1920, 1080).size(100, 100).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
换成forceSize后整体的效果就出来了,因为他严格按照指定的width和height改变输出图片。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).sourceRegion(0, 0, 1920, 1080).forceSize(100, 100).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
sourceRegion(Position position, int width, int height)
sourceRegion(Position position, int width, int height)是sourceRegion(int x, int y, int width, int height)重载,Position将X,Y封装成了对象,Position是一个接口它的唯一实现类是Positions。TOP_LEFT代表的是X(0),Y(0)。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).sourceRegion(Positions.TOP_LEFT, 1920, 1080).forceSize(1920,1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
sourceRegion(Position position, Size size)
sourceRegion(Position position, Size size)是sourceRegion(int x, int y, int width, int height)重载它将X,Y封装成了Position,将width,height封装成了Size,Size是一个接口它的唯一实现类是RelativeSize这个类的构造器接收一个参数,没错一个参数就是百分比,而且是width,height的百分比就像scale(double scale)。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).sourceRegion(Positions.TOP_LEFT, new RelativeSize(1)).forceSize(1920, 1080)
.toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
sourceRegion(Region sourceRegion)
sourceRegion(Region sourceRegion)其实就是上边三个重载形式最终组成的对象,Region的构造器是Position和Size。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).sourceRegion(new Region(Positions.TOP_LEFT, new RelativeSize(1))).forceSize(1920, 1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
sourceRegion(Rectangle region)
sourceRegion(Rectangle region)参数Rectangle跟Region差不多,不过它有无参构造,如果使用无参构造Rectangle()相当于sourceRegion(0, 0, 0, 0)如果用这个会报错,因为width,height为0。同样你也可以直接指定它的四个参数。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).sourceRegion(new Rectangle(0, 0, 1920, 1080)).forceSize(1920, 1080).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
crop(Position position)
crop(Position position)这个方法就有点意思了,相比sourceRegion(int x, int y, int width, int height)少了width,height那它是如何指定X,Y的横向纵向距离呢?谜底是它使用size(int width, int height)。也就是输出的width,height既是裁剪的width,height。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).crop(Positions.TOP_LEFT).size(500, 500).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
覆盖
allowOverwrite(boolean allowOverwrite)
是否允许覆盖源文件,如果要输出的图片已经存在(路径和文件名相同),默认是覆盖掉源文件,使用此方法传值false则可以避免文件覆盖。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).crop(Positions.TOP_LEFT).size(500, 400).allowOverwrite(false).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
输出格式
outputFormat(String format)
outputFormat(String format)这个方法用于更改输出文件的类型,如果指定了输出类型,那输出文件名就不要带文件类型了。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).outputFormat("png").toFile("C:\\Users\\86152\\Desktop\\1\\1");
输出质量
outputQuality(float quality)
outputQuality(float quality)设置输出图片的质量,0.1-1,1表示最大质量。下面看三种情况下的输出对比。
1 缩略模式,width,height保持原图1:1。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
2 缩略模式,width,height保持原图1:1,outputQuality(1f)输出全量质量。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).outputQuality(1f).toFile("C:\\Users\\86152\\Desktop\\1\\2.jpg");
3 缩略模式,width,height保持原图1:1,outputQuality(0.5f)输出一半质量。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).outputQuality(0.5f).toFile("C:\\Users\\86152\\Desktop\\1\\3.jpg");
水印
watermark(BufferedImage image)
watermark(BufferedImage image)给图片打水印,BufferedImage是水印图片的抽象表示。使用ImageIO.read(InputStream in)构造一个水印的图片的表示类。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg"))).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
watermark(BufferedImage image, float opacity)
watermark(BufferedImage image, float opacity)和watermark(BufferedImage image)的区别是第二个参数它表示水印的不透明度从0.1-1,不指定则是0.5,下图设置不透明度为0.1
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 0.1f).toFile("C:\\Users\\86152\\Desktop\\1\\2.jpg");
下图设置不透明度为0.1。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).watermark(ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f).toFile("C:\\Users\\86152\\Desktop\\1\\3.jpg");
watermark(Position position, BufferedImage image, float opacity)
watermark(Position position, BufferedImage image, float opacity)是上边两个的完整体,默认的水印位置在输出图片的中间,使用Position指定水印输出位置。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
如果需要打上多个水印,也可以连续调用watermark。下图在输出图底部中间和底部右边打上水印。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f).watermark(Positions.BOTTOM_CENTER,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
watermark(Watermark w)
watermark(Watermark w)其实就是把Position,BufferedImage,opacity封装成了Watermark对象,可以使水印共同使用。
Watermark watermark1 = new Watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f);
Watermark watermark2 = new Watermark(Positions.BOTTOM_LEFT,ImageIO.read(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\水印.jpg")), 1f);
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).watermark(watermark1).watermark(watermark2).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
旋转
rotate(double angle)
rotate(double angle)对输出图片做旋转操作。
Thumbnails.of(new FileInputStream("C:\\Users\\86152\\Desktop\\1\\原图.png")).scale(1f).rotate(180).toFile("C:\\Users\\86152\\Desktop\\1\\1.jpg");
Thumbnailator处理图片的更多相关文章
- java使用Thumbnailator处理图片
Thumbnailator是一款不可多得的处理图片的第三方工具包,它写法简单到让人无法相信,Java本身也有处理图片压缩的方法,但是代码冗长到让人痛不欲生,在篇末会给出Java本身的实现方式,做下对比 ...
- Java语言利用Google的Thumbnailator处理图片
导读:在Java中制作高质量的缩略图可能是一项相当困难的任务,或者图片质量高页面加载慢等等原因使需要对图片进行处理,但是又不想写太多的代码太麻烦去处理图片,那么就可以使用Thumbnailator—— ...
- thumbnailator + webp-imageio-core实现java处理图片支持webp
thumbnailator提供了便捷的图片处理api,webp-imageio-core屏蔽了不同平台webp本地库的差异. <dependency> <groupId>com ...
- Thumbnailator压缩图片
Thumbnailator是一款不可多得的处理图片的第三方工具包,它写法简单到让人无法相信,Java本身也有处理图片压缩的方法,但是代码冗长到让人痛不欲生,在篇末会给出Java本身的实现方式,做下对比 ...
- 玩转 SpringBoot2.x 之整合 thumbnailator 图片处理
1.序 在实际项目中,有时为了响应速度,难免会对一些高清图片进行一些处理,比如图片压缩之类的,而其中压缩可能就是最为常见的.最近,阿淼就被要求实现这个功能,原因是客户那边嫌速度过慢.借此机会,阿淼今儿 ...
- nodejs处理图片、CSS、JS链接
接触Nodejs不深,看到页面上每一个链接都要写一个handler,像在页面显示图片,或者调用外部CSS.JS文件,每个链接都要写一个handler,觉得太麻烦,是否可以写个程序出来,能够自动识别图片 ...
- PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转
[强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...
- Filter Effects - 使用 CSS3 滤镜处理图片
CSS3 Filter(滤镜)属性提供了提供模糊和改变元素颜色的功能.CSS3 Fitler 常用于调整图像的渲染.背景或边框显示效果.这里给大家分享的这个网站,大家可以体验下 CSS3 对图片的处理 ...
- 安装glue,用glue批量处理图片的步骤
glue批量处理图片:http://glue.readthedocs.io/en/latest/quickstart.html#and-why-those-css-class-names 首先需要安 ...
随机推荐
- C#开发PACS医学影像处理系统(二):界面布局之菜单栏
在菜单栏布局上,为了使用自定义窗体样式和按钮,我们需要先将窗体设置为无边框,然后添加一个Grid作为菜单栏并置顶,VerticalAlignment="Top" logo图片和标题 ...
- 容器服务 TKE 上服务暴露的几种方式
预备知识 1. K8S 上 Service 类型 ClusterIP 通过集群的内部 IP 暴露服务,选择该值,服务只能够在集群内部可以访问,这也是默认的 ServiceType. NodePort ...
- Envoy 代理中的请求的生命周期
Envoy 代理中的请求的生命周期 翻译自Envoy官方文档. 目录 Envoy 代理中的请求的生命周期 术语 网络拓扑 配置 高层架构 请求流 总览 1.Listener TCP连接的接收 2.监听 ...
- RabbitMQ Server安装及显示管理界面Installing on Windows
接上一篇文章,继续讲解 文件很小, 1.下载路径:http://www.rabbitmq.com/download.html 2.运行rabbitmq-server-3.6.5.exe,选择要安装的目 ...
- Java操作Elasticsearch 之 [Java High Level REST Clientedit]
1. 简述 Elasticsearch 是基于 Lucene 开发的一个分布式全文检索框架,向 Elasticsearch 中存储和从 Elasticsearch 中查询,格式是json. 向 Ela ...
- volatile关键字解释和使用
一.java内存模型的相关概念:原子性.可见性与有序性 原子性: 原子是世界上的最小单位,具有不可分割性.比如 a=0:(a非long和double类型) 这个操作是不可分割的,那么我们说这个操作时原 ...
- 刷题[WUSTCTF2020]CV Maker
解题思路 好家伙,打开一看像是cms,又看名字CV Maker.我以为直接要搜cve打了.搜了一会发现没什么啊.那先正常做把. 注册 注册成功后这里报错,猜测可能有注入点.先放在这里,继续登陆.发现是 ...
- 关于SpringBoot的一点笔记
@SpringBootApplication /** * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 */ @SpringBootAppl ...
- 《Web安全攻防渗透测试实战指南》 各类型 SQL注入 实验过程整理
Union注入 https://www.jianshu.com/p/8a11bf55aaee Boolean注入 https://www.jianshu.com/p/e4086f59812d 报错注入 ...
- 推荐一款轻量小众却高效免费开源windows热键脚本语言Autohotkey
写在前面的话 Autohotkey是一款轻量小众但高效免费开源的windows热键脚本语言,游戏操纵.鼠标操作.键盘快捷重定义,快捷短语等等,只有你想不到,没有它做不到,神器中的神器呀,相见恨晚. 安 ...