开发者

How to draw a bitmap with Scala?

开发者 https://www.devze.com 2023-02-05 19:47 出处:网络
I would like to draw a bitmap, manually specifying colour of every point of it (in other words, the task is to save a 2D array of RGB values to a PNG (or some other lossless true-colour bitmap format)

I would like to draw a bitmap, manually specifying colour of every point of it (in other words, the task is to save a 2D array of RGB values to a PNG (or some other lossless true-colour bitmap format) file).

It would be also nice to have a function to print some text (with a given font of a given size) pieces on top of the image at given coordinates.

How to开发者_StackOverflow implement this?


You can use the Java standard library ImageIO class. It offers a static write method that can, for example, encode and write a RenderedImage to an output stream in PNG format. For the RenderedImage, you can easily use the BufferedImage class. It offers a setRGB method for directly manipulating the colors of individual pixels. Alternatively, you can also call BufferedImage.getGraphics(), which returns an instance of Graphics that you can draw any kind of shape or text on, or even whole GUI components, just like with any AWT component.

This is regular Java stuff. Scala does not offer any special wrappers for that, and I also doubt it would be worth the effort.


You should use a java library such as the Java Advanced Imaging API. It's well documented.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号