get Image from File - Java 2D Graphics

Java examples for 2D Graphics:Image File

Description

get Image from File

Demo Code


import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class Main{
    public static void main(String[] argv) throws Exception{
        String image = "java2s.com";
        System.out.println(getImage(image));
    }//from   ww w. j  a  v a2 s . c o m
    public static BufferedImage getImage(String image) {
        try {
            if (!image.contains("/") && !image.contains("\\")) {
                return makeCompatible(ImageIO.read(CanvasImageUtils.class
                        .getClassLoader().getResource(
                                "resources/images/" + image)));
            } else {
                return makeCompatible(ImageIO.read(CanvasImageUtils.class
                        .getClassLoader().getResource(image)));
            }
        } catch (IOException ex) {
            Logger.getLogger(CanvasImageUtils.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
        return null;

    }
    public static BufferedImage getImage(File file) {
        try {
            return makeCompatible(ImageIO.read(file));
        } catch (IOException ex) {
            Logger.getLogger(CanvasImageUtils.class.getName()).log(
                    Level.SEVERE, null, ex);
        }
        return null;

    }
    private static BufferedImage makeCompatible(BufferedImage image) {
        int w = image.getWidth();
        int h = image.getHeight();

        BufferedImage result = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = result.createGraphics();
        g.drawRenderedImage(image, new AffineTransform()); //or some other drawImage function
        g.dispose();

        return result;
    }
}

Related Tutorials