[英]How to convert a 2d array of integers to an image in java?
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
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. 现在,重构此代码以读取正确的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.