Java BufferedImage to Byte Array getByteBufferToImage(ByteBuffer buffer, int width, int height)

Here you can find the source of getByteBufferToImage(ByteBuffer buffer, int width, int height)

Description

Converts a ByteBuffer (Used in OpenGL) to a BufferedImage.

License

Open Source License

Parameter

Parameter Description
buffer The ByteBuffer that contains the image data.
width The Width (in pixels) of the image.
height The Height (in pixels) of the image.

Return

Returns the BufferedImage that contains the data from the ByteBuffer.

Declaration

public static BufferedImage getByteBufferToImage(ByteBuffer buffer, int width, int height) 

Method Source Code

//package com.java2s;
/**/*  ww w  .j av a  2s  .c  o m*/
 *  Wrath Engine
 *  Copyright (C) 2015 Trent Spears
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.image.BufferedImage;

import java.nio.ByteBuffer;

public class Main {
    /**
     * Converts a ByteBuffer (Used in OpenGL) to a BufferedImage.
     * @param buffer The ByteBuffer that contains the image data.
     * @param width The Width (in pixels) of the image.
     * @param height The Height (in pixels) of the image.
     * @return Returns the BufferedImage that contains the data from the ByteBuffer.
     */
    public static BufferedImage getByteBufferToImage(ByteBuffer buffer, int width, int height) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        for (int x = 0; x < width; x++)
            for (int y = 0; y < height; y++) {
                int i = (x + (width * y)) * 4;
                int r = buffer.get(i) & 0xFF;
                int g = buffer.get(i + 1) & 0xFF;
                int b = buffer.get(i + 2) & 0xFF;
                img.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
            }

        return img;
    }
}

Related

  1. getByteArray(File file)
  2. getByteArray(RenderedImage image, String formatName)
  3. getByteBuffer(BufferedImage bufferedImage)
  4. getByteImage(BufferedImage imagem)
  5. getBytes(BufferedImage image)
  6. getBytes(final InputStream is, final int bufferSize)
  7. getBytesFromBufferedImage(BufferedImage bi, String format)