我这里是需要可以设定摄像头普清、高清、超清,摄像头一般会支持多个分辨率,然后从中选相对应的分比率出来。这里用一个简单的算法进行筛选;就是自己设定一组普清、高清、超清对应的分辨率,然后拿对应的分辨率和支持的分辨率作对比,对比的方式是分辨率的长宽相乘后取两个分辨率的差值,然后取到最接近的那一个
贴代码
QString selectCameraResolution(int type, QStringList cameraResolutionList)
{
QSize perfectLow(640,480);
QSize perfectMiddle(1280,720);
QSize perfectHigh(1920, 1080);
int refer = 0;
switch (type) {
case 0:
refer = perfectLow.width()*perfectLow.height();
break;
case 1:
refer = perfectMiddle.width()*perfectMiddle.height();
break;
case 2:
refer = perfectHigh.width()*perfectHigh.height();
break;
default:
break;
}
QMap<int,int> map; //<差值,索引>
int res;
for(int i = 0;i<cameraResolutionList.size();i++) {
QString asize = cameraResolutionList.at(i);
res = asize.section("x",0, 0).toInt()*asize.section("x",1, 1).toInt();
map.insert(abs(refer-res),i);
}//map升序
int index = map.first();
return cameraResolutionList.at(index);//差值最小的,就是最接近的
}