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.jpg
path/to/output/thumbnail.banana.jpg
path/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.jpg
path/to/output/banana.jpg
path/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. ...
随机推荐
- spring mvc:输出json,输出多个json
spring mvc:输出xml/输出json 用到的注解@ResponseBody @ResponseBody用来输出json/xml等格式数据(非html) controller输出用到的类 or ...
- flask学习(十二):for循环遍历
一. 字典的遍历 语法和python一样,可以使用items().keys().values().iteritems().iterkeys().itervalues() {% for k, v in ...
- hdu1540线段树连续区间
模板题>.<当初学了一波又忘了 #include<map> #include<set> #include<cmath> #include<queu ...
- 大数据位图法(无重复排序,重复排序,去重复排序,数据压缩)之Java实现
1,位图法介绍 位图的基本概念是用一个位(bit)来标记某个数据的存放状态,由于采用了位为单位来存放数据,所以节省了大量的空间.举个具体的例子,在Java中一般一个int数字要占用32位,如果能用一位 ...
- 玲珑oj 1117 线段树+离线+离散化,laz大法
1117 - RE:从零开始的异世界生活 Time Limit:1s Memory Limit:256MByte Submissions:438Solved:68 DESCRIPTION 486到了异 ...
- 进程上下文频繁切换导致load average过高
一.问题现象 现网有两台虚拟机主机95%的cpu处于idle状态,内存使用率也不是特别高,而主机的load average达到了40多. 二.问题分析 先在主机上通过top.free.ps.iosta ...
- day15 web框架和Django基础
参考博客: http://www.cnblogs.com/yuanchenqi/articles/6788872.html http://www.cnblogs.com/yuanchenqi/arti ...
- caffe:自己搭建网络来训练
1.准备样本 要训练自己的样本,首先需要把样本准备好,需要准备的是训练集和测试集,caffe支持直接使用图片,当然把样本转换为leveldb或lmdb格式的话训练起来会更快一点.这里我先偷个懒,直接使 ...
- 小练习:vaild number
1.描述 给定字符串,若该字符串表示的是数字,则输出true,否则输出false 2.分析 题目一看感觉不难,做起来却很麻烦,首先是数字的各种表示要知道,然后就是对这些不同形式的数字进行筛选判断.该题 ...
- java中join用法
今天又把join的用法大概看了一下,其实理解起来,还是比较简单.用个简单的例子说明一下吧. 1.通过下面的例子,可以看到说出结果中首先全部是是Thread-1,之后才会是Thread-2,这是因为在主 ...