wang 2 недель назад
Родитель
Сommit
47edc3d5fc
4 измененных файлов с 600 добавлено и 0 удалено
  1. 177 0
      src/cameradialog.cpp
  2. 31 0
      src/cameradialog.h
  3. 344 0
      src/cameradialog.ui
  4. 48 0
      src/camerafactory.cpp

+ 177 - 0
src/cameradialog.cpp

@@ -0,0 +1,177 @@
+#include "cameradialog.h"
+#include "ui_cameradialog.h"
+#include "cameratypedialog.h"
+#include "camerafactory.h"
+#include "cameraClass.h"
+#include <QMessageBox>
+#include <QFile>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QFileDialog>
+
+CameraDialog::CameraDialog(QWidget *parent) :
+    QDialog(parent),
+    ui(new Ui::CameraDialog)
+{
+    ui->setupUi(this);
+    // Load camera config
+    loadCameraConfig();
+}
+
+CameraDialog::~CameraDialog()
+{
+    delete ui;
+}
+
+void CameraDialog::on_okButton_clicked()
+{
+    // Save config
+    accept();
+}
+
+void CameraDialog::on_cancelButton_clicked()
+{
+    // Cancel operation
+    reject();
+}
+
+void CameraDialog::on_pushButton_add_clicked()
+{
+    CameraTypeDialog dialog(this);
+    if (dialog.exec() == QDialog::Accepted) {
+        CameraClass::CameraBrand brand = dialog.getSelectedBrand();
+        CameraClass::CameraType type = dialog.getSelectedType();
+        QString name = dialog.getCameraName();
+        QString device = dialog.getSelectedDevice();
+
+        CameraClass* camera = nullptr;
+        camera = CameraFactory::createCamera(brand, type, name);
+        
+        if (!device.isEmpty()) {
+            ui->listWidget_cameras->addItem(name + " (" + device + ")");
+        } else {
+            ui->listWidget_cameras->addItem(name);
+        }
+    }
+}
+
+void CameraDialog::on_pushButton_remove_clicked()
+{
+    // Get selected camera item
+    QListWidgetItem *item = ui->listWidget_cameras->currentItem();
+    if (item) {
+        // Delete selected camera
+        delete item;
+    }
+}
+
+void CameraDialog::on_pushButton_save_clicked()
+{
+    // Open file dialog to select save location
+    QString fileName = QFileDialog::getSaveFileName(this, "Save Camera Config", "", "JSON Files (*.json);;All Files (*.*)");
+    if (fileName.isEmpty()) {
+        return;
+    }
+    
+    // Create JSON document
+    QJsonObject root;
+    QJsonArray cameras;
+    
+    // Iterate camera list and add to JSON array
+    for (int i = 0; i < ui->listWidget_cameras->count(); i++) {
+        QListWidgetItem *item = ui->listWidget_cameras->item(i);
+        QJsonObject cameraObj;
+        cameraObj["name"] = item->text();
+        cameras.append(cameraObj);
+    }
+    
+    root["cameras"] = cameras;
+    
+    // Write to file
+    QFile file(fileName);
+    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+        QJsonDocument doc(root);
+        file.write(doc.toJson());
+        file.close();
+        QMessageBox::information(this, "Save Success", "Camera config saved to file");
+    } else {
+        QMessageBox::warning(this, "Save Failed", "Cannot open file for writing");
+    }
+}
+
+void CameraDialog::on_pushButton_load_clicked()
+{
+    // Open file dialog to select load file
+    QString fileName = QFileDialog::getOpenFileName(this, "Load Camera Config", "", "JSON Files (*.json);;All Files (*.*)");
+    if (fileName.isEmpty()) {
+        return;
+    }
+    
+    // Read file
+    QFile file(fileName);
+    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+        QMessageBox::warning(this, "Load Failed", "Cannot open file for reading");
+        return;
+    }
+    
+    // Parse JSON
+    QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
+    file.close();
+    
+    if (!doc.isObject()) {
+        QMessageBox::warning(this, "Load Failed", "File format is incorrect");
+        return;
+    }
+    
+    QJsonObject root = doc.object();
+    QJsonArray cameras = root["cameras"].toArray();
+    
+    // Clear existing camera list
+    ui->listWidget_cameras->clear();
+    
+    // Add cameras to list
+    for (int i = 0; i < cameras.size(); i++) {
+        QJsonObject cameraObj = cameras[i].toObject();
+        QString name = cameraObj["name"].toString();
+        ui->listWidget_cameras->addItem(name);
+    }
+    
+    QMessageBox::information(this, "Load Success", "Camera config loaded from file");
+}
+
+void CameraDialog::loadCameraConfig()
+{
+    // Try to load default config file
+    QString fileName = "camera_config.json";
+    QFile file(fileName);
+    
+    if (!file.exists()) {
+        return;
+    }
+    
+    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+        return;
+    }
+    
+    // Parse JSON
+    QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
+    file.close();
+    
+    if (!doc.isObject()) {
+        return;
+    }
+    
+    QJsonObject root = doc.object();
+    QJsonArray cameras = root["cameras"].toArray();
+    
+    // Clear existing camera list
+    ui->listWidget_cameras->clear();
+    
+    // Add cameras to list
+    for (int i = 0; i < cameras.size(); i++) {
+        QJsonObject cameraObj = cameras[i].toObject();
+        QString name = cameraObj["name"].toString();
+        ui->listWidget_cameras->addItem(name);
+    }
+}

+ 31 - 0
src/cameradialog.h

@@ -0,0 +1,31 @@
+#ifndef CAMERADIALOG_H
+#define CAMERADIALOG_H
+
+#include <QDialog>
+
+namespace Ui {
+class CameraDialog;
+}
+
+class CameraDialog : public QDialog
+{
+    Q_OBJECT
+
+public:
+    explicit CameraDialog(QWidget *parent = nullptr);
+    ~CameraDialog();
+
+private slots:
+    void on_okButton_clicked();
+    void on_cancelButton_clicked();
+    void on_pushButton_add_clicked();
+    void on_pushButton_remove_clicked();
+    void on_pushButton_save_clicked();
+    void on_pushButton_load_clicked();
+
+private:
+    Ui::CameraDialog *ui;
+    void loadCameraConfig();
+};
+
+#endif // CAMERADIALOG_H

+ 344 - 0
src/cameradialog.ui

@@ -0,0 +1,344 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>CameraDialog</class>
+ <widget class="QDialog" name="CameraDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>700</width>
+    <height>500</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>相机管理</string>
+  </property>
+  <widget class="QWidget" name="verticalLayoutWidget">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>10</y>
+     <width>681</width>
+     <height>451</height>
+    </rect>
+   </property>
+   <layout class="QVBoxLayout" name="verticalLayout">
+    <item>
+     <widget class="QGroupBox" name="groupBox">
+      <property name="title">
+       <string>相机列表</string>
+      </property>
+      <layout class="QHBoxLayout" name="horizontalLayout">
+       <item>
+        <widget class="QListWidget" name="listWidget_cameras">
+         <item>
+          <property name="text">
+           <string>相机 1</string>
+          </property>
+         </item>
+         <item>
+          <property name="text">
+           <string>相机 2</string>
+          </property>
+         </item>
+         <item>
+          <property name="text">
+           <string>相机 3</string>
+          </property>
+         </item>
+        </widget>
+       </item>
+       <item>
+        <layout class="QVBoxLayout" name="verticalLayout_2">
+         <item>
+          <widget class="QPushButton" name="pushButton_add">
+           <property name="text">
+            <string>添加</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QPushButton" name="pushButton_remove">
+           <property name="text">
+            <string>移除</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QPushButton" name="pushButton_refresh">
+           <property name="text">
+            <string>刷新</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QPushButton" name="pushButton_save">
+           <property name="text">
+            <string>保存</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QPushButton" name="pushButton_load">
+           <property name="text">
+            <string>加载</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <spacer name="verticalSpacer">
+           <property name="orientation">
+            <enum>Qt::Vertical</enum>
+           </property>
+           <property name="sizeHint" stdset="0">
+            <size>
+             <width>20</width>
+             <height>40</height>
+            </size>
+           </property>
+          </spacer>
+         </item>
+        </layout>
+       </item>
+      </layout>
+     </widget>
+    </item>
+    <item>
+     <widget class="QTabWidget" name="tabWidget">
+      <property name="currentIndex">
+       <number>0</number>
+      </property>
+      <widget class="QWidget" name="tab">
+       <attribute name="title">
+        <string>基本设置</string>
+       </attribute>
+       <layout class="QFormLayout" name="formLayout">
+        <item row="0" column="0">
+         <widget class="QLabel" name="label">
+          <property name="text">
+           <string>相机名称:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="0" column="1">
+         <widget class="QLineEdit" name="lineEdit_name"/>
+        </item>
+        <item row="1" column="0">
+         <widget class="QLabel" name="label_2">
+          <property name="text">
+           <string>相机类型:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="1" column="1">
+         <widget class="QComboBox" name="comboBox_type">
+          <item>
+           <property name="text">
+            <string>USB 相机</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>GigE 相机</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>Camera Link</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>网络相机</string>
+           </property>
+          </item>
+         </widget>
+        </item>
+        <item row="2" column="0">
+         <widget class="QLabel" name="label_3">
+          <property name="text">
+           <string>分辨率:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="2" column="1">
+         <widget class="QComboBox" name="comboBox_resolution">
+          <item>
+           <property name="text">
+            <string>640x480</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>1280x720</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>1920x1080</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>3840x2160</string>
+           </property>
+          </item>
+         </widget>
+        </item>
+        <item row="3" column="0">
+         <widget class="QLabel" name="label_4">
+          <property name="text">
+           <string>帧率:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="3" column="1">
+         <widget class="QLineEdit" name="lineEdit_fps"/>
+        </item>
+       </layout>
+      </widget>
+      <widget class="QWidget" name="tab_2">
+       <attribute name="title">
+        <string>高级设置</string>
+       </attribute>
+       <layout class="QFormLayout" name="formLayout_2">
+        <item row="0" column="0">
+         <widget class="QLabel" name="label_5">
+          <property name="text">
+           <string>曝光时间:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="0" column="1">
+         <widget class="QLineEdit" name="lineEdit_exposure"/>
+        </item>
+        <item row="1" column="0">
+         <widget class="QLabel" name="label_6">
+          <property name="text">
+           <string>增益:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="1" column="1">
+         <widget class="QLineEdit" name="lineEdit_gain"/>
+        </item>
+        <item row="2" column="0">
+         <widget class="QLabel" name="label_7">
+          <property name="text">
+           <string>白平衡:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="2" column="1">
+         <widget class="QCheckBox" name="checkBox_whiteBalance">
+          <property name="text">
+           <string>自动</string>
+          </property>
+         </widget>
+        </item>
+        <item row="3" column="0">
+         <widget class="QLabel" name="label_8">
+          <property name="text">
+           <string>触发模式:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="3" column="1">
+         <widget class="QComboBox" name="comboBox_trigger">
+          <item>
+           <property name="text">
+            <string>自由运行</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>外部触发</string>
+           </property>
+          </item>
+          <item>
+           <property name="text">
+            <string>软件触发</string>
+           </property>
+          </item>
+         </widget>
+        </item>
+       </layout>
+      </widget>
+      <widget class="QWidget" name="tab_3">
+       <attribute name="title">
+        <string>预览</string>
+       </attribute>
+       <layout class="QVBoxLayout" name="verticalLayout_3">
+        <item>
+         <widget class="QLabel" name="label_9">
+          <property name="styleSheet">
+           <string notr="true">background-color: #000; border: 1px solid #ccc;</string>
+          </property>
+          <property name="text">
+           <string>相机预览区域</string>
+          </property>
+          <property name="alignment">
+           <set>Qt::AlignCenter</set>
+          </property>
+         </widget>
+        </item>
+        <item>
+         <widget class="QPushButton" name="pushButton_startPreview">
+          <property name="text">
+           <string>开始预览</string>
+          </property>
+         </widget>
+        </item>
+       </layout>
+      </widget>
+     </widget>
+    </item>
+    <item>
+     <widget class="QDialogButtonBox" name="buttonBox">
+      <property name="orientation">
+       <enum>Qt::Horizontal</enum>
+      </property>
+      <property name="standardButtons">
+       <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+      </property>
+     </widget>
+    </item>
+   </layout>
+  </widget>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>CameraDialog</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>CameraDialog</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>

+ 48 - 0
src/camerafactory.cpp

@@ -0,0 +1,48 @@
+#include "camerafactory.h"
+#include "usbcamera.h"
+#include "gigecamera.h"
+#include "networkcamera.h"
+#include "cameralinkcamera.h"
+#include "hikvisioncamera.h"
+#include "baslercamera.h"
+
+
+CameraClass* CameraFactory::createCamera(CameraClass::CameraType type, const QString &name)
+{
+    switch (type) {
+    case CameraClass::USB_CAMERA:
+        return new USBCamera(name);
+    case CameraClass::GIGE_CAMERA:
+        return new GigECamera(name);
+    case CameraClass::NETWORK_CAMERA:
+        return new NetworkCamera(name);
+    case CameraClass::CAMERA_LINK:
+        return new CameraLinkCamera(name);
+    case CameraClass::HIKVISION_CAMERA:
+        return new HikvisionCamera(name, HikvisionCamera::HIKVISION_GIGE);
+    default:
+        return nullptr;
+    }
+}
+
+CameraClass* CameraFactory::createCamera(CameraClass::CameraBrand brand, CameraClass::CameraType type, const QString &name)
+{
+    CameraClass* camera = nullptr;
+    
+    switch (brand) {
+    case CameraClass::HIKVISION:
+        camera = new HikvisionCamera(name, HikvisionCamera::HIKVISION_GIGE);
+        break;
+    case CameraClass::BASLER:
+        camera = new BaslerCamera(name);
+        break;
+    default:
+        // ¸ù¾ÝÀàÐÍ´´½¨Ä¬ÈÏÏà»ú
+        camera = createCamera(type, name);
+        break;
+    }    
+    if (camera) {
+        camera->setBrand(brand);
+    }
+    return camera;
+}