9

i need to open external UVC camera with

Camera.open(CameraId);

but not working

Please help?

6 Answers 6

6

I am researching this topic for several days and will contribute my partial findings here.

  1. There is a native library that can be used to get access to external UVC camera https://github.com/ktossell/libuvc. I've found that this project used it and when I compiled and tested it I've found the samples only partially stable and the source code it carries overwhelming.
  2. The 2 years old Camera2 API seems to include external Camera support. I've found in Android project documentation - Camera device the following statements:

    "External camera (e.g. USB hot-plug camera) support. The API updates specify the camera static info is available only when camera is connected and ready to use for external hot-plug cameras. Calls to get static info will be invalid calls when camera status is not CAMERA_DEVICE_STATUS_PRESENT. The framework counts solely on device status change callbacks to manage the available external camera list."

I am yet unable to write code using the Camera2 API that access my webcam.

Last, I've even found that in Android source code the following code

(144) // TODO: update to CAMERA_FACING_EXTERNAL once the HAL API changes are merged.

While CAMERA_FACING_EXTERNAL is a new value of CameraInfo class for external camera that are obviously not FRONT and not back.

I hope this will help you progressing toward the complete answer.

3
  • Hi. Have you some news about Camera2 AI and webcam? Oct 11, 2018 at 10:58
  • Hey, any progress after 4 years? I am working on a similar project and external usb camera access is still almost nonexistent.
    – mcy
    Dec 29, 2020 at 13:21
  • Never succeed to progress on that and the project never completed... Dec 30, 2020 at 14:04
6

Unfortunately, while the camera2 API does have the necessary API interfaces for supporting external cameras such as USB webcams, there's not yet standard low-level support for external cameras.

Some individual device manufacturers do have such support, but that is a very small number of devices currently.

Until then, the only way to have USB webcams be usable by an Android application is to use the raw USB APIs directly, and not use the camera APIs at all. You can either try to find an existing UVC library to use, or write your own.

6

Tried and tested : https://github.com/saki4510t/UVCCamera

The above link comes with 8 different apps in a master app, implementing different features in each. Go through the apps.

Use libuvc library to access the external camera.

4

The AOSP contains a Camera3 HAL implementation that is using V4L2. It even comes with a nice readme that also points out its current limitations: https://github.com/aosp-mirror/platform_hardware_libhardware/tree/master/modules/camera/3_4

1

This feature has been implemented on some platforms by relocating each function in camera_device_ops_t on hardware to the USB_CAMERA function,like below:

camera_device_ops_t usb_camera_camera_ops = {
  set_preview_window:         usb_camera_set_preview_window,
  set_callbacks:              usb_camera_set_CallBacks,
  enable_msg_type:            usb_camera_enable_msg_type,
  disable_msg_type:           usb_camera_disable_msg_type,
  msg_type_enabled:           usb_camera_msg_type_enabled,

  start_preview:              usb_camera_start_preview,
  stop_preview:               usb_camera_stop_preview,
  preview_enabled:            usb_camera_preview_enabled,
  store_meta_data_in_buffers: usb_camera_store_meta_data_in_buffers,

  start_recording:            usb_camera_start_recording,
  stop_recording:             usb_camera_stop_recording,
  recording_enabled:          usb_camera_recording_enabled,
  release_recording_frame:    usb_camera_release_recording_frame,

  auto_focus:                 usb_camera_auto_focus,
  cancel_auto_focus:          usb_camera_cancel_auto_focus,

  take_picture:               usb_camera_take_picture,
  cancel_picture:             usb_camera_cancel_picture,

  set_parameters:             usb_camera_set_parameters,
  get_parameters:             usb_camera_get_parameters,
  put_parameters:             usb_camera_put_parameters,
  send_command:               usb_camera_send_command,

  release:                    usb_camera_release,
  dump:                       usb_camera_dump,
};
0

USB Cameras are not going to work on newer camera2 api

(Update: They work fine with Camera2 API, but intermittently has NumberFormatException for cameraID [USB Cameras are ID like /dev/video1]. Hence I cannot tell what to do for that.

USB Cameras may be supported on older camera2 api

However with the proper support USB cameras will work on camera1 api. In interface 3.4 I have also enabled the YUV support.

Here's my entire work

UVC Camera support in AOSP

Here's the patch for camera/device/3.4/default/ExternalCameraDevice.cpp YUV support

diff --git a/camera/device/3.4/default/ExternalCameraDevice.cpp b/camera/device/3.4/default/ExternalCameraDevice.cpp
index ee7ffaa..2444404 100644
--- a/camera/device/3.4/default/ExternalCameraDevice.cpp
+++ b/camera/device/3.4/default/ExternalCameraDevice.cpp
@@ -38,8 +38,10 @@ namespace {
 // Other formats to consider in the future:
 // * V4L2_PIX_FMT_YVU420 (== YV12)
 // * V4L2_PIX_FMT_YVYU (YVYU: can be converted to YV12 or other YUV420_888 formats)
-const std::array<uint32_t, /*size*/1> kSupportedFourCCs {{
-    V4L2_PIX_FMT_MJPEG
+const std::array<uint32_t, /*size*/3> kSupportedFourCCs {{
+    V4L2_PIX_FMT_MJPEG,
+    V4L2_PIX_FMT_YUYV,
+    V4L2_PIX_FMT_YUV420
 }}; // double braces required in C++11
 
 constexpr int MAX_RETRY = 5; // Allow retry v4l2 open failures a few times.
diff --git a/camera/device/3.4/default/ExternalCameraDeviceSession.cpp b/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
index 1af3f39..37bf75f 100644
--- a/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
+++ b/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
@@ -1724,33 +1724,60 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
         return false;
     };
 
-    if (req->frameIn->mFourcc != V4L2_PIX_FMT_MJPEG) {
-        return onDeviceError("%s: do not support V4L2 format %c%c%c%c", __FUNCTION__,
+    if (req->frameIn->mFourcc != V4L2_PIX_FMT_MJPEG && req->frameIn->mFourcc != V4L2_PIX_FMT_YUYV && req->frameIn->mFourcc != V4L2_PIX_FMT_YUV420) {
+        return onDeviceError("%s: do not support V4L2 format %c%c%c%c xxx", __FUNCTION__,
                 req->frameIn->mFourcc & 0xFF,
                 (req->frameIn->mFourcc >> 8) & 0xFF,
                 (req->frameIn->mFourcc >> 16) & 0xFF,
                 (req->frameIn->mFourcc >> 24) & 0xFF);
     }
+    ALOGV("%s: V4L2 format: %c%c%c%c", __FUNCTION__,
+                req->frameIn->mFourcc & 0xFF,
+                (req->frameIn->mFourcc >> 8) & 0xFF,
+                (req->frameIn->mFourcc >> 16) & 0xFF,
+                (req->frameIn->mFourcc >> 24) & 0xFF);
 
     std::unique_lock<std::mutex> lk(mBufferLock);
     // Convert input V4L2 frame to YU12 of the same size
     // TODO: see if we can save some computation by converting to YV12 here
     uint8_t* inData;
     size_t inDataSize;
+    int res = -1;
     req->frameIn->map(&inData, &inDataSize);
-    // TODO: in some special case maybe we can decode jpg directly to gralloc output?
-    ATRACE_BEGIN("MJPGtoI420");
-    int res = libyuv::MJPGToI420(
-            inData, inDataSize,
-            static_cast<uint8_t*>(mYu12FrameLayout.y),
-            mYu12FrameLayout.yStride,
-            static_cast<uint8_t*>(mYu12FrameLayout.cb),
-            mYu12FrameLayout.cStride,
-            static_cast<uint8_t*>(mYu12FrameLayout.cr),
-            mYu12FrameLayout.cStride,
-            mYu12Frame->mWidth, mYu12Frame->mHeight,
-            mYu12Frame->mWidth, mYu12Frame->mHeight);
-    ATRACE_END();
+    ALOGV("%s: Size: %zu, WxH: (%u, %u)", __FUNCTION__, inDataSize, mYu12Frame->mWidth, mYu12Frame->mHeight);
+    if (req->frameIn->mFourcc == V4L2_PIX_FMT_MJPEG) {
+        // TODO: in some special case maybe we can decode jpg directly to gralloc output?
+        ATRACE_BEGIN("MJPGtoI420");
+        res = libyuv::MJPGToI420(
+                inData, inDataSize,
+                static_cast<uint8_t*>(mYu12FrameLayout.y),
+                mYu12FrameLayout.yStride,
+                static_cast<uint8_t*>(mYu12FrameLayout.cb),
+                mYu12FrameLayout.cStride,
+                static_cast<uint8_t*>(mYu12FrameLayout.cr),
+                mYu12FrameLayout.cStride,
+                mYu12Frame->mWidth, mYu12Frame->mHeight,
+                mYu12Frame->mWidth, mYu12Frame->mHeight);
+        ATRACE_END();
+    } else if (req->frameIn->mFourcc == V4L2_PIX_FMT_YUYV && inDataSize != libyuv::kUnknownDataSize ) { //&& inDataSize != libyuv::kUnknownDataSize
+        ALOGV("%s: Converting to I420 | Size: %zu, WxH: (%ux%u), yStride: %u, cStride: %u", __FUNCTION__, inDataSize, mYu12Frame->mWidth, mYu12Frame->mHeight, mYu12FrameLayout.yStride, mYu12FrameLayout.cStride);
+        ATRACE_BEGIN("YUY2ToI420");
+        res = libyuv::YUY2ToI420(inData, mYu12Frame->mWidth * 2,
+                static_cast<uint8_t*>(mYu12FrameLayout.y),
+                mYu12FrameLayout.yStride,
+                static_cast<uint8_t*>(mYu12FrameLayout.cb),
+                mYu12FrameLayout.cStride,
+                static_cast<uint8_t*>(mYu12FrameLayout.cr),
+                mYu12FrameLayout.cStride,
+                mYu12Frame->mWidth, mYu12Frame->mHeight);
+        ATRACE_END();
+    } else{
+        ALOGV("%s: YUV420 format. No conversion", __FUNCTION__);
+        memcpy(static_cast<uint8_t*>(mYu12FrameLayout.y), inData, inDataSize/2);
+        memcpy(static_cast<uint8_t*>(mYu12FrameLayout.cb), (inData + inDataSize/2), inDataSize/4);
+        memcpy(static_cast<uint8_t*>(mYu12FrameLayout.cr), (inData + inDataSize/2 + inDataSize/4), inDataSize/4 );
+        res = 0;
+    }
 
     if (res != 0) {
         // For some webcam, the first few V4L2 frames might be malformed...

Another diff for /hardware/qcom/camera


diff --git a/QCamera2/HAL/QCameraParameters.cpp b/QCamera2/HAL/QCameraParameters.cpp
index 90d83d0..293229b 100644
--- a/QCamera2/HAL/QCameraParameters.cpp
+++ b/QCamera2/HAL/QCameraParameters.cpp
@@ -11675,7 +11675,7 @@ uint32_t QCameraParameters::getJpegQuality()
 {
     int quality = getInt(KEY_JPEG_QUALITY);
     if (quality < 0) {
-        quality = 85; // set to default quality value
+        quality = 90; // set to default quality value
     }
     return (uint32_t)quality;
 }
@@ -11691,7 +11691,7 @@ uint32_t QCameraParameters::getJpegQuality()
  *==========================================================================*/
 uint32_t QCameraParameters::getRotation() {
     int rotation = 0;
-
+    LOGE("%s: %d", __func__, mRotation);
     //If exif rotation is set, do not rotate captured image
     if (!useJpegExifRotation()) {
         rotation = mRotation;
@@ -11729,7 +11729,7 @@ void QCameraParameters::setJpegRotation(int rotation) {
  *==========================================================================*/
 uint32_t QCameraParameters::getDeviceRotation() {
     int rotation = 0;
-
+    LOGE("%s: %d", __func__, mRotation);
     rotation = mRotation;
     if (rotation < 0) {
         rotation = 0;
@@ -11749,7 +11749,7 @@ uint32_t QCameraParameters::getDeviceRotation() {
  *==========================================================================*/
 uint32_t QCameraParameters::getJpegExifRotation() {
     int rotation = 0;
-
+    LOGE("%s: %d", __func__, mRotation);
     if (useJpegExifRotation()) {
         rotation = mRotation;
         if (rotation < 0) {
@@ -11772,7 +11772,7 @@ bool QCameraParameters::useJpegExifRotation() {
     char exifRotation[PROPERTY_VALUE_MAX];
 
     property_get("persist.vendor.camera.exif.rotation", exifRotation, "off");
-
+    LOGE("%s: %s", __func__, exifRotation);
     if (!strcmp(exifRotation, "on")) {
         return true;
     }
diff --git a/usbcamcore/inc/QCameraUsbParm.h b/usbcamcore/inc/QCameraUsbParm.h
index 595bf42..9e1e070 100755
--- a/usbcamcore/inc/QCameraUsbParm.h
+++ b/usbcamcore/inc/QCameraUsbParm.h
@@ -62,13 +62,13 @@ static const int PICTURE_FORMAT_JPEG = 1;
 static const int PICTURE_FORMAT_RAW = 2;
 
 /* Default preview width in pixels */
-#define DEFAULT_USBCAM_PRVW_WD  1280//640
+#define DEFAULT_USBCAM_PRVW_WD  640
 
 /* Default preview height in pixels */
-#define DEFAULT_USBCAM_PRVW_HT  720//480
+#define DEFAULT_USBCAM_PRVW_HT  480
 
 /* Default picture format */
-#define DEFAULT_USBCAM_PICT_FMT     PICTURE_FORMAT_JPEG
+#define DEFAULT_USBCAM_PICT_FMT     PICTURE_FORMAT_RAW
 
 /* Default picture width in pixels */
 #define DEFAULT_USBCAM_PICT_WD  640
@@ -80,13 +80,13 @@ static const int PICTURE_FORMAT_RAW = 2;
 #define DEFAULT_USBCAM_PICT_QLTY  85
 
 /* Default thumbnail width in pixels */
-#define DEFAULT_USBCAM_THUMBNAIL_WD    432
+#define DEFAULT_USBCAM_THUMBNAIL_WD    352
 
 /* Default thumbnail height in pixels */
 #define DEFAULT_USBCAM_THUMBNAIL_HT    288
 
 /* Default thumbnail JPEG quality 0-100 */
-#define DEFAULT_USBCAM_THUMBNAIL_QLTY  85
+#define DEFAULT_USBCAM_THUMBNAIL_QLTY  90
 
 /* Default preview format */
 #define DEFAULT_USBCAM_PRVW_FMT HAL_PIXEL_FORMAT_YCrCb_420_SP
diff --git a/usbcamcore/src/QCameraUsbParm.cpp b/usbcamcore/src/QCameraUsbParm.cpp
index fda6e9d..4bfac42 100755
--- a/usbcamcore/src/QCameraUsbParm.cpp
+++ b/usbcamcore/src/QCameraUsbParm.cpp
@@ -94,12 +94,18 @@ static const preview_format_info_t preview_format_info_list[] = {
 };
 
 static struct camera_size_type previewSizes[] = {
-    { 1920, 1088}, //1080p
-    { 1280, 720}, // 720P,
-    { 640, 480}, // VGA
-    { 512, 384},
-    { 480, 320},
-    { 320, 240}, // QVGA
+    {1920, 1088},
+    {1280, 800},
+    {1280, 720},
+    {1024, 768},
+    {848, 480},
+    {800, 600},
+    {640, 480},
+    {640, 360},
+    {352, 288},
+    {320, 240},
+    {176, 144},
+    {160, 120}
 };
 
 // All fps ranges which can be supported. This list will be filtered according
@@ -118,14 +124,22 @@ static const str_map frame_rate_modes[] = {
 
 static const str_map picture_formats[] = {
     {QCameraParameters::PIXEL_FORMAT_JPEG, PICTURE_FORMAT_JPEG},
-    //{QCameraParameters::PIXEL_FORMAT_RAW, PICTURE_FORMAT_RAW}
+    {QCameraParameters::PIXEL_FORMAT_RAW, PICTURE_FORMAT_RAW}
 };
 
 static camera_size_type picture_sizes[] = {
-    { 1920, 1088}, //HD1080
-    { 1280, 720}, //HD720
-    { 640, 480}, // VGA
-    { 320, 240}, // QVGA
+    {1920, 1088},
+    {1280, 800},
+    {1280, 720},
+    {1024, 768},
+    {848, 480},
+    {800, 600},
+    {640, 480},
+    {640, 360},
+    {352, 288},
+    {320, 240},
+    {176, 144},
+    {160, 120}
 };
 
 /* aspect ratio removed */
diff --git a/usbcamcore/src/QualcommUsbCamera.cpp b/usbcamcore/src/QualcommUsbCamera.cpp
index b274626..2351d8b 100755
--- a/usbcamcore/src/QualcommUsbCamera.cpp
+++ b/usbcamcore/src/QualcommUsbCamera.cpp
@@ -90,7 +90,7 @@ camera_device_ops_t usbcam_camera_ops = {
 #define CALL_BACK               1
 #define MEMSET                  0
 #define FREAD_JPEG_PICTURE      0
-#define JPEG_ON_USB_CAMERA      1
+#define JPEG_ON_USB_CAMERA      0
 #define FILE_DUMP_CAMERA        0
 #define FILE_DUMP_B4_DISP       0
 
@@ -162,7 +162,7 @@ extern "C" int usbcam_get_camera_info(int camera_id, struct camera_info *info)
         struct CameraInfo camInfo;
         memset(&camInfo, -1, sizeof (struct CameraInfo));
 
-        info->facing = CAMERA_FACING_FRONT;//CAMERA_FACING_BACK;
+        info->facing = CAMERA_FACING_BACK;//CAMERA_FACING_FRONT
         info->orientation = 0;
         rc = 0;
     }


Another diff of /hardware/qcom/camera/usbcamcore

diff --git a/usbcamcore/inc/QCameraUsbParm.h b/usbcamcore/inc/QCameraUsbParm.h
index c47ef1f..2a1a92a 100755
--- a/usbcamcore/inc/QCameraUsbParm.h
+++ b/usbcamcore/inc/QCameraUsbParm.h
@@ -60,6 +60,7 @@ extern "C" {
 /* enum definitions for picture formats */
 static const int PICTURE_FORMAT_JPEG = 1;
 static const int PICTURE_FORMAT_RAW = 2;
+static const int PICTURE_FORMAT_YUV = 3;
 
 /* Default preview width in pixels */
 #define DEFAULT_USBCAM_PRVW_WD  1280//640
@@ -68,7 +69,7 @@ static const int PICTURE_FORMAT_RAW = 2;
 #define DEFAULT_USBCAM_PRVW_HT  720//480
 
 /* Default picture format */
-#define DEFAULT_USBCAM_PICT_FMT     PICTURE_FORMAT_RAW
+#define DEFAULT_USBCAM_PICT_FMT     PICTURE_FORMAT_YUV
 
 /* Default picture width in pixels */
 #define DEFAULT_USBCAM_PICT_WD  640
@@ -89,7 +90,7 @@ static const int PICTURE_FORMAT_RAW = 2;
 #define DEFAULT_USBCAM_THUMBNAIL_QLTY  85
 
 /* Default preview format */
-#define DEFAULT_USBCAM_PRVW_FMT HAL_PIXEL_FORMAT_YCrCb_420_SP
+#define DEFAULT_USBCAM_PRVW_FMT HAL_PIXEL_FORMAT_YV12
 
 /* minimum of the default preview fps range in milli-Hz */
 #define MIN_PREV_FPS            5000
diff --git a/usbcamcore/src/QCameraUsbParm.cpp b/usbcamcore/src/QCameraUsbParm.cpp
index 4bfac42..4393a35 100755
--- a/usbcamcore/src/QCameraUsbParm.cpp
+++ b/usbcamcore/src/QCameraUsbParm.cpp
@@ -26,7 +26,7 @@
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-//#define ALOG_NDEBUG 0
+#define ALOG_NDEBUG 0
 #define ALOG_NIDEBUG 0
 #define LOG_TAG "QCameraUsbParm"
 #include <utils/Log.h>
@@ -86,7 +86,8 @@ namespace android {
 
 /********************************************************************/
 static const str_map preview_formats[] = {
-    {QCameraParameters::PIXEL_FORMAT_YUV420SP, HAL_PIXEL_FORMAT_YCrCb_420_SP},
+    {QCameraParameters::PIXEL_FORMAT_YUV420P, HAL_PIXEL_FORMAT_YV12},
+    {QCameraParameters::PIXEL_FORMAT_YUV420SP, HAL_PIXEL_FORMAT_YCrCb_420_SP}
 };
 
 static const preview_format_info_t preview_format_info_list[] = {
@@ -124,7 +125,8 @@ static const str_map frame_rate_modes[] = {
 
 static const str_map picture_formats[] = {
     {QCameraParameters::PIXEL_FORMAT_JPEG, PICTURE_FORMAT_JPEG},
-    {QCameraParameters::PIXEL_FORMAT_RAW, PICTURE_FORMAT_RAW}
+    {QCameraParameters::PIXEL_FORMAT_RAW, PICTURE_FORMAT_RAW},
+    {QCameraParameters::PIXEL_FORMAT_YUV420P, PICTURE_FORMAT_YUV}
 };
 
 static camera_size_type picture_sizes[] = {

This project has taken almost 4 months and will appreciate a thumbs up. Also I hope it can be part of the google source someday after proper review.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.