如何用Scala绘制位图?
我想绘制一个位图,手动指定它每个点的颜色(换句话说,任务是将 RGB 值的 2D 数组保存到 PNG(或其他一些无损真彩色位图格式)文件中)。
如果有一个函数可以在给定坐标的图像顶部打印一些文本(具有给定大小的给定字体)片段,那就太好了。
如何实施?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用Java标准库
ImageIO
类。它提供了一个静态write
方法,例如,可以将RenderedImage
编码并写入 PNG 格式的输出流。对于RenderedImage
,您可以轻松使用BufferedImage
类。它提供了一个setRGB
方法来直接操作单个像素的颜色。或者,您也可以调用 BufferedImage.getGraphics(),它返回一个 Graphics 实例,您可以在其上绘制任何类型的形状或文本,甚至整个 GUI 组件,就像任何 AWT 组件一样。这是常规的 Java 内容。 Scala 没有为此提供任何特殊的包装器,我也怀疑这是否值得付出努力。
You can use the Java standard library
ImageIO
class. It offers a staticwrite
method that can, for example, encode and write aRenderedImage
to an output stream in PNG format. For theRenderedImage
, you can easily use theBufferedImage
class. It offers asetRGB
method for directly manipulating the colors of individual pixels. Alternatively, you can also callBufferedImage.getGraphics()
, which returns an instance ofGraphics
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.
您应该使用 Java 库,例如 Java Advanced Imaging API 。这是有据可查的。
You should use a java library such as the Java Advanced Imaging API. It's well documented.