How to convert a 2d array of integers to an image in java? -
i have array of positive integers. how display image? not range of values array contains or size of array. similar question asked here. not know possible values without searching possibly large array. best way of doing regards efficiency , length of code?
here small example of data be
0 2 4 6 8 10 12 14 16 2 2 6 6 10 10 14 14 18 4 6 4 6 12 14 12 14 20 6 6 6 6 14 14 14 14 22 8 10 12 14 8 10 12 14 24 10 10 14 14 10 10 14 14 26 12 14 12 14 12 14 12 14 28 14 14 14 14 14 14 14 14 30 16 18 20 22 24 26 28 30 16 18 18 22 22 26 26 30 30 18
take here.
int xlenght = arr.length; int ylength = arr[0].length; bufferedimage b = new bufferedimage(xlenght, ylength, 3); for(int x = 0; x < xlenght; x++) { for(int y = 0; y < ylength; y++) { int rgb = (int)arr[x][y]<<16 | (int)arr[x][y] << 8 | (int)arr[x][y] b.setrgb(x, y, rgb); } } imageio.write(b, "doublearray", new file("doublearray.jpg")); system.out.println("end");
now, refactor code read proper input.
Comments
Post a Comment