1、添加依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.11</version>
</dependency>
主要代码:
size(width,height) 图片长宽
rotate(角度),正数:顺时针 负数:逆时针
scale(比例)
keepAspectRatio(false) 默认是按照比例缩放的
watermark(位置,水印图,透明度)
outputFormat(图像格式)
Thumbnails.of(in).size(600,400).rotate(90).toOutputStream(out);
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
@Slf4j
public class ImageUtils {
private static final Integer KB = 1024;
/**
* 图片压缩指定长宽处理
* @param origPicContent
* @return
*/
public static String compressPic(String origPicContent) {
ByteArrayInputStream in = null;
ByteArrayOutputStream out = null;
try {
byte[] bytes = new BASE64Decoder().decodeBuffer(origPicContent);
in = new ByteArrayInputStream(bytes);
out = new ByteArrayOutputStream();
Thumbnails.of(in).size(600,400).toOutputStream(out);
return new BASE64Encoder().encode(bytes).replaceAll("\\r\\n","");
} catch (IOException e) {
log.error ("decode buffer fail, message:{}", e.getMessage (), e);
} finally {
if (in != null) {
try {
in.close ();
} catch (IOException e) {
log.error ("ByteArrayInputStream close fail, message:{}", e.getMessage (), e);
}
}
if (out != null) {
try {
out.close ();
} catch (IOException e) {
log.error ("ByteArrayOutputStream close fail, message:{}", e.getMessage (), e);
}
}
}
return origPicContent;
}
/**
* 图片压缩到指定大小
* @param origPicContent
* @param desFileSize
* @return
*/
public static String compressPic(String origPicContent, Integer desFileSize) {
ByteArrayInputStream in = null;
ByteArrayOutputStream out = null;
try {
byte[] bytes = new BASE64Decoder().decodeBuffer(origPicContent);
while (bytes.length > desFileSize * KB) {
in = new ByteArrayInputStream(bytes);
out = new ByteArrayOutputStream();
Thumbnails.of(in)
.scale(0.8f)
// .outputQuality(accuracy)
.toOutputStream(out);
bytes = out.toByteArray();
}
return new BASE64Encoder().encode(bytes).replaceAll("\\r\\n","");
} catch (IOException e) {
log.error ("decode buffer fail, message:{}", e.getMessage (), e);
} finally {
if (in != null) {
try {
in.close ();
} catch (IOException e) {
log.error ("ByteArrayInputStream close fail, message:{}", e.getMessage (), e);
}
}
if (out != null) {
try {
out.close ();
} catch (IOException e) {
log.error ("ByteArrayOutputStream close fail, message:{}", e.getMessage (), e);
}
}
}
return origPicContent;
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(testText());
String img = sb.toString().split(",")[1];
System.out.println("length:"+img.length());
String image = ImageUtils.compressPic(img, 200);
System.out.println("length:"+image.length());
System.out.println(image);
}
private static String testText(){
StringBuilder result = new StringBuilder();
try {
File file = new File("D:\\imgFile.txt");
//构造一个BufferedReader类来读取文件
BufferedReader br = new BufferedReader(new FileReader(file));
String s = null;
//使用readLine方法,一次读一行
while((s = br.readLine())!=null){
result.append(s);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
}
参考文章: