read Gray Scale Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage

Description

read Gray Scale Image

Demo Code


//package com.java2s;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;

public class Main {
    public static double[][] readGrayScaleImage(URL path)
            throws IOException {
        return readGrayScaleImage(path.openStream());
    }/*from   w ww .j  ava2  s.  c o m*/

    public static double[][] readGrayScaleImage(InputStream imageInputStream)
            throws IOException {
        // Converting the input image to Gray scale to prevent exceptions
        BufferedImage inputImage = ImageIO.read(imageInputStream);
        BufferedImage bufferedImage = new BufferedImage(
                inputImage.getWidth(), inputImage.getHeight(),
                BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = bufferedImage.getGraphics();
        g.drawImage(inputImage, 0, 0, null);
        g.dispose();

        double[][] finalImage = new double[bufferedImage.getHeight()][bufferedImage
                .getWidth()];

        for (int i = 0; i < finalImage.length; i++) {
            for (int j = 0; j < finalImage[0].length; j++) {
                double[] tmp = new double[1];
                bufferedImage.getRaster().getPixel(j, i, tmp);
                finalImage[i][j] = tmp[0];
            }
        }

        return finalImage;
    }
}

Related Tutorials