0

I am facing the following problems:

  • Array values are lost after looping through the for-loop
  • I am unable to display the image using createImage()
  • I am getting a NullPointerException

Here's my code:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.awt.Color;
import java.awt.image.MemoryImageSource;
import java.awt.Image;
import java.awt.Component;

class JpegData{
    public static void main(String args[]) {
        try {
            File imgfile=new File("E:/Problems/","Lee.jpg");
            Component comp = null;
            int[] array = new int[1000];
            BufferedImage img = ImageIO.read(imgfile); // ImageIO is final so no need to instatiate it
            System.out.println("Height:" + img.getHeight());
            System.out.println("Width:" + img.getWidth());

            for (int height = 500; height < 550; height++) {
                for (int width = 660; width < 680; width++) {
                    int pixel = img.getWidth()*(height-1)+(width);
                    int a = 0;
                    array[a] = img.getRGB(width, height);   

                    Color clr = new Color(img.getRGB(width, height));
                    System.out.print("Pixel-" + pixel + ": "+"R:" + clr.getRed() + " G:" + clr.getGreen() + " B:" + clr.getBlue());
                    System.out.println("at pixel:" + pixel + "rgb:" + img.getRGB(width,height));
                    a++;
                }
            }

            for (int i=0; i < array.length; i++) System.out.println(array[i]);
            Image dis = comp.createImage(new MemoryImageSource(20, 50, array, 0, 1)); 

        } catch (IOException e) {
            System.out.println("Error.Input Output Exception occured.");
        }   
    }
}

0 Answers0