【JAVA之缩略图Thumbnailator】
Thumbnailator是一个用来生成图像缩略图的 Java类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。
有了这玩意,就不用在费心思使用Image I/O API,Java 2D API等等来生成缩略图了。
Thumbnailator是一个用java生成高质量缩略图的第三方库,可以用来
1.生成缩率图;
2.添加水印;
3.图片旋转;
4.图片大小缩放;
5.图片压缩;
1.对图片尺寸进行重设
/**
* 重设图片尺寸
* @throws IOException
*/
Thumbnails.of(new File("原图.jpg"))
.size(160, 160)
.toFile(new File("新图.jpg"));
对文件夹下所有图片生成缩略图
Thumbnails.of(new File("path/to/directory")
.listFiles())
.size(640, 480)
.outputFormat("jpg")
.toFiles(Rename.PREFIX_DOT_THUMBNAIL);
Thumbnails.of("原图文件的路径")
.scale(1f)
.outputQuality(0.5f)
.toFile("压缩后文件的路径");
其中的scale是可以指定图片的大小,值在0到1之间,1f就是原图大小,0.5就是原图的一半大小,这里的大小是指图片的长宽。
而outputQuality是图片的质量,值也是在0到1,越接近于1质量越好,越接近于0质量越差。
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 to image-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
推荐阅读
-
java设计模式之模板方法模式详解
-
Java并发实例之CyclicBarrier的使用
-
Java反射之Call stack introspection详解
-
Java concurrency线程池之线程池原理(四)_动力节点Java学院整理
-
Java concurrency线程池之线程池原理(三)_动力节点Java学院整理
-
Java concurrency之AtomicLongArray原子类_动力节点Java学院整理
-
微信公众号开发之设置自定义菜单实例代码【java版】
-
Java concurrency之AtomicLong原子类_动力节点Java学院整理
-
Java concurrency线程池之Callable和Future_动力节点Java学院整理
-
Java面向对象之成员隐藏与属性封装操作示例