Java BufferedImage Read isImage(File file)

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

Description

Check whether a given file is an image that can be read by Java.

License

Open Source License

Parameter

Parameter Description
file the file to check

Return

true if the file is a valid image, false otherwise.

Declaration

public static boolean isImage(File file) 

Method Source Code

//package com.java2s;
/*/*from   ww w . j a v a 2  s  .  c  om*/
 This file is part of the Greenfoot program. 
 Copyright (C) 2005-2009  Poul Henriksen and Michael Kolling 
    
 This program is free software; you can redistribute it and/or 
 modify it under the terms of the GNU General Public License 
 as published by the Free Software Foundation; either version 2 
 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 General Public License for more details. 
    
 You should have received a copy of the GNU General Public License 
 along with this program; if not, write to the Free Software 
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
    
 This file is subject to the Classpath exception as provided in the  
 LICENSE.txt file that accompanied this code.
 */

import java.awt.image.BufferedImage;

import java.io.File;

import javax.imageio.ImageIO;

public class Main {
    /**
     * Check whether a given file is an image that can be read by Java.
     * @param file the file to check
     * @return true if the file is a valid image, false otherwise.
     */
    public static boolean isImage(File file) {
        try {
            BufferedImage img = ImageIO.read(file);
            if (img == null)
                return false;
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
}

Related

  1. isImage(File file)
  2. isImage(File imageFile)