日期:2014-05-18  浏览次数:20733 次

关于图像处理,先谢了
写了个缩微图的程序如下,我是先把要处理的图片裁切成要求的比例,然后再缩放.
可是裁切没问题,可是加上缩放就出错,哪位高手帮帮小弟!
谢谢了


Java code

    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);
        }

    }





------解决方案--------------------
你这里一个裁减一个缩放都是同一个变量excImage,建议使用2个不同的变量试试看
------解决方案--------------------
记得那本厚厚的入门书,讲swing很多那本,高级篇那本,里面有实例