这篇文章给大家介绍怎么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");
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就是缩小
//keepAspectRatio(false) 默认是按照比例缩放的 Thumbnails.of("images/a380_1280x1024.jpg") .size(200, 200) .keepAspectRatio(false) .toFile("c:/a380_200x200.jpg");
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就是用来控制图片质量的
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是生成后的图片
Thumbnails.of(fromPic).scale(0.5).rotate(90).toFile(toPic);
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");
某些应用上传的图片可能质量比较高,但是用户在列表浏览的时候,又不想原图展示,因为带宽要求较高,此时可以降低图片质量(上面提到的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");
//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 thumbnailOutputStream = new ByteArrayOutputStream(); Thumbnails.of("images/a380_1280x1024.jpg"). scale(1f). outputQuality(0.5f). outputFormat("jpg").toOutputStream(thumbnailOutputStream);
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());
ByteArrayOutputStream handlerOutputStream = new ByteArrayOutputStream(); Thumbnails.of(inputStream). scale(1f). outputQuality(0.25f). outputFormat("jpg").toOutputStream(handlerOutputStream); byte[] bytes = handlerOutputStream.toByteArray();
关于怎么java中使用Thumbnails对图片进行处理就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。