Java Image to Byte Array bufferedImageToByteArray(BufferedImage image)

Here you can find the source of bufferedImageToByteArray(BufferedImage image)

Description

buffered Image To Byte Array

License

Apache License

Declaration

public static ByteBuffer bufferedImageToByteArray(BufferedImage image)
            throws IOException, InterruptedException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static ByteBuffer bufferedImageToByteArray(BufferedImage image)
            throws IOException, InterruptedException {
        PixelGrabber pg = new PixelGrabber(image, 0, 0, -1, -1, true);

        pg.grabPixels();//from  ww  w.  ja v  a2 s .c  o m

        int[] pixels = (int[]) pg.getPixels();

        ByteBuffer out = ByteBuffer.allocate(pixels.length * 4);

        out.order(ByteOrder.nativeOrder());
        for (int i = 0; i < pixels.length; i++) {
            out.putInt(pixels[i]);
        }
        out.flip();

        return out;
    }
}

Related

  1. bufferedImageToByte(BufferedImage image, String format)
  2. bufferedImageToByte(BufferedImage originalImage)
  3. bufferedImageToByteArray(BufferedImage bufferedImage)
  4. bufferedImageToByteArray(BufferedImage img)
  5. bufferedImageToBytes(final BufferedImage bufferedImage, final ImageWriter imageWriter, final ImageWriteParam imageWriteParameter)
  6. toByteArray(Image image)
  7. toByteArray(Image img)