I have an int[image.height][image.width] array of the image. Now I'm trying to turn it back to image file but I've failed.
This is what I have done:
private void intToImg(int[][] pxls,String path){
BufferedImage image = new BufferedImage(pxls[0].length, pxls.length, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = (WritableRaster)image.getData();
int[] pxlsr=new int[pxls[0].length*pxls.length];
int k=0;
for(int i=0;i<pxls.length;i++)for(int j=0;j<pxls[0].length;j++)pxlsr[k++]=pxls[i][j];
raster.setPixels(0,0,pxls[0].length-10,pxls.length-10,pxlsr);//index out of bounds error here...
File f = new File(path);
try{ImageIO.write(image, "JPG", f);}
catch (IOException x){x.printStackTrace();}
}
However I always have the same error java.lang.ArrayIndexOutOfBoundsException. What did I do wrong and what is the right way to transform array of pixels into real image?
I've just increased array length in 4 and java.lang.ArrayIndexOutOfBoundsException has finally disapeared. But I'm still can't create real image. Instead I have image filled with #000009 no matter what value of array.
This is what i was doing:
for(int i=0;i<pxls.length;i++)for(int j=0;j<pxls[0].length;j++){
pxlsr[k++]=pxls[i][j];
pxlsr[k++]=pxls[i][j];
pxlsr[k++]=pxls[i][j];
pxlsr[k++]=pxls[i][j];
}
raster.setPixels(0,0,pxls[0].length,pxls.length,pxlsr);
and
for(int i=0;i<pxls.length;i++)for(int j=0;j<pxls[0].length;j++){
pxlsr[k++]=111;
pxlsr[k++]=111;
pxlsr[k++]=111;
pxlsr[k++]=111;
}
raster.setPixels(0,0,pxls[0].length,pxls.length,pxlsr);
and many other thing but the result is allways the same - image filled with black color!