- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Image to Byte Array in java?
Java provides ImageIO class for reading and writing an image. To convert an image to a byte array –
- Read the image using the read() method of the ImageIO class.
- Create a ByteArrayOutputStream object.
- Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class.
- Finally convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.
Example
import java.io.ByteArrayOutputStream; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageToByteArray { 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(); } }
- Related Articles
- How to convert Byte Array to Image in java?
- How to convert BLOB to Byte Array in java?
- How to convert InputStream to byte array in Java?
- How to convert java bitmap to byte array In android?
- How to convert a PDF to byte array in Java?
- How to convert an object to byte array in java?
- How to convert hex string to byte Array in Java?
- How to convert an input stream to byte array in java?
- How to convert a byte array to hex string in Java?
- Convert byte Array to Hex String in Java
- Convert Hex String to byte Array in Java
- Java Program to convert byte[] array to String
- Java Program to convert String to byte array
- How to convert a byte array to a hex string in Java?
- Java program to convert Byte array to IP Address

Advertisements