我試圖過濾圖像。首先,我將RGB值放在int[][]
之內,然後進行過濾。 在接下來的步驟中,我必須將int[][]
轉換爲int[]
,最後我希望再次顯示新圖像。這是我的代碼:int []到BufferedImage
int row,col,count=0;
int[] pixels = new int[width*height];
while(count!=(pixels.length)){
for(row=0;row<height;row++){
for(col=0;col<width;col++){
pixels[count] = imageArray[row][col];
count++;
}
}
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0,0,width,height,pixels); //The problem appear in this line
這是我的錯誤。
異常在線程 「主」 java.lang.ArrayIndexOutOfBoundsException:181000 在java.awt.image.SinglePixelPackedSampleModel.setPixels(未知來源) 在java.awt.image.WritableRaster.setPixels(未知來源)
我檢查兩個數組的類型和大小,我不知道我能做什麼。
第一陣列,INT [] [],與下一個代碼創建的:
int[][] imageArray = new int[height][width]; //...dar tamaño al array donde guardaremos la imagen
for (int row = 0; row < height; row++) { //en este doble bucle vamos guardando cada pixel
for (int col = 0; col < width; col++) {
imageArray[row][col] = image.getRGB(col, row);
}
}
改變數組聲明行此'INT []個像素=新INT [(寬度+ 1)*(高度+ 1)];',看看它是否工作。 – 2013-04-06 02:07:23
文檔[http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/image/WritableRaster.html#setPixels(int,int,int,int,int [])]列表以下**兩個**這個異常的原因:**如果座標不在邊界內,或者輸入的int像素數組太小而無法保存輸入** – 2013-04-06 02:17:18
謝謝,但我剛剛嘗試了您的想法,它仍然有同樣的問題。 – Enery93 2013-04-06 23:48:48