Java BufferedImage to Byte Array getBytesFromBufferedImage(BufferedImage bi, String format)

Here you can find the source of getBytesFromBufferedImage(BufferedImage bi, String format)

Description

Get bytes from a specified BufferedImage, with the specified format

License

Open Source License

Parameter

Parameter Description
bi - the buffered image
format - "JPG", "PNG" or "GIF"

Declaration

public static byte[] getBytesFromBufferedImage(BufferedImage bi, String format) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.image.BufferedImage;

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

import javax.imageio.ImageIO;

public class Main {
    /**// ww w.  j a v a  2s .  com
     * Get bytes from a specified BufferedImage, with the specified format
     * 
     * @param bi
     *          - the buffered image
     * @param format
     *          - "JPG", "PNG" or "GIF"
     * @return
     */
    public static byte[] getBytesFromBufferedImage(BufferedImage bi, String format) {

        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        try {

            ImageIO.write(bi, format, buff);
            byte[] bytes = buff.toByteArray();
            buff.close();

            return bytes;

        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return null;
    }
}

Related

  1. getByteBuffer(BufferedImage bufferedImage)
  2. getByteBufferToImage(ByteBuffer buffer, int width, int height)
  3. getByteImage(BufferedImage imagem)
  4. getBytes(BufferedImage image)
  5. getBytes(final InputStream is, final int bufferSize)
  6. getBytesFromImage(String fileIn, String fileOut)
  7. getBytesPerPixel(int bufferedImageType)
  8. image2Bytes(BufferedImage image)
  9. image2Bytes(File f)