I know how to capture a screenshot by using Robot, Windowtester or FEST. I also know how to read a pixel from the screen by using robot.
int x = 10;
int y = 10;
Color px = getPixelColor(int x, int y);
However, I don't know how to read a pixel from an image that is already captured. I'm planning to compare a current image, with an image from file. Lets say both are PNG. Are there any frameworks that I can use to compare images pixel by pixel?
Load them as
BufferedImage
instances and it is relatively easy.Here is part of code that creates images with text, then creates a new image that shows the difference between the image with & without text.
Screensot
Full code
You can read image file: Reading/Loading an Image.
And then get color using
getRGB
method.Is this in Java? If so, you can use
ImageIO.read( "yourImage.png" )
to get aBufferedImage
. That will have agetData()
method which will give you aRaster
object, on which you can callgetPixel
. See linkThis should work:
Then you can walk through the pixels and compare with the images pixel by pixel with this:
In C/C++, if you're comfortable requiring a minimum version of Windows, it's rather easy, you can use GDI+ to load the image and draw it to a memory bitmap, then you can use the returned pointer to get the pixel data.
Use
GdiplusStartup()
andGdiplusShutdown()
to initialise and uninitialise GDI+.Use a GDI+
Image
object, using the overload that takes a filename, to load the image, then use the methodsGetWidth()
andGetHeight()
, aBITMAPINFO
structure and theCreateDIBSection()
GDI function to create a memory bitmap.Then use
CreateCompatibleDC()
to create a device context for the bitmap andSelectObject()
to select the bitmap into that device context.Then you use a GDI+
Graphics
object, using the overload that takes a device context, and itsDrawImage()
method, using the overload that takesx
,y
,width
andheight
, to draw the image to the bitmap's device context.After that, you can get/set the pixel data using the pointer returned by
CreateDIBSection()
.When you're done, use
DeleteDC()
to get rid of the bitmap's device context BEFORE usingDeleteObject()
to get rid of the bitmap. GDI+ Image objects can also be used to save images in a supported format, including PNG.