本帖最后由 zhangchu_63 于 2011-11-22 17:17:36 编辑

解决方案 »

  1.   

    jdk 版本 图片实际格式 
      

  2.   


    JDK 1.5 图片格式JPG
      

  3.   

    JDK换成1.6也不能,还是返回null
    private static void createSlt(File imageFile, String sltFilePath, int limit) { FileOutputStream fos = null; BufferedImage bi = null; try {
    fos = new FileOutputStream(sltFilePath);
    bi = ImageIO.read(imageFile); int newWidth = 0;
    int newHeight = 0;
    int yWidth = bi.getWidth(null);
    int yHeight = bi.getHeight(null); if (yWidth > limit || yHeight > limit) {
    if (yWidth > yHeight) {
    float rate = (float) yWidth / limit;
    newWidth = limit;
    newHeight = (int) Math.rint(yHeight / rate);
    } else {
    float rate = (float) yHeight / limit;
    yHeight = limit;
    newWidth = (int) Math.rint(yWidth / rate);
    }
    } else {
    newWidth = bi.getWidth(null);
    newHeight = bi.getHeight(null);
    } BufferedImage bufferedImage = new BufferedImage(newWidth,
    newHeight, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getGraphics().drawImage(
    bi.getScaledInstance(newWidth, newHeight, bi.SCALE_SMOOTH),
    0, 0, null); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
    encoder.encode(bufferedImage); } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    fos.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    }这个是完整的创建缩略图的方法,bi返回NULL
      

  4.   

    File file = new File("test.jpg");
    image = ImageIO.read(file);我的image文件大小为2.1MB, 没有问题!
    JDK6u20
      

  5.   

    找到问题了,原来图片虽然扩展名是JPG,但是实际格式是TIF,ImageIO不支持TIF。
    这里顺便贴出来ImageIO可以读写的图片格式。
    用:String[] rf = ImageIO.getReaderFormatNames();
    String[] wf = ImageIO.getWriterFormatNames();rf:jpg,rf:BMP,rf:bmp,rf:JPG,rf:jpeg,rf:wbmp,rf:png,rf:JPEG,rf:PNG,rf:WBMP,rf:GIF,rf:gif
    wf:BMP,wf:bmp,wf:jpg,wf:JPG,wf:wbmp,wf:jpeg,wf:png,wf:PNG,wf:JPEG,wf:WBMP,wf:GIF,wf:gifImageIO应该不会因为图片太大返回NULL,就算图片太大只会抛出内存溢出的错误。
    OK,结贴了。