Java Program to Convert Byte Array to Image
Last Updated :
30 Jan, 2022
A byte array is the array of bytes that is used to store the collection of binary data. For example, the byte array of an image stores the information of every pixel of the image. In the byte array, we can store the content of any file in binary format. We can initialize the byte array with the bytes as we initialize the normal array.
In this article, we are going to learn to convert the byte array to an image in Java.
Converting Byte Array to Image:
If we have given information of every byte of the image in the array format, we can derive the image from that array. We can write and read the image details using the ImageIO class in Java. We will use the below methods of the ImageIO class to convert the byte array into the image.
- toByteArray(): To convert the image to byte array.
- ByteArrayInputStream( byteArray ): To create object of the ByteArrayInputStream class.
- ImageIO.read(): To read the image by passing the ByteArrayInputStream class object as a parameter.
- ImageIO.write(): To write the image.
Approach:
1. Here, we need the byte array to convert it into the image. So, we first read the image file and create the byte array for that image.
Read the image file using the read() method of Image.IO class.
BufferedImage image = ImageIO.read(new File("Image path"));
Create the object of the ByteArrayOutputStream class and write the image into that which we have read in the above step.
ByteArrayOutputStream outStreamObj = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", outStreamObj);
Convert the image into the byte array.
byte [] byteArray = outStreamObj.toByteArray();
2. Now, read the byte array and generate a new image file.
Create the object of the ByteArrayInputStream class to read the byte array.
ByteArrayInputStream inStreambj = new ByteArrayInputStream(byteArray);
Read the image from the object of the ByteArrayInputStream class.
BufferedImage newImage = ImageIO.read(inStreambj);
Create a new file and write an image into that which we have read from the byte array.
ImageIO.write(newImage, "jpg", new File("outputImage.jpg") );
Example:
Java
// Java program to convert byte array to image.
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
class GFG {
public static void main (String[] args) {
// read the image from the file
BufferedImage image = ImageIO.read(new File("Image path"));
// create the object of ByteArrayOutputStream class
ByteArrayOutputStream outStreamObj = new ByteArrayOutputStream();
// write the image into the object of ByteArrayOutputStream class
ImageIO.write(image, "jpg", outStreamObj);
// create the byte array from image
byte [] byteArray = outStreamObj.toByteArray();
// create the object of ByteArrayInputStream class
// and initialized it with the byte array.
ByteArrayInputStream inStreambj = new ByteArrayInputStream(byteArray);
// read image from byte array
BufferedImage newImage = ImageIO.read(inStreambj);
// write output image
ImageIO.write(newImage, "jpg", new File("outputImage.jpg"));
System.out.println("Image generated from the byte array.");
}
}
Output:
Similar Reads
Java Program to Convert File to a Byte Array Here, we will go through the different ways to convert file to byte array in Java. Note: Keep a check that prior doing anything first. Create a file on the system repository to deal with our program\writing a program as we will be accessing the same directory through our programs. Methods: Using rea
3 min read
Java Program to Convert Byte Array to Long A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0. Given an array of bytes, convert it to a long value. Example: Byte Array: 1 2 3 4 Long Value: 16909060 Equivalent Hexadecimal String: 0x1020304 There are numerous approaches fo
3 min read
Java Program to Convert Byte Array to Hex String Byte Array - A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0. Hex String - A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: "245FC" is a hexadec
5 min read
Java Program to Convert Hex String to Byte Array Hex String - A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: "245FC" is a hexadecimal string. Byte Array - A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte
4 min read
Java Program to Convert Char to Byte Given a char in Java, the task is to write a Java program that converts this char into Byte. Examples: Input: ch = 'A' Output: 65 Input: ch = 'B' Output 66 In Java, char is a primitive data type and it is used to declare characters. It has the capability to hold 16-bit unsigned Unicode characters. T
3 min read
Java Program to Convert PNG Images to JPEG PNG and JPG formats are used for image illustrations. Both the formats are used to provide good compatibilities with certain types of images like PNG works better with line drawings and icon graphics whereas JPG works well with photographs. However, both are interconvertible with respect to each oth
4 min read
Java Program to Convert GIF Images to JPEG JPEG and GIF both are a type of image format to store images. JPEG uses lossy compression algorithm and the image may lose some of its data whereas GIF uses a lossless compression algorithm and no image data loss is present in GIF format. GIF images support animation and transparency. Sometimes it i
4 min read
Java Program to Convert String to Byte Array Using getBytes() Method In Java, strings are objects that are backed internally by a char array. So to convert a string to a byte array, we need a getByte() method. It is the easiest way to convert a string to a byte array. This method converts the given string to a sequence of bytes using the platform's default charset an
2 min read
Java Program to Create Grayscale Image The RGB model where red, green and blue lights are added in various different intensities and permutations and combinations of them producing broad spectrum array of colors whereas grayscale refers to the white and black colors only where the weakest intensity gives birth to black color and stronges
4 min read
How to Convert InputStream to Byte Array in Java? In Java, input stream refers to an ordered flow of data in the form of bytes. This flow of data can be coming from various resources such as files, network programs, input devices, etc. In order to read such data, we have a Java InputStream Class in the Java IO API. There are several methods to conv
5 min read