Java Image File Check isImage(File imageFile)

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

Description

is Image

License

Apache License

Declaration

public static boolean isImage(File imageFile) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.InputStream;

public class Main {
    public static boolean isImage(File imageFile) {
        if (!imageFile.exists()) {
            return false;
        }//from   w w w. j  av  a 2s .c o  m
        Image img = null;
        try {
            img = ImageIO.read(imageFile);
            if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            img = null;
        }
    }

    public static boolean isImage(InputStream imageFile) {
        Image img = null;
        try {
            img = ImageIO.read(imageFile);
            if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            img = null;
        }
    }
}

Related

  1. isImage(File img)
  2. isImage(File imgFile)
  3. isImage(InputStream is)