Sarmad
Sarmad

Reputation: 3

java.lang.IllegalArgumentException: y must be < bitmap.height() while trying to get the points of an ImageView which are black

I am trying to find the points of the ImageView which are black. I am using a png image. Although the height of the ImageView is greater than the value of y that I'm passing. But still the exception occurs. Can you please help me identify my mistake?

public class MainActivity extends ActionBarActivity {
List<Point> points = new ArrayList<Point>();

TextView textView;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = (ImageView)findViewById(R.id.imageView1);

    //textView = (TextView)findViewById(R.id.textView1);

}

@Override
public void onWindowFocusChanged (boolean hasFocus){
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus){
        int top= imageView.getTop();
        int left= imageView.getLeft();
        int right= imageView.getRight();
        int bottom = imageView.getBottom();
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        for(int x=left; x<right; x++)
        {
            for(int y=top; y<bottom; y++)
            {
                int pixel = bitmap.getPixel(x, y);
                if(pixel==color.black)
                {
                    Point point=new Point();
                    point.x=x;
                    point.y=y;
                    points.add(point);
                }
            }
        }
        //textView.setText("Starting points: " + top + "," + left + " Ending Points:" + right + ","+ bottom);


    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class Point {
    float x, y;

@Override
public String toString() {
    return x + ", " + y;
}

}

}

Upvotes: 0

Views: 1214

Answers (1)

Don't you need to start x and y at 0, then iterate to bitmap.Width and bitmap.Height? You are using the imageView to get the dimensions, which will be different to the bitmap.

Upvotes: 1

Related Questions