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:

null

时间: 2024-12-15 16:06:10

Thumbnailator 图像处理的相关文章

Java图片缩略图裁剪水印缩放旋转压缩转格式-Thumbnailator图像处理

前言 java开发中经常遇到对图片的处理,JDK中也提供了对应的工具类,不过处理起来很麻烦,Thumbnailator是一个优秀的图片处理的开源Java类库,处理效果远比Java API的好,从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量.还支持对一个目录的所有图片进行批量处理操作,下边就和大家分享一下java中用Thumbnailator做图片各种处理的方法(相关jar包可在最

google使用的开源的工具类Thumbnailator图像处理

maven依赖 <dependency>     <groupId>net.coobird</groupId>     <artifactId>thumbnailator</artifactId>     <version>0.4.8</version> </dependency> 使用接口例子 https://github.com/coobird/thumbnailator/wiki/Examples 1.指

google thumbnailator

Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好. 从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量.还支持对一个目录的所有图片进行批量处理操作. 支持的处理操作: 1 图片缩放(按原比例:按指定大小:按指定比例:) 2 区域裁剪 3 水印 4 旋转 5 转化图像格式 5 多种输出方式(File, OutputStre

Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库

1.1. 5种常用的Photoshop滤镜,分别针对照片的曝光.风格色调.黑白照片处理.锐利度.降噪这五大方向.坦11.2. Java Image Filters 是由 Jhlabs 开发的一组用来处理 Java 图像的类库,提供各种常用的图像处理效果,例如反转色.扭曲.水波纹.凹凸.黑白效果等等数十种效果,11.3. Photoshop CS6常用8大滤镜中文集合版31.4. 史上超强图像处理开源工具包--ImageMagick?41.5. Thumbnailator是一个用来生成图像缩略图的

图像处理库大全

关键字: 1. Thumbnailator  主页:https://code.google.com/p/thumbnailator/ 2. 所说是阿里巴巴的simpleimage   主页:https://github.com/alibaba/simpleimage 3.腾讯的网页图像处理库alloyteam   主页:http://alloyteam.github.io/AlloyPhoto/ 4. Imagero 5. Sanselan 6. ImageMagick 待续......

atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本

atitit.验证码识别step3----去除边框---- 图像处理类库 attilax总结java版本 1. 去除边框思路原理 1 2. Thumbnailator 是一个用来生成图像缩略图.裁切.旋转.添加水印等操作 2 3. OpenCL的Java库 JavaCL 2 4. Java Image Filters是一款基于Java的图像处理类库,特别是在图像滤镜特效方面, 2 4.1.1. 色彩调整 2 4.1.2. 变形和扭曲 5 5. JJIL 是一个Java 的图像处理类库,有超过60

图像处理之三种常见双立方插值算法

http://blog.csdn.net/jia20003/article/details/40020775 图像处理之三种常见双立方插值算法 双立方插值计算涉及到16个像素点,其中(i', j')表示待计算像素点在源图像中的包含 小数部分的像素坐标,dx表示X方向的小数坐标,dy表示Y方向的小数坐标.具体 可以看下图: 根据上述图示与双立方插值的数学表达式可以看出,双立方插值本质上图像16个像素点 权重卷积之和作为新的像素值. 其中R(x)表示插值表达式,可以根据需要选择的表达式不同.常见有基

【20160924】GOCVHelper 图像处理部分(1)

增强后的图像需要通过图像处理获得定量的值.在实际程序设计过程中,轮廓很多时候都是重要的分析变量.参考Halcon的相关函数,我增强了Opencv在这块的相关功能.      //寻找最大的轮廓 VP FindBigestContour(Mat src){ int imax = 0; //代表最大轮廓的序号 int imaxcontour = -1; //代表最大轮廓的大小 std::vector<std::vector<cv::Point>>contours; findContou

详解OS X和iOS图像处理框架Core Image

转自:http://www.csdn.net/article/2015-02-13/2823961-core-image 摘要:本 文结合实例详解了OS X和iOS图像处理框架Core Image的使用,如何通过Core Image来创建和使用iOS的内置滤镜,非常适合初学者学习.虽然示例代码是用Swift写的iOS程序,不过实现概念很容易转换到 Objective-C和OS X. 这篇文章会为初学者介绍一下Core Image,一个OS X和iOS的图像处理框架. 如果你想跟着本文中的代码学习