Java BufferedImage Size Get getDimension(File f)

Here you can find the source of getDimension(File f)

Description

get Dimension

License

Apache License

Declaration

public static Dimension getDimension(File f) 

Method Source Code


//package com.java2s;
/*//  w w w  .  ja  v  a  2 s .  c om
   Copyright 2014 Shahriyar Amini
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    public static Dimension getDimension(File f) {
        if (f == null || !f.exists() || !f.canRead()) {
            return null;
        }

        BufferedImage bi;
        try {
            bi = ImageIO.read(f);
            return new Dimension(bi.getWidth(), bi.getHeight());
        } catch (IOException ex) {
            System.out.println("Could not read image");
            ex.printStackTrace();
        }

        return null;
    }
}

Related

  1. getDimension(BufferedImage img)
  2. getDimension(final Image anImage, final ImageObserver obs)
  3. getDimensions(byte[] image)
  4. getSize(BufferedImage image)
  5. getSize(int width, int height, Image image)