Javatpoint Logo
Javatpoint Logo

Convert Byte to an Image in Java

A byte array is a fundamental data structure used to store binary data, making it a versatile tool for various tasks. One common use case is to store images in a byte array. In this section, we will explore how to convert a byte array into an image in Java. We will cover the entire process, from reading an image file and generating the byte array to creating a new image file from the byte array.

Converting Byte Array to Image

If we have image information stored as a byte array, you can reconstruct the image from that array. Java provides a set of classes and methods to facilitate this process, particularly in the javax.imageio package.

Reading the Image File and Creating the Byte Array

  1. Read the image from a file using ImageIO.read(new File("Image path")).
  2. Create an instance of the ByteArrayOutputStream class to store the image data.
  3. Write the image into the ByteArrayOutputStream using ImageIO.write(image, "jpg", outStreamObj).
  4. Convert the image into a byte array using byteArray = outStreamObj.toByteArray().

Generating a New Image File from the Byte Array

  1. Create an instance of the ByteArrayInputStream class and initialize it with the byte array.
  2. Read the image from the ByteArrayInputStream using ImageIO.read(inStreambj).
  3. Create a new file and write the image into it using ImageIO.write(newImage, "jpg", new File("outputImage.jpg")).

Method 1: Using ImageIO

The method uses the ImageIO class from the Java standard library to read and write images. Here's the complete code:

ByteToImageConverter.java

Output:

Convert Byte to an Image in Java
Convert Byte to an Image in Java

Explanation

In Method 1, we use Java's built-in ImageIO class to convert a byte array to an image. The approach is based on a combination of ImageIO methods and ByteArrayInputStream and ByteArrayOutputStream. Here's how it works:

We start by reading an image from a file using ImageIO.read(new File("image.jpg")). This method loads the image into a BufferedImage.

To convert this BufferedImage into a byte array, we create a ByteArrayOutputStream. This stream acts as a temporary storage for image data.Next, we use ImageIO.write(image, "jpg", outStreamObj) to write the image data into the ByteArrayOutputStream. This process encodes the image into a byte array.We obtain the byte array by calling outStreamObj.toByteArray(). Now, the image is represented as a byte array.To recreate an image from the byte array, we create a ByteArrayInputStream initialized with the byte array. This stream allows us to read the image data.We use ImageIO.read(inStreambj) to read the image from the ByteArrayInputStream, resulting in a new BufferedImage.

Finally, we write the new BufferedImage to an output file using ImageIO.write(newImage, "jpg", new File("outputImageMethod1.jpg")). This step generates a new image file from the byte array.

Method 2: Using javax.imageio.ImageReader and ImageWriter

The method uses specific ImageReader and ImageWriter classes to read and write images. Here's the complete code:

ByteToImage.java

Output:

Convert Byte to an Image in Java
Convert Byte to an Image in Java

Explanation

Method 2 also leverages Java's javax.imageio package. Instead of directly using ImageIO, it employs specific ImageReader and ImageWriter classes. The process involves the following steps:

We read the image from a file using ImageIO.getImageReadersByFormatName("jpg").next(). This creates an ImageReader that understands the image format. We then read the image into a BufferedImage.

To convert the BufferedImage into a byte array, we create an ImageOutputStream. We associate this output stream with a ByteArrayOutputStream to store the image data. We use the ImageWriter to write the image data into the ImageOutputStream.

We retrieve the byte array from the ByteArrayOutputStream. To recreate the image from the byte array, we create a ByteArrayInputStream and initialize it with the byte array.

We read the image data from the ByteArrayInputStream using a new ImageReader, resulting in a new BufferedImage. Finally, we write the new BufferedImage to an output file, creating a new image file from the byte array.

Conclusion

Converting a byte array to an image in Java is a valuable skill for various applications. With the help of the javax.imageio package, we can efficiently transform image data into byte arrays and vice versa. The section provides a step-by-step guide and a complete Java program to perform this conversion successfully.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA