[英]int[] to BufferedImage
I'm trying to filter a image. 我正在尝试过滤图像。 First, I put RGB values inside int[][]
and then filter. 首先,我将RGB值放入int[][]
,然后进行过滤。 In the next step I have to convert int[][]
to int[]
and finally I would want to display the new image again. 在下一步中,我必须将int[][]
转换为int[]
,最后我想再次显示新图像。 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) 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:181000 at java.awt.image.SinglePixelPackedSampleModel.setPixels(未知源)at java.awt.image.WritableRaster.setPixels(未知源)
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 [] []是使用下一个代码创建的:
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 Java中的数组基于零,因此
int[] array = {1,2,3};
will have a length of 3, but a maximum element reference of 2 and 长度为3,但最大元素引用为2和
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. 总是会溢出数组,因为while不会失败,直到count == array.length或3,并且array [3]不存在。
use while( count < array.length )
instead 使用while( count < array.length )
代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.