QCamera linux与windows区别

Qt简单调用摄像头:

QCamera:相机操作类

QCameraViewfinder:显示窗口,执行构造函数时可传入QVideoWidget窗口类(由QWidget提升而来)

QCameraImageCapture:拍照类

1、初始化取景器

  view_finder_ = std::make_shared(ui.show_widget);
  view_finder_->resize(ui.show_widget->size());

2、选取拍照摄像头

  QList infos = QCameraInfo::availableCameras();

通过设备名称初始化相机(或直接new QCamera()使用默认相机)

  new QCamera(infos.first().deviceName().toUtf8(), this);
  camera_->setViewfinder(view_finder_.get());
  camera_->load();

QCamera获取/配置支持取景器参数(需要在相机load之后)

  QList size = camera_->supportedViewfinderResolutions();
  QCameraViewfinderSettings setting = camera_->viewfinderSettings();
  setting.setResolution(800, 600);
  camera_->setViewfinderSettings(setting);

3、配置拍照参数()

  image_capture_.reset(new QCameraImageCapture(camera_.get()));
//   bool support = image_capture_->isCaptureDestinationSupported(QCameraImageCapture::CaptureToBuffer);
  image_capture_->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
  image_capture_->setBufferFormat(QVideoFrame::PixelFormat::Format_Jpeg);
  connect(image_capture_.get(), &QCameraImageCapture::imageSaved, this, &Dialog::OnImageSaved);
  camera_->start();

4、拍照

  if (camera_ && image_capture_) {
    //camera上锁
    camera_->searchAndLock();
    image_capture_->capture(path);
    camera_->unlock();
  }

Windows下和linux下区别:

linux下需要camera被加载成功status改变才可以start,所以绑定信号在槽函数中操作,防止其他status影响,在loaded后,解绑信号。

而windows 下camera load后直接开始设置并start(),通过信号槽反而会失败。

void Init() {
  QList infos = QCameraInfo::availableCameras();
  if (infos.size() <= 0) {
    return StartCameraFailed(tr("未找到可用摄像头"));
  }

  camera_.reset(new QCamera(this));
  camera_->setViewfinder(view_finder_.get());

  image_capture_.reset(new QCameraImageCapture(camera_.get()));
  connect(image_capture_.get(), &QCameraImageCapture::imageSaved, this, &PhotographDialog::OnImageSaved);

  ui.photograph_pb->setEnabled(false);
  connect(camera_.get(), static_cast(&QCamera::error),
    [=](QCamera::Error value) { return StartCameraFailed(QString(tr("摄像头打开失败,错误码:%1")).arg(value)); });
  camera_->load();

#ifdef Q_OS_WIN
  CameraStatusChanged(QCamera::LoadedStatus);
#else
  connect(camera_.get(), &QCamera::statusChanged, this, &Dialog::CameraStatusChanged);
#endif
}

 load成功后,根据支持分辨率设置合适的尺寸。

 status和state是两种信号。

        status用于判断是否loaded,下一步是start。

        state用于判断相机是否active,下一步是拍照。

void CameraStatusChanged(QCamera::Status status) {
  if (QCamera::LoadedStatus != status) {
    return;
  }
  //std::sort(resolutions.begin(), resolutions.end(), qt_sizeLessThan);
  QCameraViewfinderSettings setting = camera_->viewfinderSettings();
  QList size_list = camera_->supportedViewfinderResolutions();
  if (size_list.size() <= 0) {
    return StartCameraFailed(tr("摄像头未开启"));
  }
  disconnect(camera_.get(), &QCamera::statusChanged, this, &Dialog::CameraStatusChanged);

  QSize simlar_size = FindSimlarSize(size_list, QSize(0, 0));
  setting.setResolution(simlar_size);
  camera_->setViewfinderSettings(setting);

  ResizeShowWidget(simlar_size.width(), simlar_size.height());

  connect(camera_.get(), &QCamera::stateChanged, this, &Dialog::CameraStateChanged);
  start_timer_.start();
  camera_->start();
}

你可能感兴趣的:(Qt,qt,开发语言)