久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Java 使用Thumbnails對(duì)大圖片壓縮

瀏覽:55日期:2022-08-21 13:43:32

引言

在最近的項(xiàng)目開發(fā)中,經(jīng)常會(huì)使用到圖片上傳,但是過大的圖片在查看的時(shí)候會(huì)影響打開速度,浪費(fèi)流量以及服務(wù)器存儲(chǔ)空間,所以需要在后端處理完圖片再上傳,這里我們用到了Thumbnails圖片處理工具類。

Thumbnails主要支持以下一些功能

1、指定大小進(jìn)行縮放

2、按照比例進(jìn)行縮放

3、不按照比例,指定大小進(jìn)行縮放

4、旋轉(zhuǎn)

5、水印

6、裁剪

7、轉(zhuǎn)化圖片格式

8、輸出到OutputStream

9、輸出到BufferedImage

使用步驟:

一、添加Maven

<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version></dependency>

二、具體操作

1:指定大小進(jìn)行縮放

/** * 指定大小進(jìn)行縮放 * * @throws IOException */ private void test1() throws IOException { /* * size(width,height) 若圖片橫比200小,高比300小,不變 * 若圖片橫比200小,高比300大,高縮小到300,圖片比例不變 若圖片橫比200大,高比300小,橫縮小到200,圖片比例不變 * 若圖片橫比200大,高比300大,圖片按比例縮小,橫為200或高為300 */ Thumbnails.of('images/test.jpg').size(200, 300).toFile('C:/image_200x300.jpg'); Thumbnails.of('images/test.jpg').size(2560, 2048).toFile('C:/image_2560x2048.jpg'); }

2:按照比例進(jìn)行縮放

/** * 按照比例進(jìn)行縮放 * scale 圖片的壓縮比例 值在0-1之間,1f就是原圖,0.5就是原圖的一半大小 * outputQuality 圖片壓縮的質(zhì)量 值在0-1 之間,越接近1質(zhì)量越好,越接近0 質(zhì)量越差 * @throws IOException */ private void test2() throws IOException { /** * scale(比例) */ Thumbnails.of('images/test.jpg').scale(0.25f).outputQuality(0.8f).toFile('C:/image_25%.jpg'); Thumbnails.of('images/test.jpg').scale(0.75f).outputQuality(0.8f).toFile('C:/image_110%.jpg'); }

3:不按照比例,指定大小進(jìn)行縮放

/** * 不按照比例,指定大小進(jìn)行縮放 * * @throws IOException */ private void test3() throws IOException { /** * keepAspectRatio(false) 默認(rèn)是按照比例縮放的 */ Thumbnails.of('images/test.jpg').size(120, 120).keepAspectRatio(false).toFile('C:/image_120x120.jpg'); }

4:旋轉(zhuǎn)

/** * 旋轉(zhuǎn) * * @throws IOException */ private void test4() throws IOException { /** * rotate(角度),正數(shù):順時(shí)針 負(fù)數(shù):逆時(shí)針 */ Thumbnails.of('images/test.jpg').size(1280, 1024).rotate(90).toFile('C:/image+90.jpg'); Thumbnails.of('images/test.jpg').size(1280, 1024).rotate(-90).toFile('C:/iamge-90.jpg'); }

5:水印

/** * 水印 * * @throws IOException */ private void test5() throws IOException { /** * watermark(位置,水印圖,透明度) */ Thumbnails.of('images/test.jpg').size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File('images/watermark.png')), 0.5f).outputQuality(0.8f).toFile('C:/image_watermark_bottom_right.jpg'); Thumbnails.of('images/test.jpg').size(1280, 1024).watermark(Positions.CENTER, ImageIO.read(new File('images/watermark.png')), 0.5f).outputQuality(0.8f).toFile('C:/image_watermark_center.jpg'); }

6:裁剪

/** * 裁剪 * * @throws IOException */ private void test6() throws IOException { /** * 圖片中心400*400的區(qū)域 */ Thumbnails.of('images/test.jpg').sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false).toFile('C:/image_region_center.jpg'); /** * 圖片右下400*400的區(qū)域 */ Thumbnails.of('images/test.jpg').sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false).toFile('C:/image_region_bootom_right.jpg'); /** * 指定坐標(biāo) */ Thumbnails.of('images/test.jpg').sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile('C:/image_region_coord.jpg'); }

7:轉(zhuǎn)化圖片格式

/** * 轉(zhuǎn)化圖片格式 * * @throws IOException */ private void test7() throws IOException { /** * outputFormat(圖像格式) */ Thumbnails.of('images/test.jpg').size(1280, 1024).outputFormat('png').toFile('C:/image_1280x1024.png'); Thumbnails.of('images/test.jpg').size(1280, 1024).outputFormat('gif').toFile('C:/image_1280x1024.gif'); }

8:輸出到OutputStream

/** * 輸出到OutputStream * * @throws IOException */ private void test8() throws IOException { /** * toOutputStream(流對(duì)象) */ OutputStream os = new FileOutputStream('C:/image_1280x1024_OutputStream.png'); Thumbnails.of('images/test.jpg').size(1280, 1024).toOutputStream(os); }

9:輸出到BufferedImage

/** * 輸出到BufferedImage * * @throws IOException */ private void test9() throws IOException { /** * asBufferedImage() 返回BufferedImage */ BufferedImage thumbnail = Thumbnails.of('images/test.jpg').size(1280, 1024).asBufferedImage(); ImageIO.write(thumbnail, 'jpg', new File('C:/image_1280x1024_BufferedImage.jpg')); }

三、對(duì)圖片文件進(jìn)行Base64操作

/** * 對(duì)內(nèi)存中的圖片文件進(jìn)行Base64處理 * * @throws IOException */ public String Base64ImageByMemory(BufferedImage pic) { String imgString = ''; ByteArrayOutputStream newBaos = new ByteArrayOutputStream();//io流 try { ImageIO.write(pic, 'jpg', newBaos);//寫入流中 byte[] bytes = newBaos.toByteArray();//轉(zhuǎn)換成字節(jié) imgString = URLEncoder.encode(new BASE64Encoder().encode(bytes), 'UTF-8'); } catch (Exception e) { e.printStackTrace(); } return imgString; }

四、獲取服務(wù)器圖片文件大小

/** * 輸出到OutputStream * * @throws IOException */ public void getImageFileSize(){ int size; URLConnection conn; try { String path=''; path='https://bkimg.cdn.bcebos.com/pic/a8773912b31bb051c36044e93b7adab44bede0af';//世界地圖 //path='http://10.30.23.217:9017/image/0c09ca36-abea-4efa-85b0-99b6d261f66c'; //服務(wù)器上圖片 URL url = new URL(path); conn = url.openConnection(); size = conn.getContentLength(); if (size < 0){ System.out.println('Could not determine file size.'); }else{ System.out.println('The size of file is = ' + size + ' bytes'); BigDecimal filesize = new BigDecimal(size); BigDecimal megabyte = new BigDecimal(1024 * 1024); float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue(); System.out.println('The size of file is = '+returnValue+'M'); } conn.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); } }

Java 使用Thumbnails對(duì)大圖片壓縮

至此,圖片壓縮的相關(guān)處理和Base64以及獲取服務(wù)器文件大小的功能就總結(jié)完了!

以上就是Java 使用Thumbnails對(duì)大圖片壓縮的詳細(xì)內(nèi)容,更多關(guān)于java 大圖片壓縮的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 卢龙县| 保靖县| 桓仁| 西和县| 恩平市| 板桥市| 岐山县| 仁化县| 中方县| 溧阳市| 德保县| 金溪县| 临泽县| 遂川县| 尚义县| 灵寿县| 雅江县| 峡江县| 瑞昌市| 额济纳旗| 广德县| 丹巴县| 巫溪县| 柘城县| 仁怀市| 宿迁市| 筠连县| 革吉县| 宁乡县| 正宁县| 广东省| 陵水| 五家渠市| 浏阳市| 潼南县| 行唐县| 苏尼特右旗| 定结县| 湖南省| 平谷区| 永年县|