Creating Image from Pixel Values

2019-09-21 11:12发布

问题:

In java...I have an integer array of RGB pixels, means output of my program is as follows: '

Pixel 1: 255 200 191 231
Pixel 2: 255 237 028 036
Pixel 3: 255 034 177 076
Pixel 4: 255 085 140 066
Pixel 5: 255 200 191 231
Pixel 6: 255 237 028 036
Pixel 7: 255 231 188 167
Pixel 8: 255 237 028 036
Pixel 9: 255 237 028 036
Pixel 10: 255 063 072 204
Pixel 11: 255 226 125 144
Pixel 12: 255 063 072 204
Pixel 13: 255 200 191 231
Pixel 14: 255 062 187 099
Pixel 15: 255 255 127 039
Pixel 16: 255 255 127 039
Pixel 17: 255 200 191 231
Pixel 18: 255 212 177 189
Pixel 19: 255 063 072 204
Pixel 20: 255 040 158 100
Pixel 21: 255 034 177 076
Pixel 22: 255 237 028 036
Pixel 23: 255 248 253 249
Pixel 24: 255 165 169 231
Pixel 25: 255 200 191 231

How can I create image of size 5 x 5 image using the above data...?'

回答1:

Creating a BufferedImage getting the Graphics/Graphics2D object from the BufferedImage and painting with the Graphics object the pixels.

The BufferedImage can be stored as a File with ImageIO or displayed in an Own Component/JComponent

A Bit Pseudo code

BufferedImage bi = new BufferedImage(5,5,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
while(i < 25){
  g.setColor(new Color(See Api for more details on how to create a Color);
  g.drawRect(i/5, i%5, 1,1);
}

Now you have a BufferdImage to be saved with ImageIO

ImageIO.write(bi, "png", new File(output));

Or get a New JComponent and overwrite its paint Method. There are plenty resources for this task.