Convert Image to Byte Array in Java

In this tutorial, we will introduce the way to convert an image to byte array using java.

1. Import some packages

import java.io.*;

import java.awt.image.*;

import javax.imageio.*;

2. Read an image using java

BufferedImage bufferimage = ImageIO.read(new File("image.jpg"));

We will read the data of an image “image.jpg”.

3. Convert image data to byte array

ByteArrayOutputStream output = new ByteArrayOutputStream();

ImageIO.write(bufferimage, "jpg", output );

byte [] data = output.toByteArray();