Java Utililty Methods BufferedImage to Byte Array

List of utility methods to do BufferedImage to Byte Array

Description

The list of methods to do BufferedImage to Byte Array are organized into topic(s).

Method

byte[]toByteArray(BufferedImage image)
to Byte Array
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "png", outputStream);
return outputStream.toByteArray();
byte[]toByteArray(BufferedImage image, float quality)
to Byte Array
try {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    if (quality == -1)
        ImageIO.write(image, "jpeg", out); 
    else
        write(image, quality, out); 
    return out.toByteArray();
} catch (IOException e) {
...
byte[]toByteArray(BufferedImage image, String extension)
Saves image into byte array.
if (image != null) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {
        ImageIO.write(image, extension, baos);
    } catch (IOException e) {
        throw new IllegalStateException(e.toString());
    byte[] b = baos.toByteArray();
...
byte[]toByteArray(BufferedImage image, String formatName)
to Byte Array
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, formatName, os);
byte[] data = os.toByteArray();
os.close();
return data;
byte[]toByteArray(BufferedImage img, String imageFileType)
to Byte Array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
    ImageIO.write(img, imageFileType, baos);
    return baos.toByteArray();
} catch (Throwable e) {
    throw new RuntimeException();
byte[]toByteArray(BufferedImage org)
Converts a BufferedImage to a byte array.
if (org != null) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(org, "jpg", baos);
    baos.flush();
    byte[] imageInByte = baos.toByteArray();
    baos.close();
    return imageInByte;
return null;
byte[]toByteArray(final BufferedImage image, final String format)
Converts an image to a byte array.
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
    ImageIO.write(image, "PNG", out);
} catch (final IOException ex) {
    ex.printStackTrace();
final byte[] data = out.toByteArray();
return data;
...
byte[]toByteArray(RenderedImage image)
Convert a RenderedImage to a byte array
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
    ImageIO.write(image, "JPG", out);
} catch (IOException e) {
    e.printStackTrace();
    System.err.println("Failed to convert image to byte array");
return out.toByteArray();
...
ByteBuffer[]toByteBuffer(BufferedImage img)
Converts a BufferedImage to a ByteBuffer.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "pnm", baos);
ByteBuffer bb = ByteBuffer.allocateDirect(1);
bb.put(baos.toByteArray());
bb.rewind();
return (new ByteBuffer[] { bb });
byte[]toBytes(final BufferedImage image)
Converts an image to a byte array
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();