Java BufferedImage Create getBufferedImage(byte[] imgStr)

Here you can find the source of getBufferedImage(byte[] imgStr)

Description

get Buffered Image

License

Apache License

Declaration

public static BufferedImage getBufferedImage(byte[] imgStr) throws Exception 

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 BufferedImage getBufferedImage(String imgPath) throws Exception {
        File f = new File(imgPath);
        BufferedImage img = null;
        img = ImageIO.read(f);/*ww  w . ja  v a2s.  co  m*/
        return img;
    }

    public static BufferedImage getBufferedImage(String imgPath, Class caller) throws Exception {
        // System.out.println("**ENTERED loadImage**");
        // System.out.println("Inside loadImage pathname is:- " + imgPath);
        File f = new File(imgPath);
        BufferedImage img = null;
        try {
            img = ImageIO.read(f);
        } catch (Throwable e) {
            String resImgPath = "/" + imgPath;
            img = getBufferedImage(loadImage(resImgPath, caller));
        }
        return img;
    }

    public static BufferedImage getBufferedImage(byte[] imgStr) throws Exception {
        // System.out.println("Buffered Size: " + imgStr.length);
        return ImageIO.read(new java.io.ByteArrayInputStream(imgStr));
    }

    public static BufferedImage getBufferedImage(byte[] imgStr, int imageSize) throws Exception {
        BufferedImage bufImg = getBufferedImage(imgStr);
        if (imageSize == -1 || bufImg.getWidth(null) <= imageSize)
            return bufImg;
        // Reseampling the Image to create Thumbview. The getScaledInstance
        // method
        // maintains the Aspect Ratio.
        bufImg = (BufferedImage) bufImg.getScaledInstance(imageSize, -1, Image.SCALE_DEFAULT);
        return bufImg;
    }

    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;
        }

        // 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. convertToBufferedImage(BufferedImage image, int imageType)
  2. getBufferedImage(Component comp, BufferedImage image)
  3. getBufferedImage(File file)
  4. getBufferedImage(String imageFile, Component c)
  5. getBufferedImage(String imagePath)