Here you can find the source of bufferedImageToByteArray(BufferedImage image)
public static ByteBuffer bufferedImageToByteArray(BufferedImage image) throws IOException, InterruptedException
//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; } }