How to Convert BufferedImage to byte[] in Java? | Code Factory

Code Factory
1 min readApr 4, 2020

Reference Link : Link

Donate : Link

BufferedImageToByte.java

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;/**
* @author code_factory
*/
public class BufferedImageToByte {
public static void main(String args[]) {
try {
BufferedImage image = ImageIO.read(new File("R:\\code_factory\\image.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
baos.flush();
byte[] byteArray = baos.toByteArray();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

--

--