Java BufferedImage to Byte Array toByteBuffer(BufferedImage img)

Here you can find the source of toByteBuffer(BufferedImage img)

Description

Converts a BufferedImage to a ByteBuffer.

License

Open Source License

Exception

Parameter Description
IOException an exception

Declaration

public static ByteBuffer[] toByteBuffer(BufferedImage img) throws IOException 

Method Source Code


//package com.java2s;
import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.nio.ByteBuffer;

public class Main {
    /**//from w ww  .j  ava  2 s  .  c  om
     * Converts a BufferedImage to a ByteBuffer.
     * 
     * @throws IOException 
     */
    public static ByteBuffer[] toByteBuffer(BufferedImage img) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "pnm", baos);
        ByteBuffer bb = ByteBuffer.allocateDirect(1);
        bb.put(baos.toByteArray());
        bb.rewind();

        return (new ByteBuffer[] { bb });
    }

    /**
     * Converts a BufferedImage to a ByteBuffer.
     * 
     * @throws IOException 
     */
    public static byte[] toByteArray(BufferedImage img) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "pnm", baos);

        return (baos.toByteArray());
    }
}

Related

  1. toByteArray(BufferedImage image, String formatName)
  2. toByteArray(BufferedImage img, String imageFileType)
  3. toByteArray(BufferedImage org)
  4. toByteArray(final BufferedImage image, final String format)
  5. toByteArray(RenderedImage image)
  6. toBytes(final BufferedImage image)