我最近用java缩放图片2M左右可以,但是大一点的图片总是出错
这句 ImageIO.read(file);抛出异常
网上找了很久都没有能解决的办法
请高手们帮我解决一下了。
用java代码实现,不要调内存
要求能缩小10M以上大小的图片

解决方案 »

  1.   

    原先用ImageIo.read();来得BufferImage 总不行后来改成String   imageFile = "d:\\112.jpg";   
    InputStream imageIn = new FileInputStream(new File(imageFile));  
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);   
    BufferedImage image1 = decoder.decodeAsBufferedImage();10M图片是能缩小了,但是颜色严重失真,就像曝光的底片。
    2M图片还是跟原来一样正常缩小。
    高手快来帮忙解决啊
      

  2.   

     BufferedImage bis = ImageIO.read(doc);//待更改的文件
            int nw = 100;
            AffineTransform transform = new AffineTransform();
            int w = bis.getWidth();
            int h = bis.getHeight();
            double scale = (double)w/h;
            int nh = (nw*h)/w ;
            double sx = (double)nw/w;
            double sy = (double)nh/h;
            transform.setToScale(sx,sy);
            AffineTransformOp ato = new AffineTransformOp(transform,null);
            BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);//             更改后的文件
            ato.filter(bis,bid);
            ImageIO.write(bid,"jpeg",doc);
      

  3.   

    public static void createFixedBoundImg(String OriFilePath, String TargetFilePath, int height, int width) throws Exception {
    double Ratio = 0.0;
    File f = new File(OriFilePath);
    Image src = ImageIO.read(f);
    int oriWidth = src.getWidth(null);
    int oriHeight = src.getHeight(null);
    int tagWidth, tagHeight;
    if (oriWidth > width || oriHeight > height) {
    if (oriHeight > oriWidth) {
    Ratio = (new Integer(height)).doubleValue() / oriHeight;
    tagHeight = height;
    tagWidth = (int) (oriWidth * Ratio);
    }
    else {
    Ratio = (new Integer(width)).doubleValue() / oriWidth;
    tagHeight = (int) (oriHeight * Ratio);
    tagWidth = width;
    }
    }
    else {
    tagHeight = oriHeight;
    tagWidth = oriWidth;
    }
    BufferedImage target = new BufferedImage(tagWidth, tagHeight, BufferedImage.TYPE_INT_RGB);
    target.getGraphics().drawImage(src, 0, 0, tagWidth, tagHeight, null);
    FileOutputStream out = new FileOutputStream(TargetFilePath);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(target);
    out.close();
    }

    调用这个方法,传四个参数:1、原图片绝对路径  2、缩略图绝对路径  3、生成缩略图后的高度   4、生成缩略图后的宽度
      

  4.   

    http://www.cnblogs.com/hxling/archive/2008/09/27/1301101.html
    没试过
    楼主去看看
      

  5.   

    弱弱地问一句,这个图片的缩放不是页面处理的么?个人觉得,对于太大的图片,还是用ps等工具把它先转换了,没必要非要等到要调用了交给java去处理,这里面也有效率的问题。。
      

  6.   

    ps 转不现实啊
    客户不会用ps怎么啊?他们也没时间去处理啊
      

  7.   

    public static void transformImage(String imgsrc, String imgdist,int size) {
    if(size==0){
    return;
    }
       try {
        BufferedImage src = ImageIO.read(new File(imgsrc)); // 读入文件
        double width = src.getWidth(); // 得到源图宽
        double height = src.getHeight(); // 得到源图长 if(width>=size||height>=size){
    if(width>height){
    height=height/(width/size);
    width=size;
    }else{
    width=width/(height/size);
    height=size;
    }
    }
        reduceImg(imgsrc,imgdist,(int)width,(int)height);
       }catch(Exception e){
       e.printStackTrace();
       }
    } public static void reduceImg(String imgsrc, String imgdist, int widthdist, int heightdist) {
    FileOutputStream out = null;
        try {
            File srcfile = new File(imgsrc);
            if (!srcfile.exists()) {
                return;
            }
            Image src = javax.imageio.ImageIO.read(srcfile);         BufferedImage tag= new BufferedImage((int) widthdist, (int) heightdist,BufferedImage.TYPE_INT_RGB);         tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,Image.SCALE_SMOOTH), 0, 0,  null);         out = new FileOutputStream(imgdist);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);     } catch (IOException ex) {
            ex.printStackTrace();
        }finally{
         try{
         if(out!=null)out.close();
         }catch(Exception e){e.printStackTrace();}
        }
    }
      

  8.   

    public String getSmallJPG(String fullname,int width,int height,String type)throws Exception{          //width 和height分别为需要裁剪的规格(等比例的)
            File _file = new File(fullname); //读入文件
            if(!_file.canRead()){
             return null;
            }
            Image src = javax.imageio.ImageIO.read(_file); //构造Image对象  
            int x=src.getWidth(null); //得到源图宽  
            int y=src.getHeight(null);//得到源图高
           
            float xness = (float)x;
            float yness = (float)y;
            if(x-y>=0){
                height = (int) (width*(yness/xness));
            }else{
                width = (int) (height*(xness/yness));
            }    
            //得到源图长  
            BufferedImage tag = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);  
            tag.getGraphics().drawImage(src.getScaledInstance(width, height, Image.SCALE_SMOOTH),0,0,null);//绘制缩小后的图  
            String _filename = _file.getName();  
            FileOutputStream out = new FileOutputStream(_file.getParent()+type+_filename);//输出到文件流  
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);                
            encoder.encode(tag);//近JPEG编码       
            String newsrc = _file.getParent()+type+_filename;
            out.close();
            return newsrc;
        }