shun-iwasawa 31accf
#include "webcam.h"
shun-iwasawa 31accf
#include "tenv.h"
shun-iwasawa 31accf
shun-iwasawa 31accf
#ifdef WIN32
shun-iwasawa 31accf
#include <windows.h></windows.h>
shun-iwasawa 31accf
#include <mfobjects.h></mfobjects.h>
shun-iwasawa 31accf
#include <mfapi.h></mfapi.h>
shun-iwasawa 31accf
#include <mfidl.h></mfidl.h>
shun-iwasawa 31accf
#pragma comment(lib, "Mfplat.lib")
shun-iwasawa 31accf
#pragma comment(lib, "Mf.lib")
shun-iwasawa 31accf
#pragma comment(lib, "Mfreadwrite.lib")
shun-iwasawa 31accf
#pragma comment(lib, "mfuuid.lib")
shun-iwasawa 31accf
#pragma comment(lib, "shlwapi.lib")
shun-iwasawa 31accf
#endif
shun-iwasawa 31accf
shun-iwasawa 31accf
#include <qcamera></qcamera>
shun-iwasawa 31accf
#include <qcamerainfo></qcamerainfo>
shun-iwasawa 31accf
#include <qapplication></qapplication>
shun-iwasawa 31accf
shun-iwasawa 31accf
TEnv::IntVar StopMotionUseDirectShow("StopMotionUseDirectShow", 1);
shun-iwasawa 31accf
TEnv::IntVar StopMotionUseMjpg("StopMotionUseMjpg", 1);
shun-iwasawa 31accf
//-----------------------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
Webcam::Webcam() {
shun-iwasawa 31accf
  m_useDirectShow = StopMotionUseDirectShow;
shun-iwasawa 31accf
  m_useMjpg       = StopMotionUseMjpg;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
Webcam::~Webcam() {}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
QList<qcamerainfo> Webcam::getWebcams() {</qcamerainfo>
shun-iwasawa 31accf
  m_webcams.clear();
shun-iwasawa 31accf
  m_webcams = QCameraInfo::availableCameras();
shun-iwasawa 31accf
  return m_webcams;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setWebcam(QCamera* camera) { m_webcam = camera; }
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
bool Webcam::initWebcam(int index) {
shun-iwasawa 31accf
#ifdef WIN32
shun-iwasawa 31accf
  if (!m_useDirectShow) {
shun-iwasawa 31accf
    // the webcam order obtained from Qt isn't always the same order as
shun-iwasawa 31accf
    // the one obtained from OpenCV without DirectShow
shun-iwasawa 31accf
    translateIndex(index);
shun-iwasawa 31accf
    m_cvWebcam.open(m_webcamIndex);
shun-iwasawa 31accf
  } else {
shun-iwasawa 31accf
    m_webcamIndex = index;
shun-iwasawa 31accf
    m_cvWebcam.open(m_webcamIndex, cv::CAP_DSHOW);
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    return false;
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
#else
shun-iwasawa 31accf
  m_webcamIndex = index;
shun-iwasawa 31accf
  m_cvWebcam.open(index);
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    return false;
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
#endif
shun-iwasawa 31accf
  // mjpg is used by many webcams
shun-iwasawa 31accf
  // opencv runs very slow on some webcams without it.
shun-iwasawa 31accf
  if (m_useMjpg) {
shun-iwasawa 31accf
    m_cvWebcam.set(cv::CAP_PROP_FOURCC,
shun-iwasawa 31accf
                   cv::VideoWriter::fourcc('m', 'j', 'p', 'g'));
shun-iwasawa 31accf
    m_cvWebcam.set(cv::CAP_PROP_FOURCC,
shun-iwasawa 31accf
                   cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  m_cvWebcam.set(3, m_webcamWidth);
shun-iwasawa 31accf
  m_cvWebcam.set(4, m_webcamHeight);
shun-iwasawa 31accf
shun-iwasawa 31accf
  return true;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::releaseWebcam() { m_cvWebcam.release(); }
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
int Webcam::getIndexOfResolution() {
shun-iwasawa 31accf
  return m_webcamResolutions.indexOf(QSize(m_webcamWidth, m_webcamHeight));
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
bool Webcam::getWebcamImage(TRaster32P& tempImage) {
shun-iwasawa 31accf
  bool error = false;
shun-iwasawa 31accf
  cv::Mat imgOriginal;
shun-iwasawa 31accf
  cv::Mat imgCorrected;
shun-iwasawa 31accf
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    // mjpg is used by many webcams
shun-iwasawa 31accf
    // opencv runs very slow on some webcams without it.
shun-iwasawa 31accf
    if (m_useMjpg) {
shun-iwasawa 31accf
      m_cvWebcam.set(cv::CAP_PROP_FOURCC,
shun-iwasawa 31accf
                     cv::VideoWriter::fourcc('m', 'j', 'p', 'g'));
shun-iwasawa 31accf
      m_cvWebcam.set(cv::CAP_PROP_FOURCC,
shun-iwasawa 31accf
                     cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
    m_cvWebcam.set(3, m_webcamWidth);
shun-iwasawa 31accf
    m_cvWebcam.set(4, m_webcamHeight);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      error = true;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
shun-iwasawa 31accf
  bool blnFrameReadSuccessfully =
shun-iwasawa 31accf
      m_cvWebcam.read(imgOriginal);  // get next frame
shun-iwasawa 31accf
shun-iwasawa 31accf
  if (!blnFrameReadSuccessfully ||
shun-iwasawa 31accf
      imgOriginal.empty()) {  // if frame not read successfully
shun-iwasawa 31accf
    std::cout << "error: frame not read from webcam\n";
shun-iwasawa 31accf
    error = true;  // print error message to std out
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
shun-iwasawa 31accf
  if (!error) {
shun-iwasawa 31accf
    cv::cvtColor(imgOriginal, imgCorrected, cv::COLOR_BGR2BGRA);
shun-iwasawa 31accf
    cv::flip(imgCorrected, imgCorrected, 0);
shun-iwasawa 31accf
    int width  = m_cvWebcam.get(3);
shun-iwasawa 31accf
    int height = m_cvWebcam.get(4);
shun-iwasawa 31accf
    int size   = imgCorrected.total() * imgCorrected.elemSize();
shun-iwasawa 31accf
shun-iwasawa 31accf
    tempImage = TRaster32P(width, height);
shun-iwasawa 31accf
    // m_liveViewImage = TRaster32P(width, height);
shun-iwasawa 31accf
    tempImage->lock();
shun-iwasawa 31accf
    uchar* imgBuf  = imgCorrected.data;
shun-iwasawa 31accf
    uchar* rawData = tempImage->getRawData();
shun-iwasawa 31accf
    memcpy(rawData, imgBuf, size);
shun-iwasawa 31accf
    tempImage->unlock();
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (error) {
shun-iwasawa 31accf
    return false;
shun-iwasawa 31accf
  } else {
shun-iwasawa 31accf
    return true;
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setUseDirectShow(int state) {
shun-iwasawa 31accf
  m_useDirectShow         = state;
shun-iwasawa 31accf
  StopMotionUseDirectShow = state;
shun-iwasawa 31accf
  emit(useDirectShowSignal(state));
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setUseMjpg(bool on) {
shun-iwasawa 31accf
  m_useMjpg         = on;
shun-iwasawa 31accf
  StopMotionUseMjpg = int(on);
shun-iwasawa 31accf
  emit(useMjpgSignal(on));
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::clearWebcam() {
shun-iwasawa 31accf
  m_webcamDescription = QString();
shun-iwasawa 31accf
  m_webcamDeviceName  = QString();
shun-iwasawa 31accf
  m_webcamIndex       = -1;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::clearWebcamResolutions() { m_webcamResolutions.clear(); }
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::refreshWebcamResolutions() {
shun-iwasawa 31accf
  clearWebcamResolutions();
shun-iwasawa 31accf
  m_webcamResolutions = getWebcam()->supportedViewfinderResolutions();
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
bool Webcam::getWebcamAutofocusStatus() {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return false;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
    double value = m_cvWebcam.get(cv::CAP_PROP_AUTOFOCUS);
shun-iwasawa 31accf
    if (value > 0.0) {
shun-iwasawa 31accf
      return true;
shun-iwasawa 31accf
    } else {
shun-iwasawa 31accf
      return false;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return false;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
void Webcam::setWebcamAutofocusStatus(bool on) {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
    double value = on ? 1.0 : 0.0;
shun-iwasawa 31accf
    m_cvWebcam.set(cv::CAP_PROP_AUTOFOCUS, value);
shun-iwasawa 31accf
    value = m_cvWebcam.get(cv::CAP_PROP_AUTOFOCUS);
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
int Webcam::getWebcamFocusValue() {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return 0;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
    double value = m_cvWebcam.get(cv::CAP_PROP_FOCUS);
shun-iwasawa 31accf
    return static_cast<int>(value);</int>
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return 0.0;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setWebcamFocusValue(int value) {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  m_cvWebcam.set(cv::CAP_PROP_FOCUS, value);
shun-iwasawa 31accf
  value = m_cvWebcam.get(cv::CAP_PROP_FOCUS);
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
int Webcam::getWebcamExposureValue() {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return 0;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
    double value = m_cvWebcam.get(cv::CAP_PROP_EXPOSURE);
shun-iwasawa 31accf
    return static_cast<int>(value);</int>
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return 0.0;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setWebcamExposureValue(int value) {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  m_cvWebcam.set(cv::CAP_PROP_AUTO_EXPOSURE, 0.25);
shun-iwasawa 31accf
  m_cvWebcam.set(cv::CAP_PROP_EXPOSURE, value);
shun-iwasawa 31accf
  value = m_cvWebcam.get(cv::CAP_PROP_EXPOSURE);
shun-iwasawa 31accf
  getWebcamExposureValue();
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
int Webcam::getWebcamBrightnessValue() {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return 0;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
    double value = m_cvWebcam.get(cv::CAP_PROP_BRIGHTNESS);
shun-iwasawa 31accf
    return static_cast<int>(value);</int>
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return 0.0;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setWebcamBrightnessValue(int value) {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  m_cvWebcam.set(cv::CAP_PROP_BRIGHTNESS, value);
shun-iwasawa 31accf
  value = m_cvWebcam.get(cv::CAP_PROP_BRIGHTNESS);
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
int Webcam::getWebcamContrastValue() {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return 0;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
    double value = m_cvWebcam.get(cv::CAP_PROP_CONTRAST);
shun-iwasawa 31accf
    return static_cast<int>(value);</int>
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return 0.0;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setWebcamContrastValue(int value) {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  m_cvWebcam.set(cv::CAP_PROP_CONTRAST, value);
shun-iwasawa 31accf
  value = m_cvWebcam.get(cv::CAP_PROP_CONTRAST);
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
int Webcam::getWebcamGainValue() {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return 0;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
    double value = m_cvWebcam.get(cv::CAP_PROP_GAIN);
shun-iwasawa 31accf
    return static_cast<int>(value);</int>
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return 0.0;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setWebcamGainValue(int value) {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  m_cvWebcam.set(cv::CAP_PROP_GAIN, value);
shun-iwasawa 31accf
  value = m_cvWebcam.get(cv::CAP_PROP_GAIN);
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
int Webcam::getWebcamSaturationValue() {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return 0;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
    double value = m_cvWebcam.get(cv::CAP_PROP_SATURATION);
shun-iwasawa 31accf
    return static_cast<int>(value);</int>
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  return 0.0;
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::setWebcamSaturationValue(int value) {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  m_cvWebcam.set(cv::CAP_PROP_SATURATION, value);
shun-iwasawa 31accf
  value = m_cvWebcam.get(cv::CAP_PROP_SATURATION);
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
void Webcam::openSettingsWindow() {
shun-iwasawa 31accf
  if (m_cvWebcam.isOpened() == false) {
shun-iwasawa 31accf
    initWebcam(m_webcamIndex);
shun-iwasawa 31accf
shun-iwasawa 31accf
    if (!m_cvWebcam.isOpened()) {
shun-iwasawa 31accf
      return;
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  m_cvWebcam.set(cv::CAP_PROP_SETTINGS, 1.0);
shun-iwasawa 31accf
}
shun-iwasawa 31accf
shun-iwasawa 31accf
//-----------------------------------------------------------------
shun-iwasawa 31accf
shun-iwasawa 31accf
bool Webcam::translateIndex(int index) {
shun-iwasawa 31accf
  // We are using Qt to get the camera info and supported resolutions, but
shun-iwasawa 31accf
  // we are using OpenCV to actually get the images.
shun-iwasawa 31accf
  // The camera index from OpenCV and from Qt don't always agree,
shun-iwasawa 31accf
  // So this checks the name against the correct index.
shun-iwasawa 31accf
  m_webcamIndex = index;
shun-iwasawa 31accf
shun-iwasawa 31accf
#ifdef WIN32
shun-iwasawa 31accf
shun-iwasawa 6bac0f
  // Thanks to:
shun-iwasawa 6bac0f
  // https://elcharolin.wordpress.com/2017/08/28/webcam-capture-with-the-media-foundation-sdk/
shun-iwasawa 6bac0f
  // for the webcam enumeration here
shun-iwasawa 31accf
shun-iwasawa 31accf
#define CLEAN_ATTRIBUTES()                                                     \
shun-iwasawa 31accf
  if (attributes) {                                                            \
shun-iwasawa 31accf
    attributes->Release();                                                     \
shun-iwasawa 31accf
    attributes = NULL;                                                         \
shun-iwasawa 31accf
  }                                                                            \
shun-iwasawa 31accf
  for (DWORD i = 0; i < count; i++) {                                          \
shun-iwasawa 31accf
    if (&devices[i]) {                                                         \
shun-iwasawa 31accf
      devices[i]->Release();                                                   \
shun-iwasawa 31accf
      devices[i] = NULL;                                                       \
shun-iwasawa 31accf
    }                                                                          \
shun-iwasawa 31accf
  }                                                                            \
shun-iwasawa 31accf
  CoTaskMemFree(devices);                                                      \
shun-iwasawa 31accf
  return hr;
shun-iwasawa 31accf
shun-iwasawa 31accf
  HRESULT hr = S_OK;
shun-iwasawa 31accf
shun-iwasawa 31accf
  // this is important!!
shun-iwasawa 31accf
  hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
shun-iwasawa 31accf
shun-iwasawa 31accf
  UINT32 count              = 0;
shun-iwasawa 31accf
  IMFAttributes* attributes = NULL;
shun-iwasawa 31accf
  IMFActivate** devices     = NULL;
shun-iwasawa 31accf
shun-iwasawa 31accf
  if (FAILED(hr)) {
shun-iwasawa 31accf
    CLEAN_ATTRIBUTES()
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  // Create an attribute store to specify enumeration parameters.
shun-iwasawa 31accf
  hr = MFCreateAttributes(&attributes, 1);
shun-iwasawa 31accf
shun-iwasawa 31accf
  if (FAILED(hr)) {
shun-iwasawa 31accf
    CLEAN_ATTRIBUTES()
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
shun-iwasawa 31accf
  // The attribute to be requested is devices that can capture video
shun-iwasawa 31accf
  hr = attributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
shun-iwasawa 31accf
                           MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
shun-iwasawa 31accf
  if (FAILED(hr)) {
shun-iwasawa 31accf
    CLEAN_ATTRIBUTES()
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  // Enummerate the video capture devices
shun-iwasawa 31accf
  hr = MFEnumDeviceSources(attributes, &devices, &count);
shun-iwasawa 31accf
shun-iwasawa 31accf
  if (FAILED(hr)) {
shun-iwasawa 31accf
    CLEAN_ATTRIBUTES()
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  // if there are any available devices
shun-iwasawa 31accf
  if (count > 0) {
shun-iwasawa 31accf
    WCHAR* nameString = NULL;
shun-iwasawa 31accf
    // Get the human-friendly name of the device
shun-iwasawa 31accf
    UINT32 cchName;
shun-iwasawa 31accf
shun-iwasawa 31accf
    for (int i = 0; i < count; i++) {
shun-iwasawa 31accf
      hr = devices[i]->GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME,
shun-iwasawa 31accf
                                          &nameString, &cchName);
shun-iwasawa 31accf
      std::string desc = m_webcamDescription.toStdString();
shun-iwasawa 31accf
      if (nameString == m_webcamDescription.toStdWString()) {
shun-iwasawa 31accf
        m_webcamIndex = i;
shun-iwasawa 31accf
        break;
shun-iwasawa 31accf
      }
shun-iwasawa 31accf
      // devices[0]->ShutdownObject();
shun-iwasawa 31accf
    }
shun-iwasawa 31accf
shun-iwasawa 31accf
    CoTaskMemFree(nameString);
shun-iwasawa 31accf
  }
shun-iwasawa 31accf
  // clean
shun-iwasawa 31accf
  CLEAN_ATTRIBUTES()
shun-iwasawa 31accf
#else
shun-iwasawa 31accf
  return true;
shun-iwasawa 31accf
#endif
shun-iwasawa 31accf
}