Skip to content

Commit b33853c

Browse files
committed
Merge pull request opencv#4199 from AVshokurov:master
2 parents 587b0cc + 4691d98 commit b33853c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

modules/videoio/src/cap_libv4l.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,30 @@ static double icvGetPropertyCAM_V4L (CvCaptureCAM_V4L* capture,
14331433
sprintf(name, "Exposure");
14341434
capture->control.id = V4L2_CID_EXPOSURE;
14351435
break;
1436+
case CV_CAP_PROP_FOCUS: {
1437+
struct v4l2_control c;
1438+
int v4l2_min;
1439+
int v4l2_max;
1440+
//we need to make sure that the autofocus is switch off, if available.
1441+
capture->control.id = V4L2_CID_FOCUS_AUTO;
1442+
v4l2_min = v4l2_get_ctrl_min(capture, capture->control.id);
1443+
v4l2_max = v4l2_get_ctrl_max(capture, capture->control.id);
1444+
if ( !((v4l2_min == -1) && (v4l2_max == -1)) ) {
1445+
//autofocus capability is supported, switch it off.
1446+
c.id = capture->control.id;
1447+
c.value = 0;//off
1448+
if( v4l2_ioctl(capture->deviceHandle, VIDIOC_S_CTRL, &c) != 0 ){
1449+
if (errno != ERANGE) {
1450+
fprintf(stderr, "VIDEOIO ERROR: V4L2: Failed to set control \"%d\"(FOCUS_AUTO): %s (value %d)\n", c.id, strerror(errno), c.value);
1451+
return -1;
1452+
}
1453+
}
1454+
}//lack of support should not be considerred an error.
1455+
1456+
sprintf(name, "Focus");
1457+
capture->control.id = V4L2_CID_FOCUS_ABSOLUTE;
1458+
break;
1459+
}
14361460
default:
14371461
sprintf(name, "<unknown property string>");
14381462
capture->control.id = property_id;
@@ -1650,6 +1674,27 @@ static int icvSetControl (CvCaptureCAM_V4L* capture, int property_id, double val
16501674
sprintf(name, "Exposure");
16511675
capture->control.id = V4L2_CID_EXPOSURE;
16521676
break;
1677+
case CV_CAP_PROP_FOCUS:
1678+
//we need to make sure that the autofocus is switch off, if available.
1679+
capture->control.id = V4L2_CID_FOCUS_AUTO;
1680+
v4l2_min = v4l2_get_ctrl_min(capture, capture->control.id);
1681+
v4l2_max = v4l2_get_ctrl_max(capture, capture->control.id);
1682+
if ( !((v4l2_min == -1) && (v4l2_max == -1)) ) {
1683+
//autofocus capability is supported, switch it off.
1684+
c.id = capture->control.id;
1685+
c.value = 0;//off
1686+
if( v4l2_ioctl(capture->deviceHandle, VIDIOC_S_CTRL, &c) != 0 ){
1687+
if (errno != ERANGE) {
1688+
fprintf(stderr, "VIDEOIO ERROR: V4L2: Failed to set control \"%d\"(FOCUS_AUTO): %s (value %d)\n", c.id, strerror(errno), c.value);
1689+
return -1;
1690+
}
1691+
}
1692+
}//lack of support should not be considerred an error.
1693+
1694+
//now set the manual focus
1695+
sprintf(name, "Focus");
1696+
capture->control.id = V4L2_CID_FOCUS_ABSOLUTE;
1697+
break;
16531698
default:
16541699
sprintf(name, "<unknown property string>");
16551700
capture->control.id = property_id;

0 commit comments

Comments
 (0)