一尿赚、引入thumbnailator
- maven:
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
- gradle:
implementation 'net.coobird:thumbnailator:0.4.8'
二照卦、單元測試代碼
public class ThumnailTEst {
@Test
public void test1() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
URL url = Thread.currentThread().getContextClassLoader()
.getResource("cage1.jpeg");
//壓縮后轉(zhuǎn)向到內(nèi)存中
Thumbnails
.of(url)//可以是文件名或輸入流
.size(100, 100)
.rotate(90)//轉(zhuǎn)90度
.keepAspectRatio(true)//保持比例(默認)
.toOutputStream(out);
//將壓縮后的圖片變成Base64
String s=Base64.getEncoder().encodeToString(out.toByteArray());
System.out.println(s);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者