Reputation: 762
I have an array of positive integers. How would I display this as an image? I do not already the range of values that the array contains or the size of the array. A similar question was asked here. But I do not know the possible values without searching a possibly very large array. What is the best way of doing this with regards to efficiency and length of code?
Here is a small example of what the data could 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
Upvotes: 1
Views: 2576
Reputation: 1173
Take a look 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 this code to read a proper input.
Upvotes: 3