Hi,

I am using a code to read a grayscale image and store the pixel value in a 1D array. But the code is taking a default image(though i am providing the complete path for the image to be loaded) and the height and the width are returned as -1. Also tell me how to get the pixel value and how to store in 1D array.

import java.awt.Image;
import java.awt.*;
import java.awt.image.PixelGrabber;


class ImagetoPixel extends Frame
{ 
        String imagePath;
	int width, height;
	Image image = null;
	PixelGrabber grabber;
	int []pixels;
	MediaTracker tracker;

	public static void main(String args[])
	{
		ImagetoPixel obj = new ImagetoPixel();
		obj.testFunction();
	}

	void testFunction()
	{
		System.out.println("The function is to use object");
		String path = System.getProperty("user.dir");
		image = Toolkit.getDefaultToolkit().getImage(path + "/DSC00030 - Copy.jpg"); );
		if(image ==null)
		{
			System.out.println("No Image");
		}
		else
		{
		}

		System.out.println(image);
		width = image.getWidth(this);
		height = image.getHeight(this);
		grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
		System.out.println("width = " +width);
		System.out.println("height = "+ height);
	}

}

This is the output of this program.

run:
The function is to use object
sun.awt.image.ToolkitImage@89ae9e
width = -1
height = -1
BUILD SUCCESSFUL (total time: 1 second)

Use javax.imageio.ImageIO.read( function,

...
        java.lang.String s1 = "IMAGES/Question.gif";
        URL url = getClass().getClassLoader().getResource(s1);
        try {
            image = javax.imageio.ImageIO.read(url);
        } catch (IOException ex) {
            System.out.println("IOException No Image");
        }
...

run:
The function is to use object
BufferedImage@18fef3d: type = 12 IndexColorModel: #pixelBits = 4 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@a3bcc1 transparency = 2 transIndex = 1 has alpha = true isAlphaPre = false BytePackedRaster: width = 32 height = 32 #channels 1 xOff = 0 yOff = 0
width = 32
height = 32

I tried my best to edit the original coding to the quuba's code but I still didn't manage to get the output that quuba already mention. Anyone please help me?

package javapixelgrabber;

import java.awt.Image;
import java.awt.*;
import java.awt.image.PixelGrabber;
import java.net.URL;
import java.io.IOException;

class ImagetoPixel extends Frame {

    String imagePath;
    int width, height;
    Image image = null;
    PixelGrabber grabber;
    int[] pixels;
    MediaTracker tracker;

    public static void main(String args[]) {
        ImagetoPixel obj = new ImagetoPixel();
        obj.testFunction();
    }

    void testFunction() {
        System.out.println("The function is to use object");

        String s1 = "gmbr.gif";
        URL input = getClass().getClassLoader().getResource(s1);
        try {
            image = javax.imageio.ImageIO.read(input);
        } catch (IOException ex) {
            System.out.println(ex);
        }

        System.out.println(image);
        width = image.getWidth(this);
        height = image.getHeight(this);
        grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
        System.out.println("width = " + width);
        System.out.println("height = " + height);
    }
}

Below is the output by running this code:

run:
The function is to use object
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
        at javax.imageio.ImageIO.read(ImageIO.java:1362)
        at javapixelgrabber.ImagetoPixel.testFunction(ImagetoPixel.java:29)
        at javapixelgrabber.ImagetoPixel.main(ImagetoPixel.java:20)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

*gmbr.gif reside with the same folder as 'imagetopixel.java'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.