1. 问题
使用QML的Camera
组件创建相机。需要配置曝光时间,使用CameraExposure
中的exposureCompensation
,exposureMode
配置无效果,原因可能是不支持USB相机。
有两种方法经测试有效果:
- 命令行调用
v4l2-ctl
命令的方法,使用QProcess::execute()
函数 - 使用
ioctl()
的方式
2. v4l2-ctl方式
2.1 .h文件
#ifndef CAMERATOOL_H
#define CAMERATOOL_H#include <QObject>
#include <QtCore>//ioctl方式需要的头文件
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>class CameraTool : public QObject
{Q_OBJECT
public:explicit CameraTool(QObject *parent = nullptr);public:Q_INVOKABLE void setExposure(const int exposure);Q_INVOKABLE void setAutoExposure(const bool en);};#endif // CAMERATOOL_H
2.2 .cpp文件
#include "cameratool.h"CameraTool::CameraTool(QObject *parent)
{}//方法1,命令行方式, execute()
void CameraTool::setExposure(const int exposure)
{//注意:exposure_absolute 根据自己的系统来,查看方式:v4l2-ctl -d /dev/video9 --list-ctrlsstd::string cmd = "v4l2-ctl -d /dev/video9 -c exposure_absolute=" + std::to_string(exposure);const char* cmdc = cmd.c_str();QProcess::execute(cmdc);return;
}void CameraTool::setAutoExposure(const bool en)
{QStringList arg;//注意:exposure_auto 根据自己的系统来,查看方式:v4l2-ctl -d /dev/video9 --list-ctrlsarg.clear();if(en == true){arg << "-d" << "/dev/video9" << "-c" << "exposure_auto=3";}else{arg << "-d" << "/dev/video9" << "-c" << "exposure_auto=1";}QProcess::execute("v4l2-ctl", arg);return;
}
3. ioctl()方式
3.1 .h文件
与上一节的头文件一样。
3.2 .cpp文件
#include "cameratool.h"CameraTool::CameraTool(QObject *parent)
{}//方法2,ioctl方式, ioctl()
void CameraTool::setExposure(const int exposure)
{v4l2_control control;int fd = open("/dev/video9", O_RDWR);if(fd < 0){printf("open camera failed\n");return;}control.id = V4L2_CID_EXPOSURE_ABSOLUTE;control.value = exposure;ioctl(fd, VIDIOC_S_CTRL, &control);close(fd);return ;
}void CameraTool::setAutoExposure(const bool en)
{v4l2_control control;int fd = open("/dev/video9", O_RDWR);if(fd < 0){printf("open camera failed\n");return;}control.id = V4L2_CID_EXPOSURE_AUTO;if(en == true){control.value = V4L2_EXPOSURE_APERTURE_PRIORITY;}else{control.value = V4L2_EXPOSURE_MANUAL;}ioctl(fd, VIDIOC_S_CTRL, &control);close(fd);return ;
}
参考:
调节UVC相机参数只需要六行代码