简体   繁体   English

根据随机数据创建位图文件

[英]Create a bitmap file from random data

How can I convert a 2 dimensional array of random data to a bitmap? 如何将随机数据的二维数组转换为位图? What I am doing is creating a height map using the diamond square algorithm - which works just fine - but I want to be able to visualize the results for testing and debugging purposes. 我正在做的是使用菱形平方算法创建高度图-效果很好-但我希望能够可视化结果以进行测试和调试。 The best way to do this I think is by generating the results as a grayscale bitmap. 我认为最好的方法是将结果生成为灰度位图。

I have seen many examples of reading and writing bitmap data (ie reading an image to a bytebuffer and back again) but nothing that would explain how to take random data and create an image with it. 我已经看到了许多读写位图数据的示例(即,将图像读取到字节缓冲区然后再次返回),但是没有任何例子可以解释如何获取随机数据并使用它创建图像。 All I want to do is take each value of my array and convert it to a grayscale pixel. 我要做的就是获取数组的每个值并将其转换为灰度像素。

For example: 例如:

data[0][0] = 98, then pixel (0,0) would be RGB (98,98,98)
data[0][1] = 220, then pixel (0,1) would be RGB (220,220,220)

My random values are already between 0 and 255 inclusive. 我的随机值已经在0到255之间(包括0和255之间)。

Here is one way to do it that's fairly quick. 这是一种相当快的方法。 You have to flatten your data into a 1-D array 3 times as long as width*height to use this method. 要使用此方法,必须将数据扁平化为宽度*高度的3倍的一维数组。 I tested it with a 2D data array populated with Math.random() in each position 我使用在每个位置填充Math.random()的2D数据数组进行了测试

int width = data.length;
int height = data[0].length;

int[] flattenedData = new int[width*height*3];
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int ind = 0;
for (int i = 0; i < width; i++)
{
    for (int j = 0; j < height; j++)
    {
        greyShade = data[i][j];
        flattenedData[ind + j*3] = greyShade;
        flattenedData[ind + j*3+1] = greyShade;
        flattenedData[ind + j*3+2] = greyShade;

      }
    ind += height*3;
}       

img.getRaster().setPixels(0, 0, 100, 100, flattenedData);

JLabel jLabel = new JLabel(new ImageIcon(img));

JPanel jPanel = new JPanel();
jPanel.add(jLabel);
JFrame r = new JFrame();
r.add(jPanel);
r.show();

There are a couple of APIs that work with images that you may want to look at, including at least one that is part of the standard extension library ( javax.imageio ). 有两种API可以处理您可能要查看的图像,包括至少一个是标准扩展库( javax.imageio )的一部分。

Alternately, if you want to roll this by hand, the BMP file format is not terribly complicated and documentation is easy to find , and sample code is readily available . 或者,如果您想手动滚动,则BMP文件格式并不十分复杂,文档也很容易找到 ,并且示例代码也很容易获得

Use BufferedImage.setRGB(x, y, rgb) , where rgb is int that you can get by using Color class like Color(data[x][y],data[x][y],data[x][y]).getRGB() . 使用BufferedImage.setRGB(x,y,rgb) ,其中rgb是可以通过使用诸如Color(data [x] [y],data [x] [y],data [x] [y] ).getRGB() When you end filling pixels just save with ImageIO.write(bufferedImage, "bmp", file) . 当您结束填充像素时,只需保存ImageIO.write(bufferedImage,“ bmp”,file)即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM