Java Utililty Methods BufferedImage from Byte Array

List of utility methods to do BufferedImage from Byte Array

Description

The list of methods to do BufferedImage from Byte Array are organized into topic(s).

Method

BufferedImagedecodeForRGBBufferedImage(String filePath)
Read the file and return it as RGB buffered image
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) {
...
BufferedImagefromByteArray(byte[] byteArray)
from Byte Array
return ImageIO.read(new ByteArrayInputStream(byteArray));
BufferedImagefromByteArray(byte[] bytes)
Convert byte[] to BufferedImage
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
    return ImageIO.read(in);
} catch (IOException e) {
    e.printStackTrace();
    System.err.println("Failed to convert byte array to image");
return null;
...
BufferedImagefromByteArray(byte[] data)
from Byte Array
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
return ImageIO.read(inputStream);
BufferedImagefromByteArray(byte[] imagebytes)
Constructs image from byte array.
try {
    if (imagebytes != null && (imagebytes.length > 0)) {
        BufferedImage im = ImageIO.read(new ByteArrayInputStream(imagebytes));
        return im;
    return null;
} catch (IOException e) {
    throw new IllegalArgumentException(e.toString());
...
BufferedImagefromByteIntensity(BufferedImage image, byte[] bytes)
from Byte Intensity
int width = image.getWidth();
int height = image.getHeight();
switch (image.getType()) {
case BufferedImage.TYPE_BYTE_BINARY:
case BufferedImage.TYPE_BYTE_GRAY: {
    image.getWritableTile(0, 0).setDataElements(0, 0, width, height, bytes);
    return image;
case BufferedImage.TYPE_INT_ARGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
case BufferedImage.TYPE_INT_RGB: {
    int[] data = new int[width * height];
    for (int i = 0; i < bytes.length; i++) {
        int b = bytes[i] & 0xff;
        data[i] = (((((0xff << 8) | b) << 8) | b) << 8) | b;
    image.getWritableTile(0, 0).setDataElements(0, 0, width, height, data);
    return image;
case BufferedImage.TYPE_3BYTE_BGR: {
    byte[] data = new byte[width * height * 3];
    int offset = 0;
    for (int i = 0; i < bytes.length; i++) {
        byte b = bytes[i];
        data[offset++] = b;
        data[offset++] = b;
        data[offset++] = b;
    image.getWritableTile(0, 0).setDataElements(0, 0, width, height, data);
    return image;
default:
    throw new IllegalArgumentException("Unsupported image type: " + image.getType());
BufferedImagefromIntIntensityScaled(int width, int height, int[] data)
from Int Intensity Scaled
int max = 0;
for (int d : data)
    if (d > max)
        max = d;
    else if (-d > max)
        max = -d;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
byte[] bytes = new byte[data.length];
...
BufferedImagefromIntRGB(BufferedImage image, int[] rgb)
from Int RGB
if (image.getType() != BufferedImage.TYPE_INT_RGB)
    throw new IllegalArgumentException();
image.getWritableTile(0, 0).setDataElements(0, 0, image.getWidth(), image.getHeight(), rgb);
return image;
BufferedImagegetBufferedImage(byte[] imageBytes)
get Buffered Image
ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
return ImageIO.read(input);
BufferedImagegetImage(byte[] bytes)
get Image
try {
    return ImageIO.read(new ByteArrayInputStream(bytes));
} catch (IOException e) {
    throw new RuntimeException(e);