簡體   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