最近在做视频相关的东西,需要启用摄像头推流,用以下的方式可以判断摄像头是否被打开占用。
//检测摄像头是否被占用
QCamera *m_pCamera = new QCamera(this);
QCameraViewfinder *m_pViewfinder = new QCameraViewfinder(this);
QCameraImageCapture *m_pImageCapture = new QCameraImageCapture(m_pCamera);
m_pImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
m_pCamera->setCaptureMode(QCamera::CaptureStillImage);
m_pCamera->setViewfinder(m_pViewfinder);
m_pCamera->start();
if (!m_pImageCapture->isReadyForCapture())
{
qDebug() << "Camera already opening by other application.";
QMessageBox box(QMessageBox::Question, tr("提示"), tr("摄像头打开失败。"));
box.addButton(tr("确 定"), QMessageBox::YesRole);
box.exec();
}
m_pCamera->stop();
delete m_pCamera;
delete m_pViewfinder;
delete m_pImageCapture;