慕的地5420249
2018-11-30 13:58
项目源码在哪?能提供一下吗?
@Service public class ThumbnailService { public static final int WIDTH = 100; public static final int HEIGHT = 100; //使用类库 public String thumbnail(MultipartFile file,String uploadPath, String realUploadPath){ String imgName = "thum_"+file.getOriginalFilename(); String des = realUploadPath+"/"+imgName; try { Thumbnails.of(file.getInputStream()).size(WIDTH,HEIGHT).toFile(des); }catch (Exception e){ e.printStackTrace(); } return uploadPath+"/"+imgName; } //使用java awt public String awt(MultipartFile file,String uploadPath, String realUploadPath){ OutputStream os = null; String imgName = "thum_"+file.getOriginalFilename(); String des = realUploadPath+"/"+imgName; try { os = new FileOutputStream(des); Image image = ImageIO.read(file.getInputStream()); int width = image.getWidth(null);//原图宽 int height = image.getHeight(null);//原图高 int rate1 = width/WIDTH;//缩略图宽比例 int rate2 = height/HEIGHT;//缩略图高比例 int rate = 0; if(rate1 > rate2){//宽度缩略比例大于高度缩略比例,使用宽度缩略比例 rate = rate1; }else { rate = rate2; } //计算缩略图最终的宽度和高度 int newWidth = width/rate; int newHeight = height/rate; BufferedImage bufferedImage = new BufferedImage(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 (Exception e){ e.printStackTrace(); }finally { if(os!=null){ try { os.close(); }catch (Exception e){ e.printStackTrace(); } } } return uploadPath+"/"+imgName; } }
Java实现图片等比例缩略图
25334 学习 · 16 问题
相似问题