 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert Byte Array to Image in java?
Java provides ImageIO class for reading and writing an image. To convert a byte array to an image.
- Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. 
- Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter). 
- Finally, Write the image to using the write() method of the ImageIo class. 
Example
import java.io.ByteArrayOutputStream; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ByteArrayToImage { public static void main(String args[]) throws Exception { BufferedImage bImage = ImageIO.read(new File("sample.jpg")); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(bImage, "jpg", bos ); byte [] data = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(data); BufferedImage bImage2 = ImageIO.read(bis); ImageIO.write(bImage2, "jpg", new File("output.jpg") ); System.out.println("image created"); } }  Output
image created

Advertisements
 