Convert IntBuffer to BufferedImage. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Convert

Description

Convert IntBuffer to BufferedImage.

Demo Code

/*/*from  www.  j av a  2  s.  c  o  m*/
 * Image conversion utilities.
 * 
 * Copyright (c) 2006 Jean-Sebastien Senecal (js@drone.ws)
 * 
 * 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 2 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, write to the Free Software Foundation, Inc., 675 Mass
 * Ave, Cambridge, MA 02139, USA.
 */
//package com.java2s;

import java.awt.image.BufferedImage;

import java.nio.IntBuffer;

public class Main {
    /**
     * Convert IntBuffer to BufferedImage. The size of IntBuffer should equal to
     * the size of BufferedImage, i.e. ib.capacity() == img.getWidth() *
     * img.getHeight()
     * 
     * @author Ying Yin
     * 
     * @param ib each 4-byte (ARGB) integer in IntBuffer corresponds to a pixel in
     *          the BufferedImage ib should have width*height of integers. ib is
     *          little endian so the byte order is BGRA. ib is a direct buffer
     *          which has no backup array, so ib.array() will fail.
     * @param bi BufferedImage should have type TYPE_INT_ARGB
     */
    public static void intBuffer2BufferedImage(IntBuffer ib,
            BufferedImage img) {
        int[] rgbArray = new int[ib.capacity()];
        ib.rewind();
        ib.get(rgbArray);
        img.setRGB(0, 0, img.getWidth(), img.getHeight(), rgbArray, 0,
                img.getWidth());
    }
}

Related Tutorials