Here you can find the source of imageToBytes(BufferedImage image, String encoding)
Parameter | Description |
---|---|
image | a parameter |
encoding | the encoding to be used, one of: png, jpeg, bmp, wbmp, gif |
Parameter | Description |
---|---|
IOException | if the bytes[] could not be written |
public static byte[] imageToBytes(BufferedImage image, String encoding) throws IOException
//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(); } }