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开发者_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.
精彩评论