1. 前言
这个是我们所负责的实验室的一个相机系统的相关内容,鉴于一些原因呢, 很多东西不能写出来, 只是作为自己资料备份使用。。。
2. 自动配置左右相机效果
在我们原先的相机程序中, 检测相机和启动相机是分开的, 需要用户独立按键。如下图所示:
而现在我们的程序中, 会根据程序对应目录下的left.ini 和 right.ini 自动配置相应的相机, 并启动他的显示界面。
如果启动失败, 有相应的提示:
3. 实现代码
我们这里新建了一个类 CAutoDetect 用来负责相机的自动配置工作:
AutoDetect.h
#pragma once
class CAVTDlg;
class CAutoDetect
{
public:
CAutoDetect(CAVTDlg * parent);
~CAutoDetect();
bool autoDetect(bool isLeft);
private:
unsigned long readfile(bool isLeft);
CAVTDlg * parent;
};
AutoDetect.cpp
#include "stdafx.h"
#include "AutoDetect.h"
#include <fstream>
#include <algorithm>
#include "PvApi.h"
#include "AVTDlg.h"
#include <string>
#include "Resource.h"
using namespace std;
const tPvUint32 KMaxPossibleCameras = 50;
CAutoDetect::CAutoDetect(CAVTDlg * parent) : parent(parent)
{
}
CAutoDetect::~CAutoDetect()
{
}
unsigned long CAutoDetect::readfile(bool isLeft)
{
unsigned long uid = 0;
ifstream in;
if (isLeft)
in.open("left.ini");
else
in.open("right.ini");
in >> uid;
return uid;
}
bool CAutoDetect::autoDetect(bool isLeft)
{
unsigned long uid = readfile(isLeft);
if (uid == -1)
{
TRACE(_T("readfile失败\n"));
return false;
}
tPvCameraInfo Infos[KMaxPossibleCameras];
tPvUint32 Count = 0;
// list all the cameras currently connected
if ((Count == PvCameraList(Infos, KMaxPossibleCameras, &Count)))
{
// and loop over all the cameras found to add item to the tree
for (tPvUint32 i = 0; i < min(KMaxPossibleCameras, Count); i++)
{
// 获取当前相机 id 信息
unsigned long cur_id = Infos[i].UniqueId;
if (cur_id == uid){
if (isLeft)
{
// 配置左相机
parent->m_cameraID = uid;
CString tip = to_wstring(uid).c_str();
parent->m_View->SetWindowText(_T("Left Camera - ") + tip);
parent->m_View->m_cap = tip;
parent->GetDlgItem(IDC_GROUP_LEFT)->SetWindowText(_T("Left Camera - ") + tip);
parent->GetDlgItem(IDC_START)->EnableWindow(false);
}
else
{
// 配置右相机
parent->m_cameraID_right = uid;
CString tip = to_wstring(uid).c_str();
parent->m_View->SetWindowText(_T("Right Camera - ") + tip);
parent->m_View->m_cap = tip;
parent->GetDlgItem(IDC_GROUP_RIGHT)->SetWindowText(_T("Right Camera - ") + tip);
parent->GetDlgItem(IDC_START_RIGHT)->EnableWindow(false);
}
return true;
}
}
}
return false;
}
3.原理说明
主要的思想就是, 新建一个类, 利用这个类读取配置文件信息, 然后根据这个配置文件中保存的相机的id 信息, 遍历所有的相机序列, 从而获取与相机id 相对应的 相机句柄信息。