This question already has an answer here:
- File to byte[] in Java 21 answers
I want to convert an image to byte array and vice versa. Here, the user will enter the name of the image (.jpg
) and program will read it from the file and will convert it to a byte array.
If you are using JDK 7 you can use the following code..
BufferedImage consists of two main classes: Raster & ColorModel. Raster itself consists of two classes, DataBufferByte for image content while the other for pixel color.
if you want the data from DataBufferByte, use:
now you can process these bytes by hiding text in lsb for example, or process it the way you want.
Using
RandomAccessFile
would be simple and handy.I think the best way to do that is to first read the file into a byte array, then convert the array to an image with ImageIO.read()
java.io.FileInputStream is what you're looking for :-)