get BufferedImage Grey Int Matrix - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

get BufferedImage Grey Int Matrix

Demo Code


//package com.java2s;
import java.awt.Color;
import java.awt.image.BufferedImage;

public class Main {
    public static int[][] getGreysIntMatrix(int[][] colorIntMatrix,
            int width, int height) {
        int[][] greysIntMatrix = new int[width][height];
        Color c = null;//w  w  w.  j  av a 2  s .c  om

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                c = new Color(colorIntMatrix[i][j]);
                //average of the three colors
                greysIntMatrix[i][j] = (c.getBlue() + c.getRed() + c
                        .getGreen()) / 3;
            }
        }

        return greysIntMatrix;
    }

    public static int[][] getGreysIntMatrix(BufferedImage image) {
        int height = image.getHeight();
        int width = image.getWidth();
        int[][] greysIntMatrix = new int[width][height];
        Color c = null;

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                c = new Color(image.getRGB(i, j));
                greysIntMatrix[i][j] = (c.getBlue() + c.getRed() + c
                        .getGreen()) / 3;
            }
        }

        return greysIntMatrix;
    }
}

Related Tutorials