Java BufferedImage to Byte Array imageToBytes(BufferedImage image, String encoding)

Here you can find the source of imageToBytes(BufferedImage image, String encoding)

Description

Converts an image to byte buffer representing PNG (bytes as they would exist on disk)

License

Apache License

Parameter

Parameter Description
image a parameter
encoding the encoding to be used, one of: png, jpeg, bmp, wbmp, gif

Exception

Parameter Description
IOException if the bytes[] could not be written

Return

byte[] representing the image

Declaration

public static byte[] imageToBytes(BufferedImage image, String encoding) throws IOException 

Method Source Code

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

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {
    /**/*from   w w  w  .ja  v  a  2s  .  c o  m*/
     * Converts an image to byte buffer representing PNG (bytes as they would exist on disk)
     * @param image
     * @param encoding the encoding to be used, one of: png, jpeg, bmp, wbmp, gif
     * @return byte[] representing the image
     * @throws IOException if the bytes[] could not be written
     */
    public static byte[] imageToBytes(BufferedImage image, String encoding) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, encoding, baos);
        return baos.toByteArray();
    }
}

Related

  1. image2Bytes(File f)
  2. imageToByte(BufferedImage bi, String format)
  3. imageToByte(BufferedImage image)
  4. imageToByte(BufferedImage img)
  5. imageToByteArray(BufferedImage o)
  6. imageToBytes(BufferedImage image, String imageFormat)
  7. imageToBytes(BufferedImage img, String formatName)
  8. toArrayByte(BufferedImage image)
  9. toByteArray(BufferedImage image)