最新可用解决办法
- 不通过imageCaptured(int,QImage)获取图片, 改用imageSaved(int, const QString &)信号
connect(mImageCapture, SIGNAL(imageSaved(int, const QString &)), this, SLOT(cameraImageSaved(int, const QString &)));
问题
- mac中使用无法捕获此信号而跳转到槽函数
connect(mImageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(cameraImageCaptured(int,QImage)));
- mImageCapture->capture()函数已经调用, 但mImageCapture无法正确发出imageCaptured(int,QImage)信号, 经试验图片已经正常被捕获, 但没有发出信号, 猜测是mac上沙盒文件系统的原因
解决办法
- mac中必须设置viewfinder才能生效, 这点与Windows中不同
- 并改用imageSaved(int, const QString &)信号
完整代码
setWindowTitle("QCamera");
m_pCamera = new QCamera(this);
m_pViewfinder = new QCameraViewfinder(this);
m_pImageCapture = new QCameraImageCapture(m_pCamera);
QPushButton* button1 = new QPushButton("Capture");
QPushButton* button2 = new QPushButton("Exit");
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(m_pViewfinder);
mainLayout->addWidget(button1);
mainLayout->addWidget(button2);
// mac端: capture()函数 如果不传入参数, 默认会将捕获到的图片存在 /User/xxx/Picture/xxxxxx
connect(button1, SIGNAL(clicked()), m_pImageCapture, SLOT(capture()));
connect(button2, SIGNAL(clicked()), this, SLOT(exitBtnResponded()));
// 改用imageSaved(int, const QString &)信号
connect(m_pImageCapture, SIGNAL(imageSaved(int, const QString &)), this, SLOT(cameraImageSaved(int, const QString &)));
m_pImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
m_pCamera->setCaptureMode(QCamera::CaptureStillImage);
m_pCamera->setViewfinder(m_pViewfinder); // Mac中必须设置此项才能有效, 原因未知
m_pCamera->start();
// 函数
void Widget::exitBtnResponded()
{
m_pCamera->stop();
close();
}
void Widget::cameraImageSaved(int, const QString &name)
{
QString savepath = QFileDialog::getSaveFileName(this,"Save Capture","Capture","Image png(*.png);;Image jpg(*.jpg)");
if(!savepath.isEmpty()){
QImage(name).save(savepath, nullptr, 100);
}
}
Info.plist中添加条目
<key>NSCameraUsageDescription</key>
<string>${PRODUCT_NAME} ...a description of why the camera is used by your app...</string>