Here you can find the source of bufferedImageToByteArray(BufferedImage bufferedImage)
Parameter | Description |
---|---|
bufferedImage | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] bufferedImageToByteArray(BufferedImage bufferedImage) throws IOException
//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; } }