Java Image to Byte Array bufferedImageToByteArray(BufferedImage bufferedImage)

Here you can find the source of bufferedImageToByteArray(BufferedImage bufferedImage)

Description

Convert BufferedImage to byte array

License

LGPL

Parameter

Parameter Description
bufferedImage a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static byte[] bufferedImageToByteArray(BufferedImage bufferedImage) throws IOException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    private static final String IMAGE_FORMAT = "png";

    /**//  w w w . j  av  a2s  .  c o  m
     * Convert {@link BufferedImage} to byte array
     * 
     * @param bufferedImage
     * @return
     * @throws IOException
     */
    public static byte[] bufferedImageToByteArray(BufferedImage bufferedImage) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, IMAGE_FORMAT, baos);
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();
        return imageInByte;
    }
}

Related

  1. bufferedImageToByte(BufferedImage image, String format)
  2. bufferedImageToByte(BufferedImage originalImage)
  3. bufferedImageToByteArray(BufferedImage image)
  4. bufferedImageToByteArray(BufferedImage img)
  5. bufferedImageToBytes(final BufferedImage bufferedImage, final ImageWriter imageWriter, final ImageWriteParam imageWriteParameter)
  6. toByteArray(Image image)