Java 循环遍历图像中的像素?

发布于 2024-12-09 17:40:46 字数 214 浏览 1 评论 0原文

我正在尝试找到一种为我的 2D java 游戏制作地图的方法,我想到了一个想法,其中我将循环遍历图像的每个像素,并取决于要绘制的图块的像素是什么颜色。

例如 在此处输入图像描述

是否可以循环遍历图像像素?如果是的话,怎么样?

您能给我提供一些有用的链接或代码片段吗?

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.

e.g.
enter image description here

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-12-16 17:40:46

请注意,如果您想循环遍历图像中的所有像素,请确保在 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);
    }
}

这可能会使您的代码更快,因为您将访问将其存储在内存中的顺序。 (作为像素行。)

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:

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.)

少女情怀诗 2024-12-16 17:40:46

我认为 Pixelgrabber 就是您所寻找的。如果您对代码有疑问,请写评论。以下是 javadoc 的链接:[Pixelgrabber][1] 和另一个简短示例:[获取特定像素的颜色][2],获取像素颜色的Java程序

以下示例来自上一个链接。感谢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

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

The following example is from the last link. Thanks to 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文