我想在三维阵列中存储图像像素,我做了如下操作:
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)
如何解决?有什么想法吗?
显然,这将是因为,
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个分量。