引言
在最近的项目开发中,经常会使用到图片上传,但是过大的图片在查看的时候会影响打开速度,浪费流量以及服务器存储空间,所以需要在后端处理完图片再上传,这里我们用到了Thumbnails图片处理工具类。
Thumbnails主要支持以下一些功能
1、指定大小进行缩放
2、按照比例进行缩放
3、不按照比例,指定大小进行缩放
4、旋转
5、水印
6、裁剪
7、转化图片格式
8、输出到OutputStream
9、输出到BufferedImage
使用步骤:
一、添加Maven
net.coobird thumbnailator0.4.8
二、具体操作
1:指定大小进行缩放
private void test1() throws IOException {
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:按照比例进行缩放
private void test2() throws IOException {
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:不按照比例,指定大小进行缩放
private void test3() throws IOException {
Thumbnails.of("images/test.jpg").size(120, 120).keepAspectRatio(false).toFile("C:/image_120x120.jpg"); }
4:旋转
private void test4() throws IOException {
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:水印
private void test5() throws IOException {
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:裁剪
private void test6() throws IOException {
Thumbnails.of("images/test.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_center.jpg");
Thumbnails.of("images/test.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false)
.toFile("C:/image_region_bootom_right.jpg");
Thumbnails.of("images/test.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:/image_region_coord.jpg");
}
7:转化图片格式
private void test7() throws IOException {
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
private void test8() throws IOException {
OutputStream os = new FileOutputStream("C:/image_1280x1024_OutputStream.png");
Thumbnails.of("images/test.jpg").size(1280, 1024).toOutputStream(os);
}
9:输出到BufferedImage
private void test9() throws IOException {
BufferedImage thumbnail = Thumbnails.of("images/test.jpg").size(1280, 1024).asBufferedImage();
ImageIO.write(thumbnail, "jpg", new File("C:/image_1280x1024_BufferedImage.jpg"));
}
三、对图片文件进行base64操作
public String base64ImageByMemory(BufferedImage pic) {
String imgString = "";
ByteArrayOutputStream newBaos = new ByteArrayOutputStream();//io流
try {
ImageIO.write(pic, "jpg", newBaos);//写入流中
byte[] bytes = newBaos.toByteArray();//转换成字节
imgString = URLEncoder.encode(new base64Encoder().encode(bytes), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return imgString;
}
四、获取服务器图片文件大小
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"; //服务器上图片
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();
}
}
至此,图片压缩的相关处理和base64以及获取服务器文件大小的功能就总结完了!
以上就是Java 使用Thumbnails对大图片压缩的详细内容,更多关于java 大图片压缩的资料请关注考高分网其它相关文章!



