I'm trying to filter a image. First, I put RGB values inside int[][]
and then filter. In the next step I have to convert int[][]
to int[]
and finally I would want to display the new image again. This is my code:
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
And this is my error.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 181000 at java.awt.image.SinglePixelPackedSampleModel.setPixels(Unknown Source) at java.awt.image.WritableRaster.setPixels(Unknown Source)
I check that the types, the size of both arrays and I don't know what can I do.
The first array, int[][], is created with the next code:
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);
}
}
Arrays in Java are zero based, therefore
int[] array = {1,2,3};
will have a length of 3, but a maximum element reference of 2 and
while( count <= array.length ) {
System.out.println( array[count] );
++count;
}
will always overrun the array because the while doesn't fail until count is == array.length, or 3, and array[3] doesn't exist.
use while( count < array.length )
instead
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.