Java Image Load loadImage(String filePath, int imageSize)

Here you can find the source of loadImage(String filePath, int imageSize)

Description

load Image

License

Apache License

Declaration

public static Image loadImage(String filePath, int imageSize) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.BufferedInputStream;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

public class Main {
    public static byte[] loadImage(String imgPath, Class caller) {
        int MAX_IMAGE_SIZE = 2400; // Change this to the size of
        // your biggest image, in bytes.
        byte buf[] = null;
        int count = 0;
        System.out.println("Load Image Stream: " + imgPath);
        BufferedInputStream imgStream = null;
        imgStream = new BufferedInputStream(caller.getResourceAsStream(imgPath));
        if (imgStream == null) {
            System.err.println("Couldn't find file: " + imgPath);
            return null;
        }//ww  w .j a  v a 2  s.  c  o m

        // Creating the Image Icon from the stream
        try {
            System.out.println("Image Stream is Not Null: " + imgStream);
            System.out.println("Image Stream 4: " + imgPath + " is: " + imgStream.available());
            buf = new byte[imgStream.available()];
            count = imgStream.read(buf, 0, buf.length);
            System.out.println("Buffered Size: " + buf.length + " count: " + count);
            imgStream.close();
        } catch (IOException ioe) {
            System.err.println("Couldn't read stream from file: " + imgPath);
            return null;
        } catch (Throwable th) {
            th.printStackTrace();
            System.out.println("Cought Exception while reading stream");
            return null;
        }
        if (count <= 0) {
            System.err.println("Empty file: " + imgPath);
            return null;
        }
        return buf;
    }

    public static Image loadImage(byte[] imgStr, int imageSize) {
        ImageIcon imgIcon = new ImageIcon(imgStr);
        if (imgIcon == null)
            return null;
        return getScaledImage(imgIcon.getImage(), imageSize);
    }

    public static Image loadImage(String filePath, int imageSize) {
        ImageIcon imgIcon = new ImageIcon(filePath);
        if (imgIcon == null)
            return null;
        return getScaledImage(imgIcon.getImage(), imageSize);
    }

    public static BufferedImage LoadImage(String filename) {
        return LoadImage(new File(filename));
    }

    public static BufferedImage LoadImage(File imgFile) {
        BufferedImage bimg = null;
        try {
            System.out.println("Image File Size: " + imgFile.length());
            bimg = ImageIO.read(imgFile);
        } catch (Exception e) {
            System.out.println("Exception at LoadImage");
            e.printStackTrace();
        }
        return bimg;
    }

    public static Image getScaledImage(Image image, int imageSize) {
        if (imageSize == -1 || image.getWidth(null) <= imageSize)
            return image;
        // Reseampling the Image to create Thumbview. The getScaledInstance
        // method
        // maintains the Aspect Ratio.
        image = image.getScaledInstance(imageSize, -1, Image.SCALE_DEFAULT);
        return image;
    }
}

Related

  1. loadImage(File original)
  2. loadImage(final String src)
  3. loadImage(InputStream s)
  4. loadImage(Object parent, String pathToImage)
  5. loadImage(String filename)
  6. loadImage(URL url)
  7. readImage(InputStream inp)
  8. readImageToLabel(String netUrl)