Live Camera Previews in Android

Posted: March 1, 2012 in Android Posts
Tags: , , , , ,

The Android SDK provided by Google doesn’t provide any camera emulation beyond a stand-in animation (see below). Since Google haven’t yet provided a working implementation (or even a date by which one will be available), I’m publishing the source code for a set of temporary classes that I wrote to overcome this limitation. It’s the first code I wrote on the Android platform, and it was written in haste, though it has functioned very well for me.

The code is public domain. I make no warranty as to its fitness for any particular purpose.

The code consists of:

  • CameraSource
    A light interface that protects client code from the choice of camera.
  • GenuineCamera
    A CameraSource implementation that uses the device’s camera.
  • HttpCamera
    A CameraSource implementation that GETs images over an HTTP connection.
  • SocketCamera
    A CameraSource implementation that obtains images directly over a TCP/IP connection.
  • BitmapCamera
    A CameraSource implementation that uses a supplied bitmap.
  • WebcamBroadcaster
    A small Java application that uses JMF to broadcast captured stills over the network.


When drawing the output of the genuine camera to screen, you will see something that looks like this:

Device’s Camera preview output.

When drawing the output of a remote camera that is obtaining frames from a running webcam broadcaster you would see this (if you were stood over my desk).

Still obtained from a webcam broadcaster.

To save myself more time, I chose to produce a small settings configuration activity too (the code for which is not included).

A simple configuration activity for the camera settings.


Using a camera source is very simple:

CameraSource cs = new SocketCamera("", 9889, 320, 240, true); if (! { /* deal with failure to obtain camera */ } while(/*some condition*/) { cs.capture(canvas) //capture the frame onto the canvas } cs.close();

The HttpCamera implementation should be able to obtain stills from any HTTP serving webcam or webcam software. At present I am using WebCam2000 which I run under Windows XP.

Fabian Necci kindly emailed me to let me know that he has successfully used this code on a MacBook using EvoCam to publish JPEG frames to an internal web server. Other software should be available on other systems.

Torben Vesterager has used the code with his MacBook Pro laptop which has Apple’s own iSight built-in. He reports that the camera only supports YUVFormats and so had to change the code in the WebcamBroadcaster to use YUVFormat instead of RGBFormat.

Jeffrey Sharkey has used Gentoo Linux with a Logitech QuickCam express using the WebcamBroadcaster and the Linux-native JMF library. He followed this by using DV4Linux to get JMF to recognize his firewire MiniDV camera. This gave him much clearer pictures in Android; he wrapped his JMF calls using the dv4lstart utility:

dv4lstart jmfinit dv4lstart java WebcamBroadcaster

If you successfully use this code on a platform other than those listed above, or have used other webcam software, please let me know so that I can include it in this documentation for the benefit of others.

The SocketCamera implementation relies on a WebcamBroadcaster to be serving webcam stills from the address & port specified in its constructor.

To run a WebcamBroadcaster you will need JMF installed and you may need to supply the library location to the JVM when you run the application (this depends on how you install it). For example, on my machine (currently Windows XP) I use:

java "-Djava.library.path=C:\Program Files\JMF2.1.1e\lib" WebcamBroadcaster

You can pass the following combination of parameters to the application, all of which are strictly positive integers (all dimensions in pixels):

  • WebcamBroadcaster port
  • WebcamBroadcaster width height
  • WebcamBroadcaster width height port

Alternatively, you may provide no parameters, in which case the defaults are width: 320, height: 240 and port: 9889.


  • I am not able to provide support/advice relating to JMF. Sorry, but the library is old and the documentation stale. I am using it here because its the quickest option, not necessarily the best. You can reimplement the WebcamBroadcaster application using any language/library you choose, so long as it emulates the extremely simple behaviour of the WebcamBroadcaster class. For an easy life I recommend using the HttpCamera implementation.
  • The SocketCamera implementation can be expected to outperform the HttpCamera implementation significantly.
  • Obviously performance is poor, every frame needs to be compressed, sent over a network and then decompressed. This is not a useful long term implementation. Nevertheless, performance isn’t so bad that the code is unusable. On my machine, capturing a frame using SocketCamera takes about 100-150ms, HttpCamera is typically two to three times slower.
  • Android currently features a very significant bug that disregards the connect timeout setting for URL connections. Until that bug is fixed, the HttpCamera implementation will hang the first time it fails to connect to the webcam server. I believe this bug was resolved in the m5 emulator releases, but have not confirmed it.
  • When capturing a sequence of stills, JMF has a habit of ‘freezing’ on a single frame. If this happens, just stop and start the WebcamBroadcaster application. You do not need to restart your Android application.
  • I don’t recommend using “localhost” when specifying an address to which the camera should connect. The Android device will probably treat this as the loopback on the emulated device itself. Instead use the IP address of the machine which is hosting the webcam (or WebcamBroadcaster).
  1. Winnie Guinane says:

    Real superb visual appeal on this web site , I’d rate it 10 10.


  2. music licensing agencies says:

    Interesting. I never knew there was such crap on the net.


  3. certified nurse assistant says:

    Great article once again! I am looking forward for more updates!


  4. games for kids says:

    But wanna input on few general things, The website layout is perfect, the subject material is very superb : D.


  5. signs of teething says:

    Rattling nice style and good subject material , hardly anything else we need : D.


  6. Jermaine Ruehter says:

    Attractive component of content. I simply stumbled upon your website and in accession capital to claim that I acquire actually loved account your blog posts. Any way I will be subscribing in your feeds and even I success you get right of entry to constantly rapidly.


  7. Buy xrumer list says:

    This actually answered my downside, thank you!


  8. try these good concepts to ramp up your affiliate marketing says:

    Incredible! This blog looks just like my old one! It’s on a totally different subject but it has pretty much the same layout and design. Great choice of colors!


  9. music licensing deals says:

    Some really nice and useful info on this internet site, likewise I think the design and style has great features.


  10. Broadjam scam says:

    Great write-up, I’m regular visitor of one’s blog, maintain up the excellent operate, and It is going to be a regular visitor for a lengthy time.


  11. cheap vacations says:

    Thanks for sharing excellent informations. Your site is very cool. I am impressed by the details that you’ve on this web site. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found simply the information I already searched everywhere and simply could not come across. What a great web-site.


  12. hybrid cars pros and cons says:

    Regards for all your efforts that you have put in this. Very interesting information. “Once in the racket you’re always in it.” by Al Capone.


  13. z media miami beach says:

    Does your website have a contact page? I’m having trouble locating it but, I’d like to shoot you an email. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it grow over time.


  14. Regena Bramasco says:

    Some really good content on this site, thankyou for contribution.


  15. seo for dummies says:

    Keep up the good work, I read few content on this website and I think that your blog is real interesting and has got sets of great information.


  16. where can i copyright my music says:

    I was reading through some of your blog posts on this site and I conceive this website is rattling informative ! Keep on posting .


  17. music licensing sites says:

    Into the bad gateway I turn though it is a 502 error I see. Okay. I can live with that but don’t want to.


  18. kore dizileri izle says:

    i bookmarked you in my browser admin thank you a lot i will be searching for your following posts


  19. carson cooper npr says:

    Hello! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having trouble finding one? Thanks a lot!


  20. mobile crane hire says:

    Appreciate it for all your efforts that you have put in this. very interesting info . “If a man writes a book, let him set down only what he knows. I have guesses enough of my own.” by Johann Wolfgang von Goethe.


  21. colleges with nursing programs says:

    Awesome story once again. I am looking forward for more updates:)


  22. how to make a website says:

    I always was interested in this subject and still am, appreciate it for putting up.


  23. credit card consolidation says:

    There is noticeably a bundle to know about this. I suppose you made certain good points in features also.


  24. Jessica Modest says:

    Hi, i think that i saw you visited my web site thus i came to “go back the choose”.I’m trying to in finding things to improve my website!I suppose its adequate to use a few of your concepts!!


  25. Joette says:

    I like this web site it’s a master piece! Glad I found this on google.


  26. four micro onde says:

    Thank you, I have recently been searching for information about this topic for ages and yours is the best I’ve discovered so far. However, what about the bottom line? Are you certain about the source?


  27. Odette says:

    I like this website very much so much fantastic information. “We cannot swing up on a rope that is attached only to our own belt.” by William Ernest Hocking.


  28. tv amr says:

    By that I mean start targeting a greater end clientele.


  29. coach credit card holder says:

    Great web site. A lot of useful info here. I’m sending it to several pals ans additionally sharing in delicious. And obviously, thanks in your effort!


  30. I conceive other website proprietors should take this site as an model, very clean and fantastic user pleasant design and style . “No act of kindness, no matter how small, is ever wasted.” by Aesop.


  31. Eva Angelina says:

    Keep up the great work, I read few posts on this internet site and I think that your website is very interesting and has circles of fantastic info.


  32. play mario games says:

    I genuinely enjoy studying on this web site , it contains fantastic posts . “Sometime they’ll give a war and nobody will come.” by Carl Sandburg.


  33. Backlinks RSS says:

    I like the efforts you have put in this, thanks for all the great posts.


  34. community health education says:

    Excellent read, I just passed this onto a friend who was doing a little research on that. And he just bought me lunch because I found it for him smile Thus let me rephrase that: Thank you for lunch!


  35. blackjack 2 case says:

    I consider something really interesting about your weblog so I saved to my bookmarks .


  36. tax attorney says:

    Undeniably believe that which you stated. Your favorite reason seemed to be on the internet the simplest thing to be aware of. I say to you, I certainly get irked while people think about worries that they plainly don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people could take a signal. Will likely be back to get more. Thanks


  37. […] Live Camera Previews in Android ( […]


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s