Use USB Camera on Android application

neoffreeman picture neoffreeman · Jan 22, 2012 · Viewed 79.3k times · Source

I need to display a video transmitted by an USB Camera connected to my tablet on Android 3.2.

How can I do this?

I can't use the tablet's camera, because my tablet is fixed on board.

Answer

mk.. picture mk.. · Jan 24, 2012

(I wrote this in 2012 when there was no support for USB camera. I am not working on Android anymore.. so I cannot explain how to do it with the API of latest Android. If you know any better solution please post it here.)

The discussion holds true only if the sensor inside USB camera is UVC compliant(most of todays' cameras are compliant).

As of the date of this post(March 1 2012), there are no default Android API's available for working on external camera. So if you are serious about the project, the work you do would involve writing a firmware that talks to the kernel drivers and then displaying the data on the Android application layer.

When you insert a USB camera, There are few things that you need to check first.

1) Is your camera getting power from USB port?(For this to happen your USB port should have USB-OTG support- USB port can act both as target and host, Check whether the lights on camera(if any) are glowing or not?)

2) Is node getting created in /dev directory?(Only in case the kernel has v4l2 and UVC support enabled,node will be created). If node is getting created, now you have access to the real hardware that is USB camera and your job is going to be easy from here on. to check do ls -l /dev/v* inside the android file system and check whether video0 or video1 is being created.

4) If node is created, you need to write a firmware(UVC) and acquire a frame from the real hardware. It(UVC) supports different ioctl calls that talk to the v4l2 layer in the kernel and will fetch you the frames.

These frames would be mostly of format YUYV422 or YUYV420 format.

Once you receive the frames, convert into RGB or Jpeg (Android has support to convert ) and display on the canvas or imageview whichever you like.

For recording you might have to use ffmpeg for encoding. This will convert the images into video.