QCameraInfo and OpenCV
Unsolved
General and Desktop
-
Dear Community,
I have a problem:
It is my Settings Widget. I have Audio In, Audio Out and Video In (Webcam).
opencv is working. But how can I open cv::VideoCapture with the Device Name of QCameraInfo? I am using Visual Studio Community 2019 and Windows 10.
VListWidgetItem is similar to QListWidgetItem but instead of using QVariant it uses std::any to store data.KSecureSettings.hpp
#pragma once #include <QWidget> #include "ui_KSecureSettings.h" #include<InternationalizationLib/language.hpp> #include<QAudioDeviceInfo> #undef max #include"toml.hpp" #include"VListWidgetItem.hpp" #include<QCameraInfo> #include<qpoint.h> #include<qmenu.h> #include<QSound> #include<QAudioOutput> #include<qfile.h> #include<opencv2/opencv.hpp> #include<QMessageBox> class KSecureSettings : public QWidget { Q_OBJECT public: KSecureSettings(const lang::Language& lang,toml::table* table, QWidget *parent = Q_NULLPTR); ~KSecureSettings(); protected: void setupLanguage(); void setupSettings(); void setupCallbacks(); void testSound(); void webcamtest(); void outputContextMenu(const QPoint& pos); void webcamContextMenu(const QPoint& pos); void tabChanged(int index); private: Ui::KSecureSettings ui; const lang::Language& language; toml::table* settings = nullptr; QAudioOutput* output = nullptr; QFile* file = nullptr; };
and
KSecureSettings.cpp#include "KSecureSettings.hpp" #ifdef _WIN32 #ifdef _DEBUG #pragma comment(lib,"opencv_world430d.lib") #else #pragma comment(lib,"opencv_world430.lib") #endif #endif KSecureSettings::KSecureSettings(const lang::Language& lang,toml::table* table, QWidget *parent) : language(lang),settings(table), QWidget(parent) { ui.setupUi(this); this->setupLanguage(); this->setupSettings(); this->setupCallbacks(); } KSecureSettings::~KSecureSettings() { } void KSecureSettings::setupLanguage() { this->setWindowTitle(QString::fromStdU32String(this->language["OptionsForm"]["Caption"]["title"])); this->ui.btnOK->setText(QString::fromStdU32String(this->language["OptionsForm"]["Caption"]["btnok"])); this->ui.btnCancel->setText(QString::fromStdU32String(this->language["OptionsForm"]["Caption"]["btncancel"])); this->ui.tabWidget->setTabText(0, QString::fromStdU32String(this->language["OptionsForm"]["Tabs"]["one"])); this->ui.tabWidget->setTabText(1, QString::fromStdU32String(this->language["OptionsForm"]["Tabs"]["two"])); this->ui.tabWidget->setTabText(2, QString::fromStdU32String(this->language["OptionsForm"]["Tabs"]["three"])); this->ui.tabWidget->setTabText(3, QString::fromStdU32String(this->language["OptionsForm"]["Tabs"]["four"])); this->ui.grp_stun->setTitle(QString::fromStdU32String(this->language["OptionsForm"]["Stun"]["grp_stun"])); this->ui.grp_nat->setTitle(QString::fromStdU32String(this->language["OptionsForm"]["NAT"]["grp_nat"])); this->ui.lbl_ipv4->setText(QString::fromStdU32String(this->language["OptionsForm"]["Stun"]["ipv4"])); this->ui.lbl_ipv6->setText(QString::fromStdU32String(this->language["OptionsForm"]["Stun"]["ipv6"])); this->ui.lbl_port->setText(QString::fromStdU32String(this->language["OptionsForm"]["Stun"]["port"])); this->ui.lbl_ipv4_2->setText(this->ui.lbl_ipv4->text()); this->ui.lbl_ipv6_2->setText(this->ui.lbl_ipv6->text()); this->ui.lbl_port_2->setText(this->ui.lbl_port->text()); this->ui.lbl_input->setText(QString::fromStdU32String(this->language["OptionsForm"]["Multimedia"]["lbl_input"])); this->ui.lbl_output->setText(QString::fromStdU32String(this->language["OptionsForm"]["Multimedia"]["lbl_output"])); this->ui.lbl_webcam->setText(QString::fromStdU32String(this->language["OptionsForm"]["Multimedia"]["lbl_webcam"])); } void KSecureSettings::setupSettings() { const auto& sys = *this->settings; this->ui.txt_ip4stun->setText(QString::fromStdString(sys["server"]["stun"]["ipv4"].value_or<std::string>(""))); this->ui.txt_ip6stun->setText(QString::fromStdString(sys["server"]["stun"]["ipv6"].value_or<std::string>(""))); this->ui.txt_portstun->setText(QString::number(sys["server"]["stun"]["port"].value_or<uint32_t>(0))); // this->ui.txt_ip4nat->setText(QString::fromStdString(sys["server"]["nat"]["ipv4"].value_or<std::string>(""))); this->ui.txt_ip6nat->setText(QString::fromStdString(sys["server"]["nat"]["ipv6"].value_or<std::string>(""))); this->ui.txt_portnat->setText(QString::number(sys["server"]["nat"]["port"].value_or<uint32_t>(0))); auto ilist = QAudioDeviceInfo::availableDevices(QAudio::Mode::AudioInput); auto olist = QAudioDeviceInfo::availableDevices(QAudio::Mode::AudioOutput); for (auto& item : ilist) { auto* li = new VListWidgetItem(QIcon(":/KSecureDesktopQT/speaker.png"),item.deviceName(),item); this->ui.lw_input->addItem(li); } for (auto& item : olist) { auto* li = new VListWidgetItem(QIcon(":/KSecureDesktopQT/speaker.png"), item.deviceName(), item); this->ui.lw_output->addItem(li); } auto vlist = QCameraInfo::availableCameras(); for (auto& item : vlist) { auto* li = new VListWidgetItem(QIcon(":/KSecureDesktopQT/camera.png"), item.deviceName(), item); this->ui.lw_webcam->addItem(li); } } void KSecureSettings::setupCallbacks() { connect(this->ui.lw_output, &QListWidget::customContextMenuRequested, this, &KSecureSettings::outputContextMenu); connect(this->ui.lw_webcam, &QListWidget::customContextMenuRequested, this, &KSecureSettings::webcamContextMenu); connect(this->ui.tabWidget, &QTabWidget::currentChanged, this, &KSecureSettings::tabChanged); } void KSecureSettings::testSound() { auto* item = this->ui.lw_output->selectedItems()[0]; //QSound* s = new QSound("testsound.wav"); //s->play(); auto* citem = static_cast<VListWidgetItem*>(item); auto dev = citem->getData<QAudioDeviceInfo>(); auto name = dev.deviceName(); QAudioFormat format; format.setCodec("audio/pcm"); format.setSampleRate(8000); format.setChannelCount(1); format.setSampleSize(8); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); this->output = new QAudioOutput(dev, format); this->file = new QFile(":/KSecureDesktopQT/testsound.wav"); this->file->open(QIODevice::ReadOnly); this->output->start(this->file); } void KSecureSettings::webcamtest() { using namespace cv; VideoCapture cap; if (!cap.open(0)) { QMessageBox::critical(nullptr, "ERROR", "Could not open Video Input"); return; } for (;;) { Mat frame; cap >> frame; if (frame.empty()) break; //Video empty; imshow("TEST", frame); if (waitKey(10) == 27) break; } } void KSecureSettings::outputContextMenu(const QPoint& pos) { QMenu ctx; ctx.addAction(QString::fromStdU32String(this->language["OptionsForm"]["ContextOutput"]["testsound"]), [&]() {this->testSound(); }); QAction *stop = new QAction(QString::fromStdU32String(this->language["OptionsForm"]["ContextOutput"]["stopsound"])); connect(stop, &QAction::triggered, this, [&]() {this->output->stop(); }); if (!this->output) stop->setEnabled(false); ctx.addAction(stop); ctx.exec(QCursor::pos()); } void KSecureSettings::webcamContextMenu(const QPoint& pos) { QMenu ctx; ctx.addAction(QString::fromStdU32String(this->language["OptionsForm"]["ContextVideo"]["webcamtest"]), [&]() {this->webcamtest(); }); ctx.exec(QCursor::pos()); } void KSecureSettings::tabChanged(int index) { if (index != 3) return; if (this->ui.lw_webcam->selectedItems().count() < 1) return; auto dev = (static_cast<VListWidgetItem*>((this->ui.lw_webcam->selectedItems()[0]))->getData<QCameraInfo>()); cv::VideoCapture cap(dev.deviceName().toStdString()); bool t = cap.isOpened(); }
-
Hi and welcome to devnet,
You should rather enumerate the cameras with OpenCV as well so you will be consistent to access them.