Java BufferedImage Load loadImage(final File refDirectory, final String imageFileName)

Here you can find the source of loadImage(final File refDirectory, final String imageFileName)

Description

load Image

License

Open Source License

Declaration

public static BufferedImage loadImage(final File refDirectory, final String imageFileName) throws IOException 

Method Source Code

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

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    private static final boolean USE_GRAPHICS_ACCELERATION = false;
    private final static GraphicsConfiguration gc = (USE_GRAPHICS_ACCELERATION)
            ? GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
            : null;/*  w w  w. jav a 2 s .  c o  m*/

    public static BufferedImage loadImage(final File refDirectory, final String imageFileName) throws IOException {
        final File imageFile = new File(refDirectory, imageFileName);
        if (!imageFile.canRead()) {
            return null;
        }
        final BufferedImage image = ImageIO.read(imageFile);
        // convert image to compatible image:
        return copyImage(image, newImage(image.getWidth(), image.getHeight()));
    }

    public static BufferedImage copyImage(final BufferedImage srcImage) {
        return copyImage(srcImage, newImage(srcImage.getWidth(), srcImage.getHeight()));
    }

    public static BufferedImage copyImage(final BufferedImage srcImage, final BufferedImage destImage) {
        // Copy image to the output buffered image
        final Graphics2D g = destImage.createGraphics();
        try {
            g.drawImage(srcImage, 0, 0, null);
        } finally {
            g.dispose();
        }
        return destImage;
    }

    public static BufferedImage newImage(final int w, final int h) {
        if (USE_GRAPHICS_ACCELERATION) {
            return gc.createCompatibleImage(w, h);
        }
        return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    }
}

Related

  1. loadImage(File file)
  2. loadImage(File file)
  3. loadImage(File pFile)
  4. loadImage(final byte[] imageData)
  5. loadImage(final File imgFile)
  6. loadImage(final String fileName)
  7. loadImage(final String iconFile)
  8. loadImage(final String imagePathname, final Class relatedClass)
  9. loadImage(final String path)