2016-05-15 17 views
0

私はまだJavaを学んでいるので、質問が簡単すぎる場合は忍耐してください。if条件で特定の色を見つけることを試みています、青(RGB:(0,0,225) )forループ以下でピクセルによって画像のピクセルを分析することによって、:特定の色を見つけるJava

public void findColor(){  

      for (int w=0; w< this.width; w++){ 
       for(int h=0; h< this.height; h++){ 

        if(this.Picture[w][h]=??){ 

私はまた、RGBの色を指定するには、別のクラスを持っている:

public class Color { 

    private int red; 
    private int green; 
    private int blue; 

    public Color(int r, int g, int b){ 
     this.red=r; 
     this.green=g; 
     this.blue=b; 
    } 

    public Color(Color c){ 
     this.red=c.red; 
     this.green=c.green; 
     this.blue=c.blue; 
    } 

    public void setColor(int r, int g, int b){ 
     this.red= r; 
     this.green= g; 
     this.blue = b; 
    } 

    public int colorRed(){ 
     return this.red; 
    } 

    public int colorGreen(){ 
     return this.green; 
    } 


    public int colorBlue(){ 
     return this.blue; 
    } 
} 

私の質問は、これらの2つのクラスを接続する方法であり、ピクセルのRGBカラーをチェックするには?

+0

何をしようとしていますか?特定のコロンを検出しますか?各ピクセルの魔法使いの色を検出する?ところで、Javaのclassnameは大文字で始まります;) – Guillaume

+0

与えられたピクセルが青であればif条件で評価しようとしていますが、それは別の色に変更しますが、 –

+1

Colorクラスにequalsメソッドを与えることができます。これは、別のColorクラスのrgb値を比較し、テストが 'if(x.equals(y))'になる可能性があります。しかし、なぜあなたは車輪を再発明していますか?他の色が既に存在する場合に、独自のColorクラスを作成するのはなぜですか? –

答えて

0

最初にメソッドヘッドfindColor()findColor (Color aColor)に変更します。したがって、このメソッドを再利用することができます。

あなたは神秘的なものPictureが何であるかのヒントを私たちに教えてくれませんでした。しかし、画像をBufferedImageに保存すると、Picture.getRGB(x,y)を呼び出してRGBカラーを取得できます。 BufferedImage on oracleの詳細については、

あなたの例では、それはint int packedInt = img.getRGB(w, h);になります。次に、この値をカラーオブジェクトに変換する必要があります。 Color myColor = new Color(packedInt, true);

この時点で、クラスの代わりに標準JAVA Colorクラスを使用することを検討する必要があります。

実際のmyColorとメソッドの入力フィールドを比較することができます。

EDIT:同様の問題がstackowerflowであなたにあります: link

0

私はそれが動作するはずピクセル

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

public class Main { 
    public static void main(String args[]) throws IOException { 
     File file = new File("your_file.jpg"); 
     BufferedImage image = ImageIO.read(file); 

     int w = image.getWidth(); 
     int h = image.getHeight(); 
     for (int i = 0; i < h; i++) { 
      for (int j = 0; j < w; j++) { 
       int pixel = image.getRGB(w, h); 
       int red = (pixel & 0x00ff0000) >> 16; 
       int green = (pixel & 0x0000ff00) >> 8; 
       int blue = pixel & 0x000000ff; 
       System.out.println("Red Color value = " + red); 
       System.out.println("Green Color value = " + green); 
       System.out.println("Blue Color value = " + blue); 
     } 
     } 
    } 
} 

の色を取得するためにこれを使用している、あなたのテストを追加する必要があり、ピクセルの色を変更する場合は、これに何らかの問題がある場合は質問してください。

関連する問題