java - int[] to BufferedImage -
i'm trying filter image. first, put rgb values inside int[][] , filter. in next step have convert int[][] int[] , want display new image again. 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 line and error.
exception in thread "main" java.lang.arrayindexoutofboundsexception: 181000 @ java.awt.image.singlepixelpackedsamplemodel.setpixels(unknown source) @ java.awt.image.writableraster.setpixels(unknown source)
i check types, size of both arrays , don't know can do.
the first array, int[][], created next code:
int[][] imagearray = new int[height][width]; //...dar tamaƱo al array donde guardaremos la imagen (int row = 0; row < height; row++) { //en este doble bucle vamos guardando cada pixel (int col = 0; col < width; col++) { imagearray[row][col] = image.getrgb(col, row); } }
arrays in java 0 based, therefore
int[] array = {1,2,3}; will have length of 3, maximum element reference of 2 and
while( count <= array.length ) { system.out.println( array[count] ); ++count; } will overrun array because while doesn't fail until count == array.length, or 3, , array[3] doesn't exist.
use while( count < array.length ) instead
Comments
Post a Comment