java Java循环遍历图像中的像素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7749895/
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
Java Loop through pixels in an image?
提问by Duncan Palmer
I'm trying to find a way to make maps for my 2D java game, and I thought of one Idea in which I would loop through each pixel of an image and depends on what color the pixel it was that would be the tile to draw.
我试图找到一种方法来为我的 2D java 游戏制作地图,我想到了一个想法,我将循环遍历图像的每个像素,并取决于像素的颜色作为要绘制的图块.
e.g.
例如
Is it possible to loop through an Images pixels? If it is, how?
是否可以遍历图像像素?如果是,如何?
Could you please supply me with some helpful links or code snippets?
你能给我提供一些有用的链接或代码片段吗?
回答by Zarkonnen
Note that if you want to loop over all pixels in an image, make sure to make the outer loop over the y-coordinate, like so:
请注意,如果要遍历图像中的所有像素,请确保在 y 坐标上进行外部循环,如下所示:
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int clr = image.getRGB(x, y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
image.setRGB(x, y, clr);
}
}
This will likely make your code much faster, as you'll be accessing the image data in the order it's stored in memory. (As rows of pixels.)
这可能会使您的代码更快,因为您将按照其存储在内存中的顺序访问图像数据。(作为像素行。)
回答by Tobias Sarnow
I think Pixelgrabber is what you looking for. If you have problems with the code please write a comment. Here is the link to the javadoc: [Pixelgrabber][1] and another short examples: [Get the color of a specific pixel][2], Java program to get the color of pixel
我认为 Pixelgrabber 正是您要找的。如果您对代码有疑问,请写评论。这是 javadoc 的链接:[Pixelgrabber][1] 和另一个简短的例子:[获取特定像素的颜色][2],获取像素颜色的 Java 程序
The following example is from the last link. Thanks to roseindia.net
以下示例来自最后一个链接。感谢 roseindia.net
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageTest
{
public static void main(final String args[])
throws IOException
{
final File file = new File("c:\example.bmp");
final BufferedImage image = ImageIO.read(file);
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
final int clr = image.getRGB(x, y);
final int red = (clr & 0x00ff0000) >> 16;
final int green = (clr & 0x0000ff00) >> 8;
final int blue = clr & 0x000000ff;
// Color Red get cordinates
if (red == 255) {
System.out.println(String.format("Coordinate %d %d", x, y));
} else {
System.out.println("Red Color value = " + red);
System.out.println("Green Color value = " + green);
System.out.println("Blue Color value = " + blue);
}
}
}
}
}
[1]: https://docs.oracle.com/javase/7/docs/api/java/awt/image/PixelGrabber.html[2]: http://www.rgagnon.com/javadetails/java-0257.html
[1]:https: //docs.oracle.com/javase/7/docs/api/java/awt/image/PixelGrabber.html[2]:http: //www.rgagnon.com/javadetails/java-0257 .html