您能否展示如何通过 C++ 在 V4L2 中获取和设置 JPEG 压缩质量(使用 JPEG 像素格式)?
我可以检测到相机支持的各种像素格式以及相应的分辨率和帧率。我还可以选择它们并相应地捕获 JPEG 图像。但是我无法设置和获得 jpeg 质量。
我正在使用 Linux Mint 和罗技 c910 相机。
相机似乎暴露了这个参数,因为
v4l2-ctl --all
生产
JPEG Compression Controls
compression_quality (int) : min=50 max=87 step=1 default=75 value=75
[...]
然而,代码
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <iostream>
using namespace std;
int main()
{
int fd = open("/dev/video0", O_RDWR);
if (fd == -1)
{
perror("open: "); return 1;
}
{
// first option
v4l2_jpegcompression ctrl={0};
if(ioctl(fd, VIDIOC_G_JPEGCOMP, &ctrl)<0)
{
perror("VIDIOC_G_JPEGCOMP:");
}
else
{
cout<<"QUALITY:"<<ctrl.quality<<endl;
}
}
{
// second option
v4l2_ext_control extCtrl={0};
extCtrl.id = V4L2_CID_JPEG_COMPRESSION_QUALITY;
extCtrl.size = 0;
extCtrl.value = 100;
v4l2_ext_controls extCtrls={0};
extCtrls.controls = &extCtrl;
extCtrls.count = 1;
extCtrls.ctrl_class = V4L2_CTRL_CLASS_JPEG;
if(ioctl(fd, VIDIOC_G_EXT_CTRLS, &extCtrls)<0)
{
perror("VIDIOC_G_EXT_CTRLS:");
}
}
{
// third option
v4l2_ext_control extCtrl={0};
extCtrl.id = V4L2_CID_JPEG_COMPRESSION_QUALITY;
extCtrl.size = 0;
extCtrl.value = 100;
v4l2_ext_controls extCtrls={0};
extCtrls.controls = &extCtrl;
extCtrls.count = 1;
extCtrls.ctrl_class = V4L2_CID_JPEG_CLASS;
if(ioctl(fd, VIDIOC_G_EXT_CTRLS, &extCtrls)<0)
{
perror("VIDIOC_G_EXT_CTRLS V4L2_CID_JPEG_CLASS:");
}
}
close(fd);
return 0;
}
可以通过编译
g++ query.cpp -o query.out
产生:
VIDIOC_G_JPEGCOMP:: Inappropriate ioctl for device
VIDIOC_G_EXT_CTRLS:: Invalid argument
VIDIOC_G_EXT_CTRLS V4L2_CID_JPEG_CLASS:: Invalid argument