Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a bitmap with Scala?

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 implement this?

like image 225
Ivan Avatar asked Jan 20 '23 17:01

Ivan


2 Answers

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.

like image 199
Madoc Avatar answered Jan 23 '23 07:01

Madoc


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

like image 33
Kim Stebel Avatar answered Jan 23 '23 06:01

Kim Stebel