how to convert image to byte array in java?

259,843

Solution 1

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:

public byte[] extractBytes (String ImageName) throws IOException {
 // open image
 File imgPath = new File(ImageName);
 BufferedImage bufferedImage = ImageIO.read(imgPath);

 // get DataBufferBytes from Raster
 WritableRaster raster = bufferedImage .getRaster();
 DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();

 return ( data.getData() );
}

now you can process these bytes by hiding text in lsb for example, or process it the way you want.

Solution 2

If you are using JDK 7 you can use the following code..

import java.nio.file.Files;
import java.io.File;

File fi = new File("myfile.jpg");
byte[] fileContent = Files.readAllBytes(fi.toPath())

Solution 3

File fnew=new File("/tmp/rose.jpg");
BufferedImage originalImage=ImageIO.read(fnew);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos );
byte[] imageInByte=baos.toByteArray();

Solution 4

Try this code snippet

BufferedImage image = ImageIO.read(new File("filename.jpg"));

// Process image

ImageIO.write(image, "jpg", new File("output.jpg"));

Solution 5

Here is a complete version of code for doing this. I have tested it. The BufferedImage and Base64 class do the trick mainly. Also some parameter needs to be set correctly.

public class SimpleConvertImage {
    public static void main(String[] args) throws IOException{
        String dirName="C:\\";
        ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
        BufferedImage img=ImageIO.read(new File(dirName,"rose.jpg"));
        ImageIO.write(img, "jpg", baos);
        baos.flush();

        String base64String=Base64.encode(baos.toByteArray());
        baos.close();

        byte[] bytearray = Base64.decode(base64String);

        BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray));
        ImageIO.write(imag, "jpg", new File(dirName,"snap.jpg"));
    }
}

Reference link

Share:
259,843
userv
Author by

userv

Updated on July 05, 2022

Comments

  • userv
    userv almost 2 years

    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.

  • Karussell
    Karussell almost 10 years
    if the image is backen by a short array (which was the case for me and a tif image) it will give you a DataBufferShort resulting in a cast exception
  • praveen
    praveen almost 10 years
    for this classes do i need any extra jars?
  • RichardK
    RichardK about 9 years
    No, in Eclipse just use Ctrl + Shift + o.
  • BalusC
    BalusC almost 9 years
    Sorry, but this answer is nonsense. Getting bytes from an image file is not different from getting bytes from an arbitrary file. You don't need Java 2D API for this at all. This approach is clumsy and would only be necessary when you're actually interested in manipulating the image in some way (resizing, cropping, colorizing, etc). Just use Files#readAllBytes() or any other sane way you'd usually use on any arbitrary file.
  • Wajdy Essam
    Wajdy Essam over 8 years
    the context for image processing.
  • mp3por
    mp3por about 8 years
    How do you convert the bytes back to an image after I have process them ?