create RGB Image From Channels - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

create RGB Image From Channels

Demo Code


import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.log4j.Logger;

public class Main{
    public static BufferedImage createRGBImageFrom3Channels(
            final int width, final int height, final BufferedImage red,
            final BufferedImage green, final BufferedImage blue) {
        int[] redPixels = grabPixels(red);
        int[] greenPixels = grabPixels(green);
        int[] bluePixels = grabPixels(blue);
        return createRGBImageFrom3Channels(width, height, redPixels,
                greenPixels, bluePixels);
    }/*from   w w  w .  j  av  a 2 s .  c o m*/
    public static BufferedImage createRGBImageFrom3Channels(
            final int width, final int height, final int[] redPixels,
            final int[] greenPixels, final int[] bluePixels) {
        int[] sumPixels = rgbToOneDImensionalArray(redPixels, greenPixels,
                bluePixels);
        return createImage(sumPixels, width, height);
    }
    public static int[] grabPixels(BufferedImage image, boolean forceRGB) {
        PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0,
                image.getWidth(), image.getHeight(), forceRGB);
        pixelGrabber.startGrabbing();
        int[] pixels = (int[]) pixelGrabber.getPixels();
        return pixels;
    }
    public static int[] grabPixels(BufferedImage image) {
        return grabPixels(image, true);
    }
    public static int[] rgbToOneDImensionalArray(final int[] redPixels,
            final int[] greenPixels, final int[] bluePixels) {
        int[] sumPixels = new int[redPixels.length];
        for (int i = 0; i < redPixels.length; i++) {
            int red = redPixels[i] << 16;
            int green = greenPixels[i] << 8;
            int blue = bluePixels[i];
            sumPixels[i] = red + green + blue;
        }
        return sumPixels;
    }
    /**
     * creates image from pixels
     * 
     * @param pixels
     * @param width
     * @param height
     * @return
     */
    public static BufferedImage createImage(final int[] pixels,
            final int width, final int height, int type) {
        BufferedImage bufImg = new BufferedImage(width, height, type);
        bufImg.setRGB(0, 0, width, height, pixels, 0, width);
        return bufImg;
    }
    /**
     * creates image from pixels
     * 
     * @param pixels
     * @param width
     * @param height
     * @return
     */
    public static BufferedImage createImage(final int[] pixels,
            final int width, final int height) {
        return createImage(pixels, width, height,
                BufferedImage.TYPE_INT_RGB);
    }
}

Related Tutorials