get Grey Scale from BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Scale

Description

get Grey Scale from BufferedImage

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;

public class Main {
    public static int getGreyScale(BufferedImage i, int x, int y) {
        int[] rgb = getRGB(i, x, y);

        return (rgb[0] + rgb[1] + rgb[2]) / 3;
    }//  w  w  w.  ja  va 2 s .c o  m

    public static int[] getRGB(BufferedImage i, int x, int y) {
        int rgb = i.getRGB(x, y);

        int[] res = new int[3];
        res[0] = (rgb >> 16) & 255;
        res[1] = (rgb >> 8) & 255;
        res[2] = (rgb) & 255;
        return res;
    }
}

Related Tutorials