前言
java开发中经常遇到对图片的处理,JDK中也提供了对应的工具类,不过处理起来很麻烦,Thumbnailator是一个优秀的图片处理的开源Java类库,处理效果远比Java API的好,从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量。还支持对一个目录的所有图片进行批量处理操作,下边就和大家分享一下java中用Thumbnailator做图片各种处理的方法(相关jar包可在最下方下载)。
测试图片
1.测试图片1024px-768px.jpg:
2.作为水印的图片:
根据路径创建图片文件
- package ThumbnaillatorTest;
-
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
-
- import javax.imageio.ImageIO;
-
- import net.coobird.thumbnailator.Thumbnails;
- import net.coobird.thumbnailator.Thumbnails.Builder;
- import net.coobird.thumbnailator.geometry.Positions;
-
- import org.junit.Test;
-
-
- public class ThumbTest {
-
- @Test
- public void testHandlePicture() throws IOException{
-
-
- File fromPic=new File("picture/测试图片1024px-768px.jpg");
- File toPic=new File("picture/结果图片.jpg");
- File waterPic=new File("picture/水印图片.jpg");
package ThumbnaillatorTest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;
import org.junit.Test;
public class ThumbTest {
@Test
public void testHandlePicture() throws IOException{
//创建图片文件(此处为1024×768px的图片)和处理后的图片文件
File fromPic=new File("picture/测试图片1024px-768px.jpg");
File toPic=new File("picture/结果图片.jpg");
File waterPic=new File("picture/水印图片.jpg");//作为水印的图片</pre>
注意:以上代码程序未结束,由下边粘贴代码不断追加。
按指定大小把图片进行缩放(会遵循原图高宽比例)
-
-
- Thumbnails.of(fromPic).size(400,500).toFile(toPic);
//按指定大小把图片进行缩和放(会遵循原图高宽比例)
//此处把图片压成400×500的缩略图
Thumbnails.of(fromPic).size(400,500).toFile(toPic);//变为400*300,遵循原图比例缩或放到400*某个高度
效果如下:
按照指定比例进行缩小和放大
-
- Thumbnails.of(fromPic).scale(0.2f).toFile(toPic);
- Thumbnails.of(fromPic).scale(2f);
//按照比例进行缩小和放大
Thumbnails.of(fromPic).scale(0.2f).toFile(toPic);//按比例缩小
Thumbnails.of(fromPic).scale(2f);//按比例放大
缩小效果如下:
放大由于过大,不再展示效果。
按指定的大小进行缩放(不遵循原图比例)
-
- Thumbnails.of(fromPic).size(100, 100).keepAspectRatio(false).toFile(toPic);
-
//不按比例,就按指定的大小进行缩放
Thumbnails.of(fromPic).size(100, 100).keepAspectRatio(false).toFile(toPic);
//或者Thumbnails.of(fromPic).forceSize(100,100).toFile(toPic);
效果如下:
旋转图片
-
- Thumbnails.of(fromPic).size(200,200).rotate(90).toFile(toPic);
//旋转图片,rotate(角度),正数则为顺时针,负数则为逆时针
Thumbnails.of(fromPic).size(200,200).rotate(90).toFile(toPic);
效果如下:
图片尺寸不变,压缩图片文件大小
-
- Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
//图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量
Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
效果是图片的大小由原来的2M多,变为十几K的,图片还是较清晰。
给图片加水印
-
- Thumbnails.of(fromPic).size(400,400)
- .watermark(Positions.CENTER,ImageIO.read(waterPic),0.5f)
- .outputQuality(0.8f).toFile(toPic);
//给图片加水印,watermark(位置,水印图,透明度)Positions.CENTER表示加在中间
Thumbnails.of(fromPic).size(400,400)
.watermark(Positions.CENTER,ImageIO.read(waterPic),0.5f)
.outputQuality(0.8f).toFile(toPic);
效果如下:
图片裁剪
1.
-
-
- Thumbnails.of(fromPic).sourceRegion(Positions.CENTER,300,300)
- .size(300,300).toFile(toPic);
//用sourceRegion()实现图片裁剪
//图片中心300*300的区域,Positions.CENTER表示中心,还有许多其他位置可选
Thumbnails.of(fromPic).sourceRegion(Positions.CENTER,300,300)
.size(300,300).toFile(toPic);
效果如下:
2.
-
- Thumbnails.of(fromPic).sourceRegion(Positions.TOP_CENTER,300,300)
- .size(300,300).toFile(toPic);
//图片中上区域300*300的区域
Thumbnails.of(fromPic).sourceRegion(Positions.TOP_CENTER,300,300)
.size(300,300).toFile(toPic);
效果如下:
3.
- Thumbnails.of(fromPic).sourceRegion(0,0,200,200)
- .size(300,300).toFile(toPic);
Thumbnails.of(fromPic).sourceRegion(0,0,200,200)
.size(300,300).toFile(toPic);
效果如下:
转换图片格式
-
- Thumbnails.of(fromPic).scale(1f).outputFormat("png")
- .toFile("picture/png格式的图片.png");
//用outputFormat(图像格式)转换图片格式,保持原尺寸不变
Thumbnails.of(fromPic).scale(1f).outputFormat("png")
.toFile("picture/png格式的图片.png");
效果就是格式由jpg变为png,图片大小没变。
输出成文件流OutputStream
-
- OutputStream os=new FileOutputStream(toPic);
- Thumbnails.of(fromPic).size(120,120).toOutputStream(os);
//输出成文件流OutputStream
OutputStream os=new FileOutputStream(toPic);
Thumbnails.of(fromPic).size(120,120).toOutputStream(os);
输出成BufferedImage
-
- BufferedImage bi=Thumbnails.of(fromPic).size(120,120).asBufferedImage();
- ImageIO.write(bi,"jpg",toPic);
//输出BufferedImage,asBufferedImage()返回BufferedImage
BufferedImage bi=Thumbnails.of(fromPic).size(120,120).asBufferedImage();
ImageIO.write(bi,"jpg",toPic);
压缩至指定图片尺寸,保持图片不变形,多余部分裁剪掉
-
- BufferedImage image = ImageIO.read(fromPic);
- Builder<BufferedImage> builder = null;
-
- int imageWidth = image.getWidth();
- int imageHeitht = image.getHeight();
- if ((float)300 / 400 != (float)imageWidth / imageHeitht) {
- if (imageWidth > imageHeitht) {
- image = Thumbnails.of(fromPic).height(300).asBufferedImage();
- } else {
- image = Thumbnails.of(fromPic).width(400).asBufferedImage();
- }
- builder = Thumbnails.of(image).sourceRegion(Positions.CENTER, 400, 300).size(400, 300);
- } else {
- builder = Thumbnails.of(image).size(400, 300);
- }
- builder.outputFormat("jpg").toFile(toPic);
//压缩至指定图片尺寸(例如:横400高300),保持图片不变形,多余部分裁剪掉(这个是引的网友的代码)
BufferedImage image = ImageIO.read(fromPic);
Builder<BufferedImage> builder = null;
int imageWidth = image.getWidth();
int imageHeitht = image.getHeight();
if ((float)300 / 400 != (float)imageWidth / imageHeitht) {
if (imageWidth > imageHeitht) {
image = Thumbnails.of(fromPic).height(300).asBufferedImage();
} else {
image = Thumbnails.of(fromPic).width(400).asBufferedImage();
}
builder = Thumbnails.of(image).sourceRegion(Positions.CENTER, 400, 300).size(400, 300);
} else {
builder = Thumbnails.of(image).size(400, 300);
}
builder.outputFormat("jpg").toFile(toPic);</pre>
测试over!
}
}
相关jar下载
Thumbnailator免费下载
转载请注明—作者:Java我人生(陈磊兴) 原文出处:http://blog.csdn.net/chenleixing/article/details/44685817
最后,认真看过的网友们,大神们,如有感觉我这个程序猿有哪个地方说的不对或者不妥或者你有很好的提议或者建议或点子方法,还望