How do you know it is in focus?  You recognize the object, of course, but more generally, because it has detail.  Detail, typically, means drastic change in color over a short range of pixels.  I'm sure you can find a lot of edge detection algorithms out there via google.  Without giving it much thought:
edgePixelCount = 0;
for each pixel in image
{
    mixed = pixel.red + pixel.blue + pixel.green;
    for each adjacentPixel in image.adjacentPixels(pixel)
    {
        adjacentMixed = 
           adjacentPixel.red + 
           adjacentPixel.blue + 
           adjacentPixel.green;
        if ( abs ( adjacentMixed - mixed ) > EDGE_DETECTION_THRESHOLD )
        {
             edgePixelCount++;
             break;
        }
    }
}
if (edgePixelCount > NUMBER_OF_EDGE_PIXELS_THRESHOLD)
{
     focused = true;
}
Note:  you'd probably need to use "adjacent pixels" with some distance, not just immediate edge pixels.  Even in focus, high res images could often have gradients.