Java Image Create getImage(File imageFile)

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

Description

get Image

License

Open Source License

Declaration

public static BufferedImage getImage(File imageFile) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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;

import javax.swing.JOptionPane;

public class Main {
    public static BufferedImage getImage(File imageFile) {
        BufferedImage al = null;/*from  w  w w  .j  a v  a  2  s  . c o m*/
        try {
            String imageFileName = imageFile.getName();
            String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1);
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat);
            ImageReader reader = readers.next();

            if (reader == null) {
                JOptionPane.showConfirmDialog(null,
                        "Need to install JAI Image I/O package.\nhttps://jai-imageio.dev.java.net");
                return null;
            }

            ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
            reader.setInput(iis);

            al = reader.read(0);

            reader.dispose();
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

        return al;
    }
}

Related

  1. createImageFromVisibleComponent(JComponent comp, int cw, int ch)
  2. createImagePanel(Image image, Color backColor)
  3. getImage(Class clazz, String path)
  4. getImage(Component component)
  5. getImage(Component component)
  6. getImage(final int width, final int height)
  7. getImage(final String name)
  8. getImage(ImageIcon imageIcon)
  9. getImage(JComponent component)