读取源图

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处理图片的更多相关文章

  1. java使用Thumbnailator处理图片

    Thumbnailator是一款不可多得的处理图片的第三方工具包,它写法简单到让人无法相信,Java本身也有处理图片压缩的方法,但是代码冗长到让人痛不欲生,在篇末会给出Java本身的实现方式,做下对比 ...

  2. Java语言利用Google的Thumbnailator处理图片

    导读:在Java中制作高质量的缩略图可能是一项相当困难的任务,或者图片质量高页面加载慢等等原因使需要对图片进行处理,但是又不想写太多的代码太麻烦去处理图片,那么就可以使用Thumbnailator—— ...

  3. thumbnailator + webp-imageio-core实现java处理图片支持webp

    thumbnailator提供了便捷的图片处理api,webp-imageio-core屏蔽了不同平台webp本地库的差异. <dependency> <groupId>com ...

  4. Thumbnailator压缩图片

    Thumbnailator是一款不可多得的处理图片的第三方工具包,它写法简单到让人无法相信,Java本身也有处理图片压缩的方法,但是代码冗长到让人痛不欲生,在篇末会给出Java本身的实现方式,做下对比 ...

  5. 玩转 SpringBoot2.x 之整合 thumbnailator 图片处理

    1.序 在实际项目中,有时为了响应速度,难免会对一些高清图片进行一些处理,比如图片压缩之类的,而其中压缩可能就是最为常见的.最近,阿淼就被要求实现这个功能,原因是客户那边嫌速度过慢.借此机会,阿淼今儿 ...

  6. nodejs处理图片、CSS、JS链接

    接触Nodejs不深,看到页面上每一个链接都要写一个handler,像在页面显示图片,或者调用外部CSS.JS文件,每个链接都要写一个handler,觉得太麻烦,是否可以写个程序出来,能够自动识别图片 ...

  7. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  8. Filter Effects - 使用 CSS3 滤镜处理图片

    CSS3 Filter(滤镜)属性提供了提供模糊和改变元素颜色的功能.CSS3 Fitler 常用于调整图像的渲染.背景或边框显示效果.这里给大家分享的这个网站,大家可以体验下 CSS3 对图片的处理 ...

  9. 安装glue,用glue批量处理图片的步骤

     glue批量处理图片:http://glue.readthedocs.io/en/latest/quickstart.html#and-why-those-css-class-names 首先需要安 ...

随机推荐

  1. Ubuntu修改默认Python版本,你了解多少~

    目录 1 查看Python版本 1.1 查看装有哪些版本 1.2 查看默认版本 2 修改Python默认版本 2.1 基于文件修改 2.2 基于软链接修改 1 查看Python版本 1.1 查看装有哪 ...

  2. 【python】itchat登录微信获取好友签名并生成词云

    在知乎上看到一篇关于如何使用itchat统计微信好友男女比例并使用plt生成柱状图以及获取微信好友签名并生成词云的文章https://zhuanlan.zhihu.com/p/36361397,感觉挺 ...

  3. python之类方法和静态方法

    在类中定义的函数称为方法,主要有三种:实例方法.类方法.静态方法. class MyTest(): # 普通实例函数 def func1(self, arg1, arg2): pass # 类函数 @ ...

  4. 2. 构建DNS集群

    DNS是什么 DNS(Domain Name System,域名系统),是互联网上存储域名和IP映射关系的一个分布式数据库,他负责把域名转换为IP地址,或IP转换为域名,工作于OSI应用层之上,DNS ...

  5. [LeetCode]394. 字符串解码(栈)

    题目 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数. ...

  6. python循环输出

    python 目录 python 1.九九乘法表 2.循环输出数字0-9,数字为六,跳出循环,执行其他循环,数字为八,结束循环 3.使用循环计算0-100素数的和 4.使用for循环输出三角形 1.九 ...

  7. SpringMVC-12-SSM回顾与总结

    12.SSM回顾与总结

  8. openstack核心组件——keystone身份认证部署服务(5)

    node1主机执行 1.mysql -u root -p 2.create database keystone; 创建数据库 MariaDB [(none)]> show databases; ...

  9. matlab中的多项式计算

    在做多项式加法的时候需要做多项式扩展.这里将g1扩展到与f等长 多项式的乘积,是两个多项式之和减1, 多项式求导函数:ployder() 先建立两个多项式,再求a的导函数 在计算两个多项式乘积的导函数 ...

  10. 判断9X9数组是否是数独的java代码

    闲来无事,理了一下数独的判断逻辑,用java实现,代码如下 import java.util.logging.FileHandler;import java.util.logging.Level;im ...