您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。