From a technical perspective it's definitely possible. I can't remember off hand, but i'm almost certain I came across something that did this before..
A quick google turned up the following:
http://www.bytestrom.eu/blog/2009/1120a_jpeg_encoder_for_jav...
https://gist.github.com/1245476
Hope it helps