繁体   English   中英

如何获取Java小程序中像素的颜色以生成图?

[英]How to get the color of a pixel in a Java applet to produce a map?

我正在使用Java创建一个小applet。 我很想知道是否有一种方法可以“扫描”图像以获取某个像素的颜色值。 我希望不必在屏幕上显示图像,但是如果您发现这是唯一的方法,请告诉我。 理想情况下,我希望能够使applet扫描图像文件并根据图像在屏幕上创建图像。 请尽量使答案简单一点,因为我仍然习惯所有技术术语。

谢谢,〜Rane

到目前为止,我有:

import java.applet.Applet;

public class LoadGuideImage {

Applet applet;

public LoadGuideImage(Applet applet){
    this.applet = applet;
}

public String getPixelColor(String pathToImage, int Xpix, int Ypix){
    int redC = 0;
    int greenC = 0;
    int blueC = 0;

    //Get Pixel colors here and save to ints

    return redC + " " + greenC + " " + blueC;
}

}

您是否在建议类似“另一个人”的内容?:

        BufferedImage img = (BufferedImage) getImage(pathToImage);

    System.out.println("Color: " + img.getRGB(3, 3));

getImage方法:

    public Image getImage(String path) {

    Image img;
    URL url = null;

    try {
        url = applet.getDocumentBase();
    } catch (Exception e){
        // TODO: handle exception
    }
    img = applet.getImage(url, path);

    return img;
}

顺便说一句好名字。 所以我前一阵子处于同一位置。

将其用于您的获取图像方法,只需对其进行调整并使其受益即可:

public class ImageLoader {

    public BufferedImage load(String path){
        try {
            return ImageIO.read(getClass().getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

我一直在制作一个游戏,该游戏使用单个像素加载这些像素,然后将图块放置在地图中某些像素所在的位置。 如果您想从那里拿走我的代码段,请告诉我,让您不满意:P

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM