日期:2014-05-18 浏览次数:20733 次
private void getLittleImg(HttpServletRequest request, String fromdir, String img) { String ext = img.substring(img.length()-3);//后缀名 double littleH = 84.00;//生成图片的高 double littleW = 118.00;//生成图片的宽 double HWRatio = littleH/littleW; double excRatio = 0.0; File imgFile = new File(request.getSession().getServletContext().getRealPath("/")+ fromdir, img); File excFile = new File(request.getSession().getServletContext().getRealPath("/") + fromdir + "/xImg", img); try { BufferedImage bi = ImageIO.read(imgFile); double imgFileRatio = new Double(bi.getHeight())/new Double(bi.getWidth()); Image excImage = bi.getScaledInstance ((int)littleW,(int)littleH,Image.SCALE_SMOOTH); excRatio = bi.getWidth()/littleW; if(imgFileRatio<HWRatio){ int excFileWidth = (int) (littleW*bi.getHeight()/littleH); int excFileY = (bi.getWidth()- excFileWidth)/2; excImage = bi.getSubimage(excFileY,0,excFileWidth,bi.getHeight()); excRatio = excFileWidth/littleW; }else if(imgFileRatio>HWRatio){ int excFileHeight = (int) (littleH*bi.getWidth()/littleW); int excFileX = (bi.getHeight()- excFileHeight)/2; excImage= bi.getSubimage(0,excFileX,bi.getWidth(),excFileHeight); } [color=#FF0000]//以上使处理图片长宽比例符合生成图片要求,没有问题[/color] excImage=excImage.getScaledInstance((int)littleW,(int)littleH,Image.SCALE_DEFAULT[color=#FF0000]);//但这句想改大小却有问题[/color] ImageIO.write((BufferedImage) excImage, ext, excFile); } catch (Exception ex) { log.error("上传图片生成缩微图失败", ex); } }