Java BufferedImage from Byte Array decodeForRGBBufferedImage(String filePath)

Here you can find the source of decodeForRGBBufferedImage(String filePath)

Description

Read the file and return it as RGB buffered image

License

Open Source License

Parameter

Parameter Description
filePath a parameter

Exception

Parameter Description
Exception an exception

Return

image as buffered image

Declaration

public static BufferedImage decodeForRGBBufferedImage(String filePath) throws Exception 

Method Source Code


//package com.java2s;
import java.awt.Dimension;
import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import java.awt.image.RenderedImage;

import java.io.File;

import javax.imageio.ImageIO;

public class Main {
    /**//w ww  .j  a  v  a  2s.com
     * Read the file and return it as RGB buffered image     
     * @param filePath
     * @return image as buffered image
     * @throws Exception
     */
    public static BufferedImage decodeForRGBBufferedImage(String filePath) throws Exception {
        BufferedImage bi = null;
        try {
            File file = new File(filePath);
            RenderedImage im = ImageIO.read(file);
            Dimension imageSize = new Dimension(im.getWidth(), im.getHeight());
            bi = new BufferedImage(imageSize.width, imageSize.height, BufferedImage.TYPE_INT_ARGB);
            drawImage(bi, im);
        } catch (Exception e) {
            System.out.println("cannot read file:  " + filePath + " as RGB buffered image");
        }
        return bi;
    }

    private static void drawImage(BufferedImage bi, RenderedImage im) {
        Graphics2D g = bi.createGraphics();
        g.drawRenderedImage(im, new AffineTransform());
        g.dispose();
    }
}

Related

  1. fromByteArray(byte[] byteArray)
  2. fromByteArray(byte[] bytes)
  3. fromByteArray(byte[] data)
  4. fromByteArray(byte[] imagebytes)