java等比压缩图片_Java 实现图片等比例压缩

本文介绍了如何使用Java源生类和第三方库Thumbnailator来实现图片等比例压缩。通过编写上传图片和压缩图片的业务逻辑,展示了具体的代码实现,包括上传图片到服务器并保存,以及使用Thumbnailator设置固定尺寸进行等比例压缩。还提供了Maven添加Thumbnailator库的依赖信息和项目实例代码链接。
摘要由CSDN通过智能技术生成

Java实现等比例压缩

1. 使用Java源生类实现

2. 使用第三方插件(Thumbnailator)

开发步骤:

创建工程

编写页面

编写Controller

编写业务逻辑

HTML代码

Java实现图片等比例压缩

控制器

@RequestMapping("upload")public String thumbnail(@RequestParam("fileNamePath") CommonsMultipartFile file, HttpSession session, Mapmap) {//图片保存的的文件名称

String uploadPath = "images";//项目发布的路径

String realUploadPath =session.getServletContext().getRealPath(uploadPath);

String imgeUrl= "";

String thumbnailImageUrl= "";//上传图片

imgeUrl =uploadService.uploadImage(file, uploadPath, realUploadPath);//压缩图片

thumbnailImageUrl =thumbnailService.thumbnail(file, uploadPath, realUploadPath);

map.put("imgeUrl", imgeUrl);

map.put("thumbnailImageUrl", thumbnailImageUrl);return "showImage";

}

业务逻辑

上传图片

@OverridepublicString uploadImage(CommonsMultipartFile file, String uploadPath, String realUploadPath) {

InputStream is= null;

OutputStream os= null;try{

is=file.getInputStream();

String des= realUploadPath + File.separator +file.getOriginalFilename();

os= newFileOutputStream(des);byte[] buffer = new byte[1024];int len = 0;while ((len = is.read(buffer)) > 0) {

os.write(buffer);

}

}catch(Exception e) {

e.printStackTrace();

}finally{if (is != null) {try{

is.close();

}catch(IOException e) {

e.printStackTrace();

}

}if (os != null) {try{

os.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}return uploadPath + File.separator +file.getOriginalFilename();

}

Thumbnailator 等比例压缩图片

public static final int WINDTH = 68;public static final int HEIGHT = 68;publicString thumbnail(CommonsMultipartFile file, String uploadPath, String realUploadPath) {try{

String des= realUploadPath + "/thum_" +file.getOriginalFilename();

Thumbnails.of(file.getInputStream()).size(WINDTH, HEIGHT).toFile(des);

}catch(IOException e) {

e.printStackTrace();

}return uploadPath + "/thum_" +file.getOriginalFilename();

}

Java源生等比例压缩图片

publicString thumbnail(CommonsMultipartFile file, String uploadPath, String realUploadPath) {

OutputStream os= null;

String desc= realUploadPath + "/thum_" +file.getOriginalFilename();try{

os= newFileOutputStream(desc);

Image image=ImageIO.read(file.getInputStream());int width = image.getWidth(null);//获得原图的宽度

int height = image.getHeight(null);//获得原图的高度

int rete1 = width / WIDTH;//宽度缩略比例

int rete2 = height / HEIGHT;//高度缩略比例

int rete = 0;if (rete1 >rete2) {

rete=rete1;

}else{

rete=rete2;

}//计算缩略图最总的宽度和高度

int newWidth = width /rete;int newHeight = height /rete;

BufferedImage bufferedImage= newBufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

bufferedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, image.SCALE_SMOOTH),0, 0, null);

String imageType= file.getContentType().substring(file.getContentType().indexOf("/") + 1);

ImageIO.write(bufferedImage, imageType, os);

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{if (null !=os) {try{

os.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}return uploadPath + "/thum_" +file.getOriginalFilename();

}

附件一:Maven添加Thumbnailatorjar

net.coobird

thumbnailator

0.4.8

附件二:项目实例代码

https://git.oschina.net/hxxiaodao/Thumbnail.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值