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

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();
}
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Meaning of Hue in the Angular Palette Context

Introducing Gaum: An Open Source O/RM That isn’t an O/RM

鬼灭之刃剧场版:无限列车篇2020 在线流高清[1080P]

Create your own modules in Python

Go Interfaces, can be a type of its own….

The Request Sent Bad Data; What’s the Response?

Google Interview Experience

Python Morsel — count_calls

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Method Overloading and Method Overriding in Java

Singleton Design Pattern in Java

Singleton Design Pattern

Functional Interfaces in Java