create image in green RGB channel - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

create image in green RGB channel

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{
    /**/*from w ww .j  a v a 2  s. c o  m*/
     * create image in green RGB channel
     * 
     * @param width
     * @param height
     * @param colorModel
     * @return
     */
    public static BufferedImage createGreenImage(final int width,
            final int height, final ColorModel colorModel,
            final int[] pixels) {
        int[] greenPixels = getGreenPixels(width, height, colorModel,
                pixels);
        return createImage(greenPixels, width, height);
    }
    public static int[] getGreenPixels(final int width, final int height,
            final ColorModel colorModel, final int[] pixels) {
        int[] result = new int[height * width];
        for (int i = 0; i < result.length; i++) {
            result[i] = getGreen(pixels[i], colorModel);
        }
        return result;
    }
    /**
     * 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);
    }
    /**
     * return the green component of the pixel of the image in java representation than can be summed up with other components to get the summed RGB valued
     * 
     * @param pixel
     *            the pixel number
     * @param colorModel
     * @return
     */
    public static int getGreen(final int pixel, final ColorModel colorModel) {
        return colorModel.getGreen(pixel);//<<8;
    }
}

Related Tutorials