java中使用Thumbnails对图片进行处理
Posted hechurui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中使用Thumbnails对图片进行处理相关的知识,希望对你有一定的参考价值。
Thumbnails支持:
-
指定大小进行缩放
-
按照比例进行缩放
-
不按照比例,指定大小进行缩放
-
旋转
-
水印
-
裁剪
-
转化图像格式
-
输出到OutputStream
-
输出到BufferedImage
-
输出到ByteArrayOutputStream(OutputStream)
-
输出到ByteArrayInputStream(InputStream)
-
输出到byte[]
- Thumbnails导入依赖
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
一,指定大小进行缩放
//size(宽度, 高度) /* * 若图片横比200小,高比300小,不变 * 若图片横比200小,高比300大,高缩小到300,图片比例不变 * 若图片横比200大,高比300小,横缩小到200,图片比例不变 * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300 */ Thumbnails.of("images/a380_1280x1024.jpg") .size(200, 300) .toFile("c:/a380_200x300.jpg"); Thumbnails.of("images/a380_1280x1024.jpg") .size(2560, 2048) .toFile("c:/a380_2560x2048.jpg");
size(int width, int height)
按比例,使原图撑满size大小; -
二,单个图片等比例缩放
File file = new File("c:\\\\test.png"); Thumbnails.of(new FileInputStream(file)).scale(3.0).toFile(new File("c:\\\\yyyyy.png"));
3.0是一个double类型的数字,缩放比例,大于1就是变大,小于1就是缩小
-
.
width(int width)
设置宽,高按比例;.
height(int height)
设置高,宽按比例;.
forceSize(int width, int height)
设置宽高,不按比例,会按照设置的宽高拉伸;.
scale(double scale)
按比例缩放,0~1缩小,1原比例,>1放大;.
scale(double scaleWidth, double scaleHeight)
长宽各自设置比例,会拉伸;三,不按照比例,指定大小进行缩放
//keepAspectRatio(false) 默认是按照比例缩放的 Thumbnails.of("images/a380_1280x1024.jpg") .size(200, 200) .keepAspectRatio(false) .toFile("c:/a380_200x200.jpg");
.
scalingMode(ScalingMode config)
缩放模式(ScalingMode枚举BICUBIC、BILINEAR、PROGRESSIVE_BILINEAR);.
keepAspectRatio(boolean keep)
设置是否按比例,false不按比例;注:size、width/height、scale、forceSize不能并用;size相当于width+height;forceSize相当于设置长款+keepAspectRatio=false,所以forceSize不能跟其他设置长款属性、keepAspectRatio并用;
-
四,批量产生缩略图
Thumbnails.of(new File("D:\\\\pics").listFiles()).scale(0.2).outputFormat("png") .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
五,控制图片质量,图片尺寸不变
File fromPic = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\IdCardPositive_987136936_1531741954688.jpeg"); File toPic =new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\IdCardPositive_987136936_08.jpeg"); Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
outputQuality就是用来控制图片质量的
-
.
outputQuality(double quality)
质量0.0<=quality<=1.0;.
outputQuality(float quality)
六,给图片加水印
Thumbnails.of(fromPic).scale(0.8) .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(waterPic), 0.5f) .outputQuality(0.8f).toFile(toPic); //watermark(位置,水印图,透明度) Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f) .outputQuality(0.8f) .toFile("c:/a380_watermark_bottom_right.jpg"); Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f) .outputQuality(0.8f) .toFile("c:/a380_watermark_center.jpg");
fromPic是原图,waterPic是水印图片,toPic是生成后的图片
-
.
watermark(BufferedImage image)
中心位置50%透明度设置水印;.
watermark(BufferedImage image, float opacity)
中心位置,opacity的不透明度设置水印(0.0<=opacity<=1.0);.
watermark(Position position, BufferedImage image, float opacity)
在position位置,opacity不透明度设置水印;七,旋转图片
Thumbnails.of(fromPic).scale(0.5).rotate(90).toFile(toPic);
.
rotate(double angle)
旋转角度,顺时针为正; -
八,图片裁剪
Thumbnails.of(fromPic).sourceRegion(Positions.CENTER, 300, 300).scale(1.0).toFile(toPic); //sourceRegion() //图片中心400*400的区域 Thumbnails.of("images/a380_1280x1024.jpg") .sourceRegion(Positions.CENTER, 400,400) .size(200, 200) .keepAspectRatio(false) .toFile("c:/a380_region_center.jpg"); //图片右下400*400的区域 Thumbnails.of("images/a380_1280x1024.jpg") .sourceRegion(Positions.BOTTOM_RIGHT, 400,400) .size(200, 200) .keepAspectRatio(false) .toFile("c:/a380_region_bootom_right.jpg"); //指定坐标 Thumbnails.of("images/a380_1280x1024.jpg") .sourceRegion(600, 500, 400, 400) .size(200, 200) .keepAspectRatio(false) .toFile("c:/a380_region_coord.jpg");
.
sourceRegion(int x, int y, int width, int height)
剪裁原图,坐标x,y起始,剪裁出宽度width高度height的图像,x向右为正,y向下为正,width(向右)和height(向下)必须大于0;.
sourceRegion(Position position, int width, int height)
剪裁原图,区域位置position可用Positions枚举的9个位置或者实现Position接口的实现类;.
sourceRegion(Position position, Size size)
.
sourceRegion(Rectangle region)
.
sourceRegion(Region sourceRegion)
-
九,WEB输出流图片
某些应用上传的图片可能质量比较高,但是用户在列表浏览的时候,又不想原图展示,因为带宽要求较高,此时可以降低图片质量(上面提到的outputQuality),以outputstream输出流的方式response给浏览器去展示
@RequestMapping("/getImages") public void getImages(HttpServletRequest request, HttpServletResponse response) throws IOException Thumbnails.of("images/a380_1280x1024.jpg"). scale(1f). outputQuality(0.5f). outputFormat("jpg").toOutputStream(response.getOutputStream());
十,图像的格式转换
//outputFormat(图像格式) Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .outputFormat("png") .toFile("c:/a380_1280x1024.png"); Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .outputFormat("gif") .toFile("c:/a380_1280x1024.gif");
十一,输出到BufferedImage
//asBufferedImage() 返回BufferedImage BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .asBufferedImage(); ImageIO.write(thumbnail, "jpg", new File("c:/a380_1280x1024_BufferedImage.jpg"));
十二,输出到ByteArrayOutputStream(OutputStream)
ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream(); Thumbnails.of("images/a380_1280x1024.jpg"). scale(1f). outputQuality(0.5f). outputFormat("jpg").toOutputStream(thumbnailOutputStream);
十三,输出到ByteArrayInputStream(InputStream)
ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream(); Thumbnails.of("images/a380_1280x1024.jpg"). scale(1f). outputQuality(0.5f). outputFormat("jpg").toOutputStream(thumbnailOutputStream); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
十三,输出到byte[]
ByteArrayOutputStream handlerOutputStream = new ByteArrayOutputStream(); Thumbnails.of(inputStream). scale(1f). outputQuality(0.25f). outputFormat("jpg").toOutputStream(handlerOutputStream); byte[] bytes = handlerOutputStream.toByteArray();
以上是关于java中使用Thumbnails对图片进行处理的主要内容,如果未能解决你的问题,请参考以下文章