三维数组

时间:2013-02-16 07:22:11

标签: java multidimensional-array

我想将图像像素存储在三维数组中,我的确如下:

public class ImageProcessing {
    private BufferedImage image;
    int p = image.getWidth();
    int q = image.getHeight();
    int r = 3;
    private int[][][] data = new int[p][q][r];

然后我从该图像中获取像素值并将其存储到该数组中,并按如下方式构建构造函数:

public ImageProcessing(BufferedImage img){
        image = img;
        for(int x = 0; x < image.getWidth(); x++){
            for(int y = 0; y < image.getHeight(); y++){
                int px = image.getRGB(x,y);
                int alpha = (px >> 24) & 0xFF;
                int red = (px >> 16) & 0xFF;
                int green = (px >> 8) & 0xFF;
                int blue = px & 0xFF;
                data[x][y][0] = alpha;
                data[x][y][1] = red;
                data[x][y][2] = green;
                data[x][y][3] = blue;
            }
        }
    }

然后我想检索它以将图像转换为灰度,如下所示:

public BufferedImage greyscale(){   
        for(int x = 0; x < p ; x++){
            for(int y = 0; y < q ; y++){
                int avg = (data[x][y][1] + data[x][y][2] + data[x][y][3]/3);
                int grey = (data[x][y][0]<<24) + (avg<<16) + (avg<<8) + avg;
                image.setRGB(x, y, grey);
            }
        }
        return image;
    }

但是当我运行它时,我会显示如下错误我不知道它意味着什么以及如何解决它?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ImageProcessing.<init>(ImageProcessing.java:6)
    at Main$1.run(Main.java:14)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

如何解决?有什么想法吗?

1 个答案:

答案 0 :(得分:2)

显然,这将是因为,

    private BufferedImage image;       // had not initialized image
    int p = image.getWidth();          // will be null
    int q = image.getHeight();         // same here
    int r = 3;
    private int[][][] data = new int[p][q][r];

您尚未初始化BufferedImage image因此,它当前将指向null,当您初始化p = image.getWidth();int q = image.getHeight();时,则没有任何值,因此

private int[][][] data = new int[p][q][r];

以上陈述将抛出NE

您应该在Class中声明数组,并且应该在类的构造函数中初始化它,并初始化image


编辑:

查看下面的代码,初始化BufferedImage然后再做同样的事情,并且工作正常!!

public class ArrayInitialize {
    public static void main(String ar[]){
        File file = new File("Image.jpg");
        BufferedImage img = new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB);
        try {
            img = ImageIO.read(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int p = img.getWidth();          // will be null
        int q = img.getHeight();         // same here
        int r = 3;
        int count = 0;
        int[][][] data = new int[p][q][r];
        for(int i=0;i<p;i++){
            for(int j=0;j<q;j++){
                for(int k=0;k<r;k++){
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

我认为,在初始化BufferedImage后,如果您获得NullPointerException,那么您的getImage()方法存在一些问题,可能是因为它无法正常工作。

并参见评论

  

int r = 3应该是int r = 4不是吗。每种颜色都有4个组成部分。