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);
	}

}

Sorry for the duplicate threads. Dont know how to delete this thread.

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.