0

Java で Color メソッドを使用して画像からピクセルを抽出するプログラムに取り組んでいます。ただし、それはそれほど関係ありません。

X 変数と Y 変数をそれぞれ画像の幅と高さまでインクリメントする必要があります。for ループをネストしようとしましたが、他のループの条件が満たされるとすぐに他のループがトリガーされます。

コード

    for (int x = 0; x<ScreenWidth; x++)
    {
        for (int y = 0; y<ScreenHeight; y++)
        {

        Color c = new Color(DesktopCapture.getRGB(x,y));
        int r = c.getRed();
        int g = c.getGreen();
        int b = c.getBlue();
        System.out.println("Colour at location of screen is " + r + " " + g + " " + b + " Position is " + x + " " + y);

        Thread.sleep(40);

出力

Colour at location of screen is 151 184 216 Position is 0 0
Colour at location of screen is 151 186 218 Position is 0 1
Colour at location of screen is 151 188 220 Position is 0 2
Colour at location of screen is 151 190 222 Position is 0 3
Colour at location of screen is 152 192 224 Position is 0 4
Colour at location of screen is 152 194 226 Position is 0 5
Colour at location of screen is 152 195 227 Position is 0 6
Colour at location of screen is 153 196 228 Position is 0 7
Colour at location of screen is 153 197 229 Position is 0 8
Colour at location of screen is 154 197 229 Position is 0 9
Colour at location of screen is 154 196 228 Position is 0 10
Colour at location of screen is 154 195 227 Position is 0 11
Colour at location of screen is 154 194 225 Position is 0 12
Colour at location of screen is 154 192 223 Position is 0 13
Colour at location of screen is 154 190 221 Position is 0 14
Colour at location of screen is 154 188 219 Position is 0 15
Colour at location of screen is 153 186 216 Position is 0 16
Colour at location of screen is 152 184 214 Position is 0 17
Colour at location of screen is 152 182 212 Position is 0 18
Colour at location of screen is 153 181 210 Position is 0 19
Colour at location of screen is 210 222 234 Position is 0 20
4

1 に答える 1

2

ソリューションの実行時の複雑さは恐ろしいものになるでしょう: O(N^2)。次のようなことを行うことで、これを再帰的に解決できます。

int height = 0;
int width = 0;

public void returnPixels(int height, int width)
     Color c = new Color(DesktopCapture.getRGB(width,height));
        int r = c.getRed();
        int g = c.getGreen();
        int b = c.getBlue();
        System.out.println("Colour at location of screen is " + r + " " + g + " " + b + " Position is " + x + " " + y);
      if(height < screenHeight){
        if(width < screenwidth){
           width++;
           returnPixels(height, width);
        } else if(width > screenWidth){
          width=0;
          height++;
          returnPixels(height, width);
          }
       }
    }

または似たようなもの。これは各行を読み取り、最後に到達すると次の高さ行に移動します

于 2013-05-25T05:00:46.547 に答える