1

I am still learning Java, so please be patient if the question is too easy.. I am trying to find a specific color in a if-condition, let's say blue (RGB: (0,0,225)) by analyzing a picture pixel by pixel with the following for-loop:

    public void findColor(){     

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

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

I also have another class to specify the RGB color:

 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;
    }
}   

My question is, how to connect those two classes in order to check for the RGB color of a pixel?

Janna Sherazi
  • 167
  • 1
  • 1
  • 15
  • What are you trying to do ? Detect a particular colon ? Detect witch color each pixel is ? BTW, classname in Java usually starts with a capital letter ;) – Guillaume May 15 '16 at 18:54
  • As I said I am trying to evaluate in my if condition if given pixel is blue, if it is then I'll change it to another color, but I know how to handle the second part :) – Janna Sherazi May 15 '16 at 18:56
  • 1
    You could give your Color class an equals method, one which would compare the rgb values of another Color class, and then the test could be `if (x.equals(y))`. But why are you re-inventing the wheel? Why create your own Color class when others already exist? – Hovercraft Full Of Eels May 15 '16 at 19:05

2 Answers2

1

At first I would change the method head findColor () to findColor (Color aColor). So you can re-use this method.

You didn't give us any hints what the mystic Picture is. But if you save the image in a BufferedImage, you can get the RGB-Color by calling Picture.getRGB(x,y). More documentation in BufferedImage on oracle.

In your example it would be int int packedInt = img.getRGB(w, h); Then you should convert this value to a Color Object. Color myColor = new Color(packedInt, true);

At this point you should think about to use the standard JAVA Color class instead of your class.

Now you can compare the actual myColor with the input field of your Method.

EDIT: there is a similar issue to yours at stackowerflow: link

Community
  • 1
  • 1
hydroid
  • 33
  • 6
1

I have been using this for getting color of pixels

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);
         }
      }
   }
}

it should work, you need to add your test and else for changing color of pixel, if you have some problem with this just ask

Zoran Kokeza
  • 165
  • 12