java 生成缩略图_Java实现等比例缩略图
1.簡介:Web應(yīng)用為上傳圖片生成縮略圖是常見的基本功能,通過縮略圖生成提高了信息瀏覽時的性能,在保證用戶使用體驗的同時減少了數(shù)據(jù)傳輸量。
2.實現(xiàn)圖片等比例縮略圖生成,方式及相關(guān)工具介紹:
(1)Thumbnailator類庫(推薦)
工具:size()API方法
(2)Java AWT類庫(復(fù)雜)
根據(jù)縮略比例計算縮略圖高度賀寬度,使用Image類獲得原圖的縮放版本,使用ImageIO類保存縮略圖;
工具:bufferedImage(圖像的存儲和操作);ImageIO(圖片讀入、輸出、生成);Graphics(圖片的繪制)。
本實例是基于springMVC框架的Java web應(yīng)用程序,允許上傳圖片,并生成圖片的縮略圖。
3.實現(xiàn)步驟
(1)應(yīng)用程序框架搭建;
(2)上傳文件界面的開發(fā);
(3)控制器開發(fā);
(4)編寫圖片上傳服務(wù)類;
(5)編寫縮略圖生成服務(wù)類
需要的關(guān)鍵壓縮使用的jar包:
搭建web項目目錄:
直接上核心代碼:
控制層controller實現(xiàn):
@Controller
@RequestMapping("/")public classThumbnailController {
@AutowiredprivateUploadService uploadService;
@AutowiredprivateThumbnailService thumbnailService;
@RequestMapping(value="/thumbnail",method=RequestMethod.POST)public ModelAndView thumbnail(@RequestParam("image") CommonsMultipartFile file,HttpSession session) throwsException{
System.out.println("=========================");//主要針對于圖片上傳//相對路徑
String uploadPath="/images";//圖片在服務(wù)器上的絕對路徑信息
String realUploadPath=session.getServletContext().getRealPath(uploadPath);//返回的結(jié)果//圖片原圖在服務(wù)器上訪問的相對路徑信息
String imageUrl=uploadService.uploadImage(file, uploadPath, realUploadPath);//縮略圖訪問路徑
String thumImageUrl=thumbnailService.thumbnail(file, uploadPath, realUploadPath);//設(shè)置返回前端顯示(渲染)
ModelAndView modelAndView=newModelAndView();
modelAndView.addObject("imageURL", imageUrl);
modelAndView.addObject("thumImageURL", thumImageUrl);
modelAndView.setViewName("thumbnail");returnmodelAndView;
}
}
文件上傳的業(yè)務(wù)實現(xiàn)類,代碼(service):
/*** 文件上傳文件的處理*/@Servicepublic classUploadService {publicString uploadImage(CommonsMultipartFile file,String uploadPath,String realUploadPath){
InputStream iStream=null;
OutputStream oStream=null;try{//獲取上傳文件的流文件
iStream=file.getInputStream();//創(chuàng)建文件輸出流與位置
String des=realUploadPath+"/"+file.getOriginalFilename();
oStream=newFileOutputStream(des);byte[] buffer=new byte[1024];while (iStream.read(buffer)>0) {
oStream.write(buffer);
}
}catch(Exception e) {
e.printStackTrace();
}finally{if(iStream!=null){try{
iStream.close();
}catch(IOException e) {
e.printStackTrace();
}
}if(oStream!=null){try{
oStream.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}//返回文件上傳的相對路徑
return uploadPath+"/"+file.getOriginalFilename();
}
}
圖片縮略處理,實現(xiàn)方式:
(1)Thumbnails圖片縮略處理,代碼如下:
/*** Thumbnails圖片縮略處理*/@Servicepublic classThumbnailService {public static final int WIDTH=100;public static final int HEIGHT=100;publicString thumbnail(CommonsMultipartFile file,String uploadPath,String realUploadPath){try{
String des=realUploadPath+"/thum_"+file.getOriginalFilename();//圖片縮略圖實現(xiàn),強制按照指定的寬高進行縮略keepAspectRatio(false)
//方式一//Thumbnails.of(file.getInputStream()).size(WIDTH, HEIGHT).toFile(des);
//方式二
Builder extends InputStream> thumbnail =Thumbnails.of(file.getInputStream());
thumbnail.size(WIDTH, HEIGHT);
thumbnail.toFile(des);
}catch(Exception e) {
e.printStackTrace();
}//縮略圖返回的相對路徑
return uploadPath+"/thum_"+file.getOriginalFilename();
}
}
(2)AWT圖片縮略處理,代碼如下:
/*** AWT圖片縮略處理*/@Servicepublic classAWTService {public static final int WIDTH=100;public static final int HEIGHT=100;publicString thumbnail(CommonsMultipartFile file,String uploadPath,String realUploadPath){
OutputStream oStream=null;try{
String des=realUploadPath+"/thum_"+file.getOriginalFilename();
oStream=newFileOutputStream(des);//ImageIO獲取圖片流信息
Image image=ImageIO.read(file.getInputStream());int width=image.getWidth(null); //獲取原圖寬度
int height=image.getHeight(null);//獲取原圖高度
int wrate=width/WIDTH; //寬度縮略圖
int hrate=height/HEIGHT;//高度縮略圖
int rate=0;if (wrate>hrate) {//寬度縮略圖比例大于高度縮略圖,使用寬度縮略圖
rate=wrate;
}else{
rate=hrate;
}//計算縮略圖最終的寬度和高度
int newWidth=width/rate;int newHeight=height/rate;
BufferedImage bufferedImage=newBufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);//圖片縮略圖實現(xiàn)
bufferedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, image.SCALE_SMOOTH), 0, 0, null);//*image/jpeg
String imageType=file.getContentType().substring(file.getContentType().indexOf("/")+1);
ImageIO.write(bufferedImage, imageType, oStream);
}catch(Exception e) {
e.printStackTrace();
}finally{try{
oStream.close();
}catch(IOException e) {
e.printStackTrace();
}
}//縮略圖返回的相對路徑
return uploadPath+"/thum_"+file.getOriginalFilename();
}
}
配置信息一,web容器配置,web.xml:
ImageCompress
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springmvc
/
index.jsp
配置信息二,springmvc容器配置,spring-mvc.xml:
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的java 生成缩略图_Java实现等比例缩略图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 广东清远有哪些漂流的地方(广东清远这些独
- 下一篇: java 传递函数_java传递函数参数