Here's gig's thread. It was written for a Web cam but the principles should be transferable to a scanner.