From 4691d98e1930f2a9fc40fee6dabf809c49dcfe24 Mon Sep 17 00:00:00 2001 From: "Anton V. Shokurov" Date: Sat, 18 Jul 2015 15:24:16 +0300 Subject: [PATCH] Adds support for property CV_CAP_PROP_FOCUS in functions cvSetCaptureProperty and cvGetCaptureProperty. In compliance with the Windows (DirectDraw) version autofocus is disabled when either of the abovementioned functions is called. --- modules/videoio/src/cap_libv4l.cpp | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/modules/videoio/src/cap_libv4l.cpp b/modules/videoio/src/cap_libv4l.cpp index accfc0f6b6..46fac95288 100644 --- a/modules/videoio/src/cap_libv4l.cpp +++ b/modules/videoio/src/cap_libv4l.cpp @@ -1433,6 +1433,30 @@ static double icvGetPropertyCAM_V4L (CvCaptureCAM_V4L* capture, sprintf(name, "Exposure"); capture->control.id = V4L2_CID_EXPOSURE; break; + case CV_CAP_PROP_FOCUS: { + struct v4l2_control c; + int v4l2_min; + int v4l2_max; + //we need to make sure that the autofocus is switch off, if available. + capture->control.id = V4L2_CID_FOCUS_AUTO; + v4l2_min = v4l2_get_ctrl_min(capture, capture->control.id); + v4l2_max = v4l2_get_ctrl_max(capture, capture->control.id); + if ( !((v4l2_min == -1) && (v4l2_max == -1)) ) { + //autofocus capability is supported, switch it off. + c.id = capture->control.id; + c.value = 0;//off + if( v4l2_ioctl(capture->deviceHandle, VIDIOC_S_CTRL, &c) != 0 ){ + if (errno != ERANGE) { + fprintf(stderr, "VIDEOIO ERROR: V4L2: Failed to set control \"%d\"(FOCUS_AUTO): %s (value %d)\n", c.id, strerror(errno), c.value); + return -1; + } + } + }//lack of support should not be considerred an error. + + sprintf(name, "Focus"); + capture->control.id = V4L2_CID_FOCUS_ABSOLUTE; + break; + } default: sprintf(name, ""); capture->control.id = property_id; @@ -1650,6 +1674,27 @@ static int icvSetControl (CvCaptureCAM_V4L* capture, int property_id, double val sprintf(name, "Exposure"); capture->control.id = V4L2_CID_EXPOSURE; break; + case CV_CAP_PROP_FOCUS: + //we need to make sure that the autofocus is switch off, if available. + capture->control.id = V4L2_CID_FOCUS_AUTO; + v4l2_min = v4l2_get_ctrl_min(capture, capture->control.id); + v4l2_max = v4l2_get_ctrl_max(capture, capture->control.id); + if ( !((v4l2_min == -1) && (v4l2_max == -1)) ) { + //autofocus capability is supported, switch it off. + c.id = capture->control.id; + c.value = 0;//off + if( v4l2_ioctl(capture->deviceHandle, VIDIOC_S_CTRL, &c) != 0 ){ + if (errno != ERANGE) { + fprintf(stderr, "VIDEOIO ERROR: V4L2: Failed to set control \"%d\"(FOCUS_AUTO): %s (value %d)\n", c.id, strerror(errno), c.value); + return -1; + } + } + }//lack of support should not be considerred an error. + + //now set the manual focus + sprintf(name, "Focus"); + capture->control.id = V4L2_CID_FOCUS_ABSOLUTE; + break; default: sprintf(name, ""); capture->control.id = property_id;