covert Image To Gray Matrix - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

covert Image To Gray Matrix

Demo Code


//package com.java2s;

import java.awt.*;
import java.awt.image.BufferedImage;

public class Main {
    private static final int IMG_WIDTH = 64;
    private static final int IMG_HEIGHT = 64;

    public static int[][] covertImageToGrayMatrix(BufferedImage original) {

        int alpha, red, green, blue;
        int newPixel;

        int[][] result = new int[IMG_WIDTH][IMG_HEIGHT];

        for (int i = 0; i < original.getWidth(); i++) {
            for (int j = 0; j < original.getHeight(); j++) {
                alpha = new Color(original.getRGB(i, j)).getAlpha();
                red = new Color(original.getRGB(i, j)).getRed();
                green = new Color(original.getRGB(i, j)).getGreen();
                blue = new Color(original.getRGB(i, j)).getBlue();

                red = (int) (0.299 * red + 0.587 * green + 0.114 * blue);

                result[i][j] = red;//from  w w  w.  j ava 2  s.co m
            }
        }

        return result;
    }
}

Related Tutorials