Java BufferedImage Load loadImageFromFile_ORIG(String imgName)

Here you can find the source of loadImageFromFile_ORIG(String imgName)

Description

load Image From FilORIG

License

Open Source License

Declaration

public static Image loadImageFromFile_ORIG(String imgName) 

Method Source Code


//package com.java2s;
/*//from w  ww . j  a v  a  2  s . c  o  m
 * $Id: PixelUtil.java,v 1.3 2007/01/29 09:08:46 eiki Exp $ Created on May 29,
 * 2006
 * 
 * Copyright (C) 2006 Idega Software hf. All Rights Reserved.
 * 
 * This software is the proprietary information of Idega hf. Use is subject to
 * license terms.
 */

import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static Image loadImageFromFile_ORIG(String imgName) {
        byte[] imageBytes = getBytesFromFile(imgName);
        Image tmpi = null;
        int numTries = 20;
        if (imageBytes != null) {
            tmpi = Toolkit.getDefaultToolkit().createImage(imageBytes, 0, imageBytes.length);
            while (tmpi.getWidth(null) < 0 && numTries-- > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
            }
            while (tmpi.getHeight(null) < 0 && numTries-- > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println(e);
                }
            }
        }
        return tmpi;
    }

    /**
     * Given name of file, return entire file as a byte array.
     * 
     * @param filename
     * @return
     */
    public static byte[] getBytesFromFile(String filename) {
        File f = new File(filename);
        byte[] bytes = null;
        if (f.exists()) {
            try {
                bytes = getBytesFromFile(f);
            } catch (Exception e) {
                System.out.println("getBytesFromFile() exception: " + e);
            }
        }
        return bytes;
    }

    /**
     * Given File object, returns the contents of the file as a byte array.
     */
    public static byte[] getBytesFromFile(File file) throws IOException {
        byte[] bytes = null;
        if (file != null) {
            InputStream is = new FileInputStream(file);
            long length = file.length();
            // Can't create an array using a long type.
            // Before converting to an int type, check
            // to ensure that file is not larger than Integer.MAX_VALUE.
            if (length > Integer.MAX_VALUE) {
                System.out.println("getBytesFromFile() error: File " + file.getName() + " is too large");
            } else {
                // Create the byte array to hold the data
                bytes = new byte[(int) length];
                int offset = 0;
                int numRead = 0;
                // Read in the bytes
                while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                    offset += numRead;
                }
                // Ensure all the bytes have been read in
                if (offset < bytes.length) {
                    throw new IOException(
                            "getBytesFromFile() error: Could not completely read file " + file.getName());
                }
            }
            // Close the input stream and return bytes
            is.close();
        }
        return bytes;
    }

    /**
     * Creates a BufferedImage using
     * GraphicsEnvironment...createCompatibleImage(...)<br>
     * The BufferedImage returned is colormodel compatible with the display, and
     * will be managed (cachable in vram)
     * 
     * @param width
     * @param height
     * @return A BufferedImage that is colormodel compatible with the display,
     *         and will be managed (cachable in vram)
     */
    public static BufferedImage createImage(int width, int height) {
        return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
                .createCompatibleImage(width, height, Transparency.BITMASK);
    }
}

Related

  1. loadImageFromFile(File f)
  2. loadImageFromFile(File file)
  3. loadImageFromFile(String filename)
  4. loadImageFromFile(String fileName)
  5. loadImageFromFile(String filepath)
  6. loadImagefromJar(Object refObj, String fileName)
  7. loadImageFromURL(String imageURL, Component component)
  8. loadImageFromVisualgorithmJar(String imageFileName)
  9. loadImageRessource(Object source, String file)