Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte Array to Image object

I am given a byte[] array in Java which contains the bytes for an image, and I need to output it into an image. How would I go about doing this?

Much thanks

like image 535
Señor Reginold Francis Avatar asked Oct 16 '09 19:10

Señor Reginold Francis


People also ask

How do I print a byte array?

You can simply iterate the byte array and print the byte using System. out. println() method.

How do you create a multipart file from a byte array?

Use the default CommonsMultipartFile where you to use the FileDiskItem object to create it. Example: FileItem fileItem = new DiskFileItem("fileData", "application/pdf",true, outputFile. getName(), 100000000, new java.


2 Answers

BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes)); 
like image 121
Nick Veys Avatar answered Sep 24 '22 03:09

Nick Veys


If you know the type of image and only want to generate a file, there's no need to get a BufferedImage instance. Just write the bytes to a file with the correct extension.

try (OutputStream out = new BufferedOutputStream(new FileOutputStream(path))) {     out.write(bytes); } 
like image 30
Sam Barnum Avatar answered Sep 21 '22 03:09

Sam Barnum