Thumbnailator 图像处理

Create a thumbnail from an image file
Thumbnails.of(new File("original.jpg"))
.size(160, 160)
.toFile(new File("thumbnail.jpg"));
In this example, the image from original.jpg is resized, and then saved to thumbnail.jpg.
Alternatively, Thumbnailator will accept file names as a String. Using File objects to specify image files is not required:
Thumbnails.of("original.jpg")
.size(160, 160)
.toFile("thumbnail.jpg");
This form can be useful when writing quick prototype code, or when Thumbnailator is being used from scripting languages.
Create a thumbnail with rotation and a watermark
Thumbnails.of(new File("original.jpg"))
.size(160, 160)
.rotate(90)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("watermark.png")), 0.5f)
.outputQuality(0.8)
.toFile(new File("image-with-watermark.jpg"));
In this example, the image from original.jpg is resized, then rotated to clockwise by 90 degrees, then a watermark is placed at the bottom right-hand corner which is half transparent, then is saved toimage-with-watermark.jpg with 80% compression quality settings.
Create a thumbnail and write to an OutputStream
OutputStream os = ...;
Thumbnails.of("large-picture.jpg")
.size(200, 200)
.outputFormat("png")
.toOutputStream(os);
In this example, an image from the file large-picture.jpg is resized to a maximum dimension of 200 x 200 (maintaining the aspect ratio of the original image) and writes the that to the specifiedOutputStream as a PNG image.
Creating fixed-size thumbnails
BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.asBufferedImage();
The above code takes an image in originalImage and creates a 200 pixel by 200 pixel thumbnail using and stores the result in thumbnail.
Scaling an image by a given factor
BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.scale(0.25)
.asBufferedImage();
The above code takes the image in originalImage and creates a thumbnail that is 25% of the original image, and uses the default scaling technique in order to make the thumbnail which is stored in thumbnail.
Rotating an image when creating a thumbnail
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.rotate(90)
.asBufferedImage();
The above code takes the original image and creates a thumbnail which is rotated clockwise by 90 degrees.
Creating a thumbnail with a watermark
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f)
.asBufferedImage();
As shown, a watermark can be added to an thumbnail by calling the watermark method.
The positioning can be selected from the Positions enum.
The opaqueness (or conversely, transparency) of the thumbnail can be adjusted by changing the last argument, where 0.0f being the thumbnail is completely transparent, and 1.0f being the watermark is completely opaque.
Writing thumbnails to a specific directory
File destinationDir = new File("path/to/output");
Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
.size(200, 200)
.toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL);
This example will take the source images, and write the thumbnails them as files to destinationDir(path/to/output directory) while renaming them with thumbnail. prepended to the file names.
Therefore, the thumbnails will be written as files in:
path/to/output/thumbnail.apple.jpgpath/to/output/thumbnail.banana.jpgpath/to/output/thumbnail.cherry.jpg
It's also possible to preserve the original filename while writing to a specified directory:
File destinationDir = new File("path/to/output");
Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
.size(200, 200)
.toFiles(destinationDir, Rename.NO_CHANGE);
In the above code, the thumbnails will be written to:
path/to/output/apple.jpgpath/to/output/banana.jpgpath/to/output/cherry.jpg
Thumbnailator 图像处理的更多相关文章
- google使用的开源的工具类Thumbnailator图像处理
maven依赖 <dependency> <groupId>net.coobird</groupId> <artifactId>thum ...
- Java图片缩略图裁剪水印缩放旋转压缩转格式-Thumbnailator图像处理
前言 java开发中经常遇到对图片的处理,JDK中也提供了对应的工具类,不过处理起来很麻烦,Thumbnailator是一个优秀的图片处理的开源Java类库,处理效果远比Java API的好,从API ...
- google thumbnailator
Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好. 从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片 ...
- Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库
Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库1.1. 5种常用的Photoshop滤镜,分别针对照片的曝光.风格色调.黑白照片处理.锐利度.降噪这五大 ...
- atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本
atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本 1. 去除边框思路原理 1 2. Thumbnailator 是一个用来生成图像缩略图.裁切. ...
- Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉
Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉 1.1. 按照当前流行的分类方法,可以分为以下三部分:三部分 图像处理 图像分析 计算机视觉1 1.2. 图像处理需要 ...
- Atitit 图像处理的摩西五经attilax总结
Atitit 图像处理的摩西五经attilax总结 1. 数字图像处理(第三版)1 2. 图像处理基础(第2版)(世界著名计算机教材精选)1 3. 计算机视觉特征提取与图像处理(第三版)2 4. Op ...
- Atitit 图像处理的心得与疑惑 attilax总结
Atitit 图像处理的心得与疑惑 attilax总结 1.1. 使用类库好不好??还是自己实现算法1 1.2. 但是,如果遇到类库体积太大,后者没有合适的算法,那就只能自己开发算法了1 1.3. 如 ...
- Atitit 图像处理 调用opencv 通过java api attilax总结
Atitit 图像处理 调用opencv 通过java api attilax总结 1.1. Opencv java api的支持 opencv2.4.2 就有了对java api的支持1 1. ...
随机推荐
- LabVIEW之生产者/消费者模式
LabVIEW之生产者/消费者设计模式 彭会锋
- HDU 4815 概率dp,背包
Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K ( ...
- Qt5全局热键-QxtGlobalShortcut
最近做一个项目需要注册全局热键,在网上搜索发现有个第三方库 libqxt 中给出一个比较好的跨平台的解决方案,就是 QxtGlobalShortcut. 但是编译过程中发现这个库用到的QAbstrac ...
- Python -- 使用pickle 和 CPickle对数据对象进行归档和解析
经常遇到在Python程序运行中得到了一些字符串.列表.字典.对象等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就丢失数据. 这个时候Pickle模块就派上用场了,它可以将对 ...
- Git 从了解到放弃
1. 简单介绍 1.1. git起源 在1991年linus创建了Linux从此linux成为服务器领域的佼佼者,大部分web服务器.邮件.数据库各种服务器端程序都安装在了linux上面运行,主要是因 ...
- PIVOT 和 UNPIVOT 命令的SQL Server版本
I:使用 PIVOT 和 UNPIVOT 命令的SQL Server版本要求 1.数据库的最低版本要求为 SQL Server 2005 或 更高 2.必须将数据库的兼容级别设置为 90 或 更高 3 ...
- Yii2学习笔记:汉化yii,设置表单的描述(属性标签attributeLabels)
一:汉化框架 框架汉化在这里设置,如果不生效,前台后台的配置文件都设置下应该就可以了 二:汉化表单 汉化表单,直接在模型层设置,例如: 原来的联系我们表单 汉化后: ] 这种汉化在哪里修改呢?其实是设 ...
- Kotlin Reference (三) Coding Conventions
most from reference 命名规则 1.使用驼峰式命名规则,尽量避免在命名中使用下划线 2.类型以大写字母开头 3.方法和属性以小写字母开头 4.使用4个空格缩进 5.public的方法 ...
- 学习三部曲:WHAT、HOW、WHY
一个人学习的过程要经历以下三步,才可以说得上"学会"两字: 第一步:WHAT 所谓的"WHAT",就是搞清楚某个东东是什么?有什么用?有什么语法?有什么功能特性 ...
- java入门学习(2)—基本数据类型
1.变量:定义变量:[数据类型] 变量名 = 赋值(这样定义的变量一般属于局部变量,放置在栈内存中): 2.标识符:可以有字母(可以使任意文字),数字,下划线,$等组成:但是不能以数字开头,不能是保留 ...