Java 有 Bitmap 类吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25714003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Java have a Bitmap class?
提问by KDecker
I have been searching google for stuff like "Java Bitmap", "Create Java Bitmap", etc and cannot seem to find much information. From all of the code examples it looks like all of the bitmap libraries are third party or for android.
我一直在谷歌上搜索“Java 位图”、“创建 Java 位图”等内容,但似乎找不到太多信息。从所有代码示例看来,所有位图库都是第三方的或适用于 android 的。
What I want to do is very simple. I want to create a small bitmap maybe 10x80, and be able to color each pixel at (x,y). I want to make a small I guess color bar that will show that position of items in a queue by color.
我想做的很简单。我想创建一个可能为 10x80 的小位图,并且能够为 (x,y) 处的每个像素着色。我想制作一个小的我猜颜色栏,它将按颜色显示队列中项目的位置。
Are there any build in libraries to do this?
是否有任何内置库来执行此操作?
采纳答案by khelwood
There's the java.awt.image.BufferedImage
class. This has pixel-specific get/set methods.
http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html
有java.awt.image.BufferedImage
课。这具有特定于像素的 get/set 方法。
http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html
回答by Eben
Here is an example of creating and writing to pixels with BufferedImage:
以下是使用 BufferedImage 创建和写入像素的示例:
BufferedImage image = new BufferedImage(10, 80, BufferedImage.TYPE_4BYTE_ABGR);
image.setRGB(5, 20, Color.BLUE.getRGB());
The third parameter on the image is the type, which may vary based on your use, but for basic solid colors the one shown is good. The setRGB uses an int representation of the color so you can't just use a Color constant directly.
图像上的第三个参数是类型,它可能会因您的使用而异,但对于基本纯色,显示的参数很好。setRGB 使用颜色的 int 表示,因此您不能直接使用 Color 常量。
You probably don't want to use VolatileImage because the benefits are dubious in current Java implementations. See this stackoverflow questionfor why it may not help.
您可能不想使用 VolatileImage,因为它的好处在当前的 Java 实现中是可疑的。请参阅此 stackoverflow 问题,了解为什么它可能无济于事。
Look at the Oracle image turorialfor help with this. It explains both your options and how to interact with them.
查看Oracle image turorial寻求帮助。它解释了您的选择以及如何与它们交互。