Java BufferedImage to Byte Array getByteBuffer(BufferedImage bufferedImage)

Here you can find the source of getByteBuffer(BufferedImage bufferedImage)

Description

get Byte Buffer

License

Open Source License

Declaration

public static ByteBuffer getByteBuffer(BufferedImage bufferedImage) 

Method Source Code

//package com.java2s;

import java.awt.image.BufferedImage;

import java.awt.image.DataBufferByte;

import java.awt.image.Raster;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static ByteBuffer getByteBuffer(BufferedImage bufferedImage) {
        Raster raster = bufferedImage.getRaster();
        DataBufferByte dataBufferByte = (DataBufferByte) raster.getDataBuffer();
        byte[] data = dataBufferByte.getData();

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length);
        byteBuffer.order(ByteOrder.nativeOrder());
        byteBuffer.put(data, 0, data.length);
        byteBuffer.flip();//from   w  ww .  j  ava 2s. co m

        return byteBuffer;
    }
}

Related

  1. getByteArray(File file)
  2. getByteArray(RenderedImage image, String formatName)
  3. getByteBufferToImage(ByteBuffer buffer, int width, int height)
  4. getByteImage(BufferedImage imagem)
  5. getBytes(BufferedImage image)
  6. getBytes(final InputStream is, final int bufferSize)