Help! how to identify the overall of a jpeg

Hi, I would like to do something like this.

Therefore, my concepts is I have webcam. My webcam is able to catch a image. Whereby, the flash will calculate the overall of the color. And it will resize and place it to the nearest color of a original jpeg color.