Java BufferedImage Load loadImage(File file)

Here you can find the source of loadImage(File file)

Description

Load an image from a file using ImageIO.

License

Open Source License

Parameter

Parameter Description
file File name

Exception

Parameter Description
IOException thrown on IO errors

Return

Image

Declaration

public static BufferedImage loadImage(File file) throws IOException 

Method Source Code

//package com.java2s;
/*/* w  w w  .ja va  2s .  c  o  m*/
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2013
 Ludwig-Maximilians-Universit?t M?nchen
 Lehr- und Forschungseinheit f?r Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class Main {
    /**
     * Load an image from a file using ImageIO.
     * 
     * @param file File name
     * @return Image
     * @throws IOException thrown on IO errors
     */
    public static BufferedImage loadImage(File file) throws IOException {
        ImageInputStream is = ImageIO.createImageInputStream(file);
        Iterator<ImageReader> iter = ImageIO.getImageReaders(is);

        if (!iter.hasNext()) {
            throw new IOException("Unsupported file format.");
        }
        ImageReader imageReader = iter.next();
        imageReader.setInput(is);
        return imageReader.read(0);
    }
}

Related

  1. loadFromResource(String imageName, Class cls)
  2. loadFromURL(String url)
  3. loadImage(byte[] data)
  4. loadImage(ClassLoader classLoader, String path)
  5. loadImage(File file)
  6. loadImage(File file)
  7. loadImage(File file)
  8. loadImage(File pFile)