为什么我在for循环后丢失了数组值?

时间:2016-10-04 13:32:17

标签: java image

我面临以下问题:

  • 循环遍历for - 循环
  • 后,数组值会丢失
  • 我无法使用createImage()
  • 显示图片
  • 我收到NullPointerException

这是我的代码:

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 个答案:

没有答案