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