It's actually really easy!
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Once, you have a Bitmap
object, there's a couple of options.
bm.getPixel(x,y)
will return an int
that corresponds to an int
in the Color
class, such as Color.BLACK
or Color.WHITE
.
Additionally, bm.copyPixelsToBuffer(Buffer destination)
will copy all of the pixels into a Buffer
object, which you could search pixel-by-pixel.
Check out the documentation for further details.
Here is a sample snippet of code, assuming that you have an image in your /res/drawable folder called 'image'.
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
int pixelColor = bm.getPixel(10,10); //Get the pixel at coordinates 10,10
if(pixelColor == Color.BLACK) {
//The pixel is black
}
else if(pixelColor == Color.WHITE) {
//The pixel was white
}
Obviously, you should be careful about getting pixels. Make sure the pixel exists, and that the coordinate is not bigger than the image. To get the dimensions of a Bitmap
, simply use bm.getHeight()
and bm.getWidth()
, respectively.