Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the camera scene mode for OpenCV on Android

I'm looking for a way to set a scene mode value for a web camera from c++ (Android NDK).

OpenCV gets the values from the web camera when it is initialized. In the below log, the log says Supported Scene Modes: auto,action,portrait,landscape,beach,fireworks,night,night-portrait,snow,sports,steadyphoto,sunset,theatre,barcode.

I would like to set one of the values (such as beach) for the camera.

D/OpenCV::camera﹕ CvCapture_Android::CvCapture_Android(1)
D/OpenCV::camera﹕ Library name: libopencv_java.so
D/OpenCV::camera﹕ Library base address: 0x69f3b000
D/OpenCV::camera﹕ Libraries folder found: /data/app-lib/gms.rewards.selfie-2/
D/OpenCV::camera﹕ CameraWrapperConnector::connectToLib: folderPath=/data/app-lib/gms.rewards.selfie-2/
E/OpenCV::camera﹕ ||libnative_camera_r4.1.1.so
E/OpenCV::camera﹕ ||libnative_camera_r4.0.3.so
E/OpenCV::camera﹕ ||libnative_camera_r2.3.3.so
E/OpenCV::camera﹕ ||libnative_camera_r4.2.0.so
E/OpenCV::camera﹕ ||libnative_camera_r2.2.0.so
E/OpenCV::camera﹕ ||libnative_camera_r3.0.1.so
E/OpenCV::camera﹕ ||libnative_camera_r4.3.0.so
E/OpenCV::camera﹕ ||libnative_camera_r4.0.0.so
E/OpenCV::camera﹕ ||libnative_camera_r4.4.0.so
D/OpenCV::camera﹕ try to load library 'libnative_camera_r4.4.0.so'
D/OpenCV::camera﹕ CameraWrapperConnector::connectToLib ERROR: cannot dlopen camera wrapper library /data/app-lib/gms.rewards.selfie-2/libnative_camera_r4.4.0.so, dlerror="Cannot load library: soinfo_relocate(linker.cpp:975): cannot locate symbol "_ZN7android17IConsumerListenerD2Ev" referenced by "libnative_camera_r4.4.0.so"..."
D/OpenCV::camera﹕ try to load library 'libnative_camera_r4.3.0.so'
D/OpenCV::camera﹕ CameraWrapperConnector::connectToLib ERROR: cannot dlopen camera wrapper library /data/app-lib/gms.rewards.selfie-2/libnative_camera_r4.3.0.so, dlerror="Cannot load library: soinfo_relocate(linker.cpp:975): cannot locate symbol "_ZN7android6Camera17setPreviewTextureERKNS_2spINS_22IGraphicBufferProducerEEE" referenced by "libnative_camera_r4.3.0.so"..."
D/OpenCV::camera﹕ try to load library 'libnative_camera_r4.2.0.so'
D/OpenCV::camera﹕ Loaded library '/data/app-lib/gms.rewards.selfie-2/libnative_camera_r4.2.0.so'
D/OpenCV_NativeCamera﹕ CameraHandler::initCameraConnect(0x6a64eb6d, 1, 0x69359e18, 0x0)
D/OpenCV_NativeCamera﹕ Connecting to CameraService v 2.3
D/OpenCV_NativeCamera﹕ Instantiated new CameraHandler (0x6a64eb6d, 0x69359e18)
I/OpenCV_NativeCamera﹕ initCameraConnect: [antibanding=auto;antibanding-values=off,auto,50hz,60hz;auto-exposure-lock=false;auto-exposure-lock-supported=true;auto-exposure-lock-values=false,true;auto-whitebalance-lock=false;auto-whitebalance-lock-supported=true;auto-whitebalance-lock-values=false,true;effect=none;effect-values=mono,negative,none,posterize,sepia,aqua,solarize,nv-vivid,nv-emboss;exposure-compensation-step=0.1;flash-mode=off;flash-mode-values=off,on,auto,torch,red-eye;focal-length=1.000;focus-areas=(0,0,0,0,0);focus-distances=0.95,1.9,Infinity;focus-mode=fixed;focus-mode-values=fixed;horizontal-view-angle=60.000;jpeg-quality=95;jpeg-thumbnail-height=240;jpeg-thumbnail-quality=90;jpeg-thumbnail-size-values=0x0,320x240,240x320;jpeg-thumbnail-width=320;max-exposure-compensation=20;max-num-detected-faces-hw=10;max-num-focus-areas=0;max-num-metering-areas=4;max-zoom=28;metering-areas=(0,0,0,0,0);min-exposure-compensation=-20;picture-format=jpeg;picture-format-values=jpeg,jfif,exif,yuv420p,yuv420sp;picture-size=1280x960;pic
D/OpenCV_NativeCamera﹕ Supported Cameras: (null)
D/OpenCV_NativeCamera﹕ Supported Picture Sizes: 320x240,480x480,640x368,640x480,800x600,960x720,1024x768,1280x720,1280x752,1280x960,1440x1080,1600x1200,1836x1080,1920x1080,2048x1152,2048x1360,2048x1536,2592x1456,2592x1520,2592x1920,2592x1944
D/OpenCV_NativeCamera﹕ Supported Picture Formats: jpeg,jfif,exif,yuv420p,yuv420sp
D/OpenCV_NativeCamera﹕ Supported Preview Sizes: 176x144,320x240,352x288,480x480,640x480,704x576,720x408,720x480,720x576,768x432,800x448,960x720,1280x720,1280x752,1280x960,1360x720,1440x1080,1920x1080,1920x1088,1920x1440
D/OpenCV_NativeCamera﹕ Supported Preview Formats: yuv420p,yuv420sp
D/OpenCV_NativeCamera﹕ Supported Preview Frame Rates: 5,8,10,15,20,24,25,30
D/OpenCV_NativeCamera﹕ Supported Thumbnail Sizes: 0x0,320x240,240x320
D/OpenCV_NativeCamera﹕ Supported Whitebalance Modes: auto,incandescent,fluorescent,warm-fluorescent,daylight,cloudy-daylight,shade,twilight
D/OpenCV_NativeCamera﹕ Supported Effects: mono,negative,none,posterize,sepia,aqua,solarize,nv-vivid,nv-emboss
D/OpenCV_NativeCamera﹕ Supported Scene Modes: auto,action,portrait,landscape,beach,fireworks,night,night-portrait,snow,sports,steadyphoto,sunset,theatre,barcode
D/OpenCV_NativeCamera﹕ Supported Focus Modes: fixed
D/OpenCV_NativeCamera﹕ Supported Antibanding Options: off,auto,50hz,60hz
D/OpenCV_NativeCamera﹕ Supported Flash Modes: off,on,auto,torch,red-eye
D/OpenCV_NativeCamera﹕ initCameraConnect: preview format is set to yuv420sp
D/OpenCV_NativeCamera﹕ Starting preview
D/OpenCV_NativeCamera﹕ Preview started successfully

Example code (Not exactly the same code that I use):

  cv::Mat inframe = cv::Mat();
  cv::VideoCapture mCamera;
  mCamera.open(1);
  mCamera.set(CV_CAP_PROP_FRAME_WIDTH, 400);
  mCamera.set(CV_CAP_PROP_FRAME_HEIGHT, 300);

// mCamera.set("scene mode", "beach"); // <-- looking for a way

  while (mCamera.isOpened()) {
    bool grab = mCamera.grab();
    if (grab) {
      mCamera.retrieve(inframe, CV_CAP_ANDROID_COLOR_FRAME_RGB);

      // To do something for the iframe

    } else {
      LOGE("--->Cannot grab a frame from camera.");
    }
  }
  mCamera.release();
like image 739
wf9a5m75 Avatar asked Sep 29 '22 15:09

wf9a5m75


1 Answers

The only problem is that there isn't any standardized API for handling camera by Android NDK. It is necessary to go through Java, since NDK access will be specific to particular hardware.

Although there is a VideoCapture class in OpenCV's C++ interface, you can use it, if you implement Native Activity approach, but since NDK does not provide API for camera access from native code, so VideoCapture uses undocumented interfaces, that makes VideoCapture not universal. It does not work on some old or new devices. Just take a look at the issues on OpenCV's native camera here.

I suggest to use setSceneMode(String value) on managed side, you can also send a request from native side to managed to set a scene mode thereby making a conversion between the SCENE_MODE_# on managed side and corresponding enums on native side (you should implement yourself).

By the way here are the properties for Android cameras (of the VideoCapture's set() method): videoio.hpp #L263 - #L325


Update #1:

You can try to extend the functionalities of OpenCV's camera_wrapper by CameraParameters to set scene mode (like focus or white balance) but there could be differences between some Android versions.

Also take care that you have to compile the androidcamera module to all of the Android versions to get working on them. That's why you see a lot of libnative_camera_r#.#.#.so under the folder OPENCV_ANDROID_PATH\sdk\native\libs\armeabi-v7a.

like image 149
Kornel Avatar answered Oct 03 '22 07:10

Kornel