camera: Single/Double (QtMultimediaCamera)

This commit is contained in:
zhupengfei 2018-06-02 22:13:54 +08:00
parent 3cb91338e9
commit 341c07156a
No known key found for this signature in database
GPG key ID: 85B82A3E62174206
4 changed files with 23 additions and 8 deletions

View file

@ -48,7 +48,7 @@ bool QtCameraSurface::present(const QVideoFrame& frame) {
QtMultimediaCamera::QtMultimediaCamera(const std::string& camera_name, QtMultimediaCamera::QtMultimediaCamera(const std::string& camera_name,
const Service::CAM::Flip& flip) const Service::CAM::Flip& flip)
: QtCameraInterface(flip), handler(QtMultimediaCameraHandler::GetHandler()) { : QtCameraInterface(flip), handler(QtMultimediaCameraHandler::GetHandler(camera_name)) {
if (handler->thread() == QThread::currentThread()) { if (handler->thread() == QThread::currentThread()) {
handler->CreateCamera(camera_name); handler->CreateCamera(camera_name);
} else { } else {
@ -94,7 +94,7 @@ void QtMultimediaCamera::SetFrameRate(Service::CAM::FrameRate frame_rate) {
auto framerate = FrameRateList[static_cast<int>(frame_rate)]; auto framerate = FrameRateList[static_cast<int>(frame_rate)];
handler->settings.setMinimumFrameRate(framerate.minimumFrameRate); handler->settings.setMinimumFrameRate(framerate.minimumFrameRate);
handler->settings.setMinimumFrameRate(framerate.maximumFrameRate); handler->settings.setMaximumFrameRate(framerate.maximumFrameRate);
} }
QImage QtMultimediaCamera::QtReceiveFrame() { QImage QtMultimediaCamera::QtReceiveFrame() {
@ -115,17 +115,25 @@ std::array<std::shared_ptr<QtMultimediaCameraHandler>, 3> QtMultimediaCameraHand
std::array<bool, 3> QtMultimediaCameraHandler::status; std::array<bool, 3> QtMultimediaCameraHandler::status;
std::unordered_map<std::string, std::shared_ptr<QtMultimediaCameraHandler>>
QtMultimediaCameraHandler::loaded;
void QtMultimediaCameraHandler::Init() { void QtMultimediaCameraHandler::Init() {
for (auto& handler : handlers) { for (auto& handler : handlers) {
handler = std::make_shared<QtMultimediaCameraHandler>(); handler = std::make_shared<QtMultimediaCameraHandler>();
} }
} }
std::shared_ptr<QtMultimediaCameraHandler> QtMultimediaCameraHandler::GetHandler() { std::shared_ptr<QtMultimediaCameraHandler> QtMultimediaCameraHandler::GetHandler(
const std::string& camera_name) {
if (loaded.count(camera_name)) {
return loaded.at(camera_name);
}
for (int i = 0; i < handlers.size(); i++) { for (int i = 0; i < handlers.size(); i++) {
if (!status[i]) { if (!status[i]) {
NGLOG_INFO(Service_CAM, "Successfully got handler {}", i); NGLOG_INFO(Service_CAM, "Successfully got handler {}", i);
status[i] = true; status[i] = true;
loaded.emplace(camera_name, handlers[i]);
return handlers[i]; return handlers[i];
} }
} }
@ -140,6 +148,12 @@ void QtMultimediaCameraHandler::ReleaseHandler(
NGLOG_INFO(Service_CAM, "Successfully released handler {}", i); NGLOG_INFO(Service_CAM, "Successfully released handler {}", i);
status[i] = false; status[i] = false;
handlers[i]->started = false; handlers[i]->started = false;
for (auto it = loaded.begin(); it != loaded.end(); it++) {
if (it->second == handlers[i]) {
loaded.erase(it);
break;
}
}
break; break;
} }
} }

View file

@ -6,6 +6,7 @@
#include <array> #include <array>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
#include <QAbstractVideoSurface> #include <QAbstractVideoSurface>
#include <QCamera> #include <QCamera>
@ -62,7 +63,7 @@ class QtMultimediaCameraHandler final : public QObject {
public: public:
/// Creates the global handler. Must be called in UI thread. /// Creates the global handler. Must be called in UI thread.
static void Init(); static void Init();
static std::shared_ptr<QtMultimediaCameraHandler> GetHandler(); static std::shared_ptr<QtMultimediaCameraHandler> GetHandler(const std::string& camera_name);
static void ReleaseHandler(const std::shared_ptr<QtMultimediaCameraHandler>& handler); static void ReleaseHandler(const std::shared_ptr<QtMultimediaCameraHandler>& handler);
/** /**
@ -92,6 +93,7 @@ private:
static std::array<std::shared_ptr<QtMultimediaCameraHandler>, 3> handlers; static std::array<std::shared_ptr<QtMultimediaCameraHandler>, 3> handlers;
static std::array<bool, 3> status; static std::array<bool, 3> status;
static std::unordered_map<std::string, std::shared_ptr<QtMultimediaCameraHandler>> loaded;
friend class QtMultimediaCamera; // For access to camera_surface (and camera) friend class QtMultimediaCamera; // For access to camera_surface (and camera)
}; };

View file

@ -24,7 +24,7 @@ bool StillImageCamera::IsPreviewAvailable() {
return !image.isNull(); return !image.isNull();
} }
const std::string StillImageCameraFactory::getFilePath() { const std::string StillImageCameraFactory::GetFilePath() {
QList<QByteArray> types = QImageReader::supportedImageFormats(); QList<QByteArray> types = QImageReader::supportedImageFormats();
QList<QString> temp_filters; QList<QString> temp_filters;
for (QByteArray type : types) { for (QByteArray type : types) {
@ -32,7 +32,6 @@ const std::string StillImageCameraFactory::getFilePath() {
} }
QString filter = QObject::tr("Supported image files (%1)").arg(temp_filters.join(" ")); QString filter = QObject::tr("Supported image files (%1)").arg(temp_filters.join(" "));
return QFileDialog::getOpenFileName(nullptr, QObject::tr("Open File"), ".", filter) return QFileDialog::getOpenFileName(nullptr, QObject::tr("Open File"), ".", filter)
.toStdString(); .toStdString();
} }
@ -41,7 +40,7 @@ std::unique_ptr<CameraInterface> StillImageCameraFactory::Create(
const std::string& config, const Service::CAM::Flip& flip) const { const std::string& config, const Service::CAM::Flip& flip) const {
std::string real_config = config; std::string real_config = config;
if (config.empty()) { if (config.empty()) {
real_config = getFilePath(); real_config = GetFilePath();
} }
QImage image(QString::fromStdString(real_config)); QImage image(QString::fromStdString(real_config));
if (image.isNull()) { if (image.isNull()) {

View file

@ -31,7 +31,7 @@ public:
const Service::CAM::Flip& flip) const override; const Service::CAM::Flip& flip) const override;
private: private:
static const std::string getFilePath(); static const std::string GetFilePath();
}; };
} // namespace Camera } // namespace Camera