How to convert BufferedImage to byte[] in Java?

Often times, you need to convert BufferedImage to byte array in order to store the image into database , or some other purpose. Some conversion is required as follow :


BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( originalImage, "jpg", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();

Example

This class will load an image from “c:\\image\\mypic.jpg”, use ImageIO.write to write the BufferedImage into ByteArrayOutputStream object and convert it to byte array.


import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/*
 * @author mkyong
 *
 */
public class ImageTest {

   public static void main(String [] args){
		
   try{
			
	BufferedImage originalImage = 
                              ImageIO.read(new File("c:\\image\\mypic.jpg"));
			
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ImageIO.write( originalImage, "jpg", baos );
	baos.flush();
	byte[] imageInByte = baos.toByteArray();
	baos.close();
			
	}catch(IOException e){
		System.out.println(e.getMessage());
	}		
   }	
}

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
sankar ece
Guest
sankar ece

how can i retrive the image from the byte array…

Mircea
Guest
Mircea

Thanks, man! You’re helping us so much. Thank you!

Anand
Guest
Anand

Thanks a ton mkyong .

jjv
Guest
jjv

Hi,

How to view the output of this program.

How to view the Converted byte array as the output??????

Dhinesh waran
Guest
Dhinesh waran

I need to convert a blob type image to black and white.
if (rs.next()) {
blob = rs.getBlob(“value”);
}
int blobLength = (int) blob.length();

byte[] blobAsBytes = blob.getBytes(1, blobLength);
InputStream in = new ByteArrayInputStream(bytes);

final BufferedImage bufferedImage = ImageIO.read(in);

But i am getting java.lang.NullPointerException
where i am doing mistake?

Manish Bhardwaj
Guest
Manish Bhardwaj

.png image is fading.when i again convert it from byte to image format

puja
Guest
puja

will it work for a BMP type image too?

Saravanan
Guest
Saravanan

Sir, Its working fine. But still I am getting lossy JPEG Image that means, my 17.6 MB file become 5.4MB size. How do I Get lossless JPEG and my input also is a Image not the File so How can I get the byte[] for Image(BufferedImage) ..?

VJ
Guest
VJ

this writes the file to desk and then converts to byte[]. This is a costly approach

the following does the conversion without writing to disk

byte[] byteArray = ((DataBufferByte) bufferedImage.getData().getDataBuffer()).getData();

Pramod Pandey
Guest
Pramod Pandey

its not working. I am getting an exception – java.lang.ClassCastException: java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte. I’d appreciate if you could help.

Anand
Guest
Anand

Tnanks a ton mkyong .

Sreelal
Guest
Sreelal

Thank you sir, Was useful to me.

friend
Guest
friend

thanks for the post, very good

Haseeb Wali
Guest
Haseeb Wali

Thank you sir you made my day…pretty short and straight forward solution for resizing image..!!!

Showtime
Guest
Showtime

Thanks

Aars
Guest
Aars

how to view the output ?? pls reply

banging girls day and night
Guest
banging girls day and night

Such a nice tut

cheers

Leonarda Chaloux
Guest
Leonarda Chaloux

Great info, thanks for the post!