Camera概述
Qt 多媒体API提供了许多摄像机相关的类,可以通过移动设备摄像机或者网络摄像机采集视频或者图像。对于通常任务来说,Qt提供了C++和QML两种实现API。
Camera 特点
为了使用camera类,快速浏览一下摄像头是如何工作的是有必要的。如果你已经很熟悉了,你可以路过这部分,直接进入Cameraimplementation details.
检测和选择摄像头设备
在使用camera APIs之前,你应该检查一下摄像头是否可用。如果没有,你应该在你的应用中禁用摄像头相关的特性,使用QCameraInfo::availableCameras()函数检查,示例代码如下:
bool checkCameraAvailability()
{
if(QCameraInfo::availableCameras().count() > 0)
returntrue;
else
returnfalse;
}
QML中,使用QtMultimedia.availableCameras属性:
Item {
property boolisCameraAvailable: QtMultimedia.availableCameras.length > 0
}
检测完摄像头是否可用之后,使用在C++中使用QCamera访问它,或者在QML中使用Camera访问它
如果有多个摄像头,你可以指定哪一个使用:
In C++
QList<QCameraInfo>cameras = QCameraInfo::availableCameras();
foreach (constQCameraInfo &cameraInfo, cameras) {
if(cameraInfo.deviceName() == "mycamera")
camera =new QCamera(cameraInfo);
}
In QML,你可以设置CameradeviceId属性。所有可用的IDs可以从
QtMultimedia.availableCameras获取:
Camera{
deviceId:QtMultimedia.availableCameras[0].deviceId
}
也可以通过物理位置选择使用哪个摄像头,这在移动设备上很有用,因为移动设备通常有一个前置摄像头和一个后置摄像头。
In C++:
camera = new QCamera(QCamera::FrontFace);
In QML:
Camera {
position: Camera.FrontFace
}
如果既不指定设备ID也不指定位置,将使用默认摄像头,在桌面平台上,默认摄像头是在系统设置中设定的。在移动设备上,后置摄像头通过是默认摄像头。你也可以通过使用
QCameraInfo::defaultCamera() in C++ or QtMultimedia.defaultCamera in QML.获取默认摄像头。
Viewfinder
大多数数字摄像机允许输出一个低分辨率的图像用于录像或者截图,或者切换到一个更高分辨率模式用于截图。
依赖于使用QML还是C++,在QML中你可以使用Camera和VideoOutput来显示一下简单的viewfinder:
VideoOutput {
source: camera
Camera {
id: camera
// You can adjust various settings in here
}
}
在C++中,你可以使用Widget或者QGraphicsView,QCameraViewfinder类用在Widget场景中,QGrahpicsVideoItem用于QGraphicsView.
camera = new QCamera;
viewfinder = new QCameraViewfinder;
camera->setViewfinder(viewfinder);
viewfinder->show();
camera->start(); // to start the viewfinder
对于高级用法(比如处理viewfinder 帧,检测目标或者patterns),你也可以从QAbstractVideoSurface继承然后设置为viewfinder给QCamera object,在这种情况下你需要自己渲染viewfinder图像。
camera = new QCamera;
mySurface = newMyVideoSurface;
camera->setViewfinder(mySurface);
camera->start();
//MyVideoSurface::present(..) will be called with viewfinder frames
在移动设备上,viewfinder图像可能不是你期望的方向,摄像机传感器在这些设备上通常以横幅安装然而通常屏幕的自然方向是垂直的。这就导致图像显示靠边或者转向。为了正常反映真实看到的,你应该确认viewfinder帧总是旋转成正确的方向,考虑传感器的方向和当前 显示的方向。
// Assuming a QImage has beencreated from the QVideoFrame that needs to be presented
QImage videoFrame;
QCameraInfo cameraInfo(camera); // needed to get the camera sensor position and orientation
// Get the current display orientation
const QScreen *screen = QGuiApplication::primaryScreen();
const int screenAngle = screen->angleBetween(screen->nativeOrientation(), screen->orientation());
int rotation;
if (cameraInfo.position() == QCamera::BackFace) {
rotation = (cameraInfo.orientation() - screenAngle) % 360;
} else {
// Front position, compensate the mirror
rotation = (360 - cameraInfo.orientation() + screenAngle) % 360;
}
// Rotate the frame so it always shows in the correct orientation
videoFrame = videoFrame.transformed(QTransform().rotate(rotation));
Still Images
设置完viewfinder之后找到一些好看的,抓取一张图片,我们需要初始化一个新的QCameraimageCapture实例,然后需要启动摄像头,锁定它目标正好对焦,设置不同于viewfinder当图像采集时,采集图像,最终解锁摄像头给下一次拍照。
imageCapture = new QCameraImageCapture(camera);
camera->setCaptureMode(QCamera::CaptureStillImage);
camera->start(); // Viewfinder frames start flowing
//on half pressedshutter button
camera->searchAndLock();
//on shutterbutton pressed
imageCapture->capture();
//on shutterbutton released
camera->unlock();