C++Builder XE6开发海康设备控制软件,控制摄像头图像预览与云台控制,写了两遍都发布失败了,只好直接上代码了,注意XE6的wchar_t问题,因为海康dll是vc写的,只支持char,需要转换,特别是“.c_str()”函数的使用,程序中使用AnsiString定义字符串,使用String会出错。
Main.cpp代码:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
Dir = ExtractFilePath(Application->ExeName) ;
iLastErr = 0 ;
m_lUserID = -1 ;
m_lRealHandle = -1 ;
m_bInitSDK = false ;
m_bRecord = false ;
bAuto = false ;
CreateDirectory(String(Dir + "SdkLog").c_str(),NULL); //创建日志目录
CreateDirectory(String(Dir + "CapPic").c_str(),NULL); //创建抓图目录
CreateDirectory(String(Dir + "Record").c_str(),NULL); //创建录像目录
m_bInitSDK = NET_DVR_Init() ;
if (m_bInitSDK)
NET_DVR_SetLogToFile(3, AnsiString(Dir+ "SdkLog\\").c_str(), true) ; //保存SDK日志
else
ShowMessage("NET_DVR_Init初始化失败!") ;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormDestroy(TObject *Sender)
{
if(m_lRealHandle >= 0)
NET_DVR_StopRealPlay(m_lRealHandle);
if(m_lUserID >= 0)
NET_DVR_Logout(m_lUserID);
if(m_bInitSDK)
NET_DVR_Cleanup();
}
//---------------------------------------------------------------------------
//登录
void __fastcall TfrmMain::btnLoginClick(TObject *Sender)
{
if (edtIPAddress->Text==""||edtPortNumber->Text==""
||edtUserName->Text==""||edtPassword->Text=="")
{
ShowMessage("请输入设备地址、端口、用户名和密码!") ;
return ;
}
DVRIPAddress = edtIPAddress->Text ; //设备IP地址或者域名
DVRPortNumber = edtPortNumber->Text.ToInt() ; //设备服务端口号
DVRUserName = edtUserName->Text ; //设备登录用户名
DVRPassword = edtPassword->Text ; //设备登录密码
NET_DVR_DEVICEINFO_V30 DeviceInfo ;
if (m_lUserID < 0)
{
//登录设备
m_lUserID = NET_DVR_Login_V30(DVRIPAddress.c_str(), DVRPortNumber, DVRUserName.c_str(), DVRPassword.c_str(), &DeviceInfo) ;
if (m_lUserID < 0)
{
iLastErr = NET_DVR_GetLastError() ;
//登录失败,输出错误号
ShowMessage("NET_DVR_Login_V30登录失败,错误代码" + String(iLastErr)) ;
}
else
{
//登录成功
ShowMessage(DVRIPAddress+"登录成功") ;
btnLogin->Caption = "注销" ;
}
}
else
{
//注销登录
if (m_lRealHandle >= 0)
{
ShowMessage("请首先停止在线预览!") ;
return ;
}
if (!NET_DVR_Logout(m_lUserID))
{
iLastErr = NET_DVR_GetLastError() ;
ShowMessage("NET_DVR_Logout注销失败,错误代码" + String(iLastErr)) ;
}
else
ShowMessage(DVRIPAddress+"注销成功") ;
m_lUserID = -1 ;
btnLogin->Caption = "登录" ;
}
}
//---------------------------------------------------------------------------
//预览
void __fastcall TfrmMain::btnPreviewClick(TObject *Sender)
{
if(m_lUserID < 0)
{
ShowMessage("请首先登录设备") ;
return ;
}
NET_DVR_PREVIEWINFO lpPreviewInfo ;
lpPreviewInfo.hPlayWnd = pnlPreview->Handle ; //预览窗口
lpPreviewInfo.lChannel = edtChannel->Text.ToInt() ; //预te览的设备通道
lpPreviewInfo.dwStreamType = 0 ; //码流类型:0-主码流,1-子码流,2-码流3,3-码流4,以此类推
lpPreviewInfo.dwLinkMode = 0 ; //连接方式:0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP
lpPreviewInfo.bBlocked = true ; //0- 非阻塞取流,1- 阻塞取流
lpPreviewInfo.dwDisplayBufNum = 15 ; //播放库播放缓冲区最大缓冲帧数
if (m_lRealHandle < 0)
{
REALDATACALLBACK RealData ;//预览实时流回调函数
void* pUser = 0 ;//用户数据
//打开预览
m_lRealHandle = NET_DVR_RealPlay_V40(m_lUserID, &lpPreviewInfo, NULL, pUser) ;
if (m_lRealHandle < 0)
{
iLastErr = NET_DVR_GetLastError();
//预览失败,输出错误号
ShowMessage("NET_DVR_RealPlay_V40预览失败,错误代码" + String(iLastErr)) ;
return;
}
else
{
//预览成功
btnPreview->Caption = "停止预览" ;
ShowMessage("预览成功") ;
}
}
else
{
//停止预览
if (!NET_DVR_StopRealPlay(m_lRealHandle))
{
iLastErr = NET_DVR_GetLastError() ;
//停止预览失败,输出错误号
ShowMessage("NET_DVR_StopRealPlay停止预览失败,错误代码" + String(iLastErr)) ;
}
else
{
//pnlPreview->Assign(NULL) ;
//pnlPreview->Color = clBlack ; // pnlPreview->Free() ;// Update() ;
ShowMessage("停止预览成功") ;
}
m_lRealHandle = -1 ;
btnPreview->Caption = "预览" ;
}
if(m_lRealHandle > -1)
chkPreview->Checked = true ;
else
chkPreview->Checked = false ;
}
//---------------------------------------------------------------------------
//BMP抓图
void __fastcall TfrmMain::btnBMPClick(TObject *Sender)
{
//图片保存路径和文件名
String sBmpPicFileName = "BMP" + Now().FormatString("yyyymmddhhnnss") + ".bmp" ;
//BMP抓图
if (!NET_DVR_CapturePicture(m_lRealHandle, (Dir + "CapPic\\" + sBmpPicFileName).c_str()))
{
iLastErr = NET_DVR_GetLastError();
ShowMessage("NET_DVR_CapturePicture BMP抓图失败,错误代码" + String(iLastErr)) ;
}
else
{
ShowMessage("BMP抓图成功,文件保存为" + sBmpPicFileName) ;
}
}
//---------------------------------------------------------------------------
//JPEG抓图
void __fastcall TfrmMain::btnJPEGClick(TObject *Sender)
{
//图片保存路径和文件名
String sJpegPicFileName = "JPEG" + Now().FormatString("yyyymmddhhnnss") + ".jpg" ;
int lChannel = edtChannel->Text.ToInt() ; //通道号
NET_DVR_JPEGPARA lpJpegPara ;
lpJpegPara.wPicQuality = 0 ; //图像质量
lpJpegPara.wPicSize = 0xff ; //抓图分辨率2- 4CIF,0xff- Auto(使用当前码流分辨率),抓图分辨率需要设备支持,更多取值请参考SDK文档
//JPEG抓图
if (!NET_DVR_CaptureJPEGPicture(m_lUserID, lChannel, &lpJpegPara, (Dir + "CapPic\\" + sJpegPicFileName).c_str()))
{
iLastErr = NET_DVR_GetLastError();
ShowMessage("NET_DVR_CaptureJPEGPicture JPEG抓图失败,错误代码" + String(iLastErr)) ;
}
else
{
ShowMessage("JPEG抓图成功,文件保存为" + sJpegPicFileName) ;
}
}
//---------------------------------------------------------------------------
//客户端录像
void __fastcall TfrmMain::btnRecordClick(TObject *Sender)
{
//录像保存路径和文件名 the path and file name to save
String sVideoFileName = "Record" + Now().FormatString("yyyymmddhhnnss") + ".mp4" ;
if (m_bRecord == false)
{
//强制I帧
m_lChannel = edtChannel->Text.ToInt() ; //通道号
NET_DVR_MakeKeyFrame(m_lUserID, m_lChannel) ;
//开始录像
if (!NET_DVR_SaveRealData(m_lRealHandle, (Dir + "Record\\" + sVideoFileName).c_str()))
{
iLastErr = NET_DVR_GetLastError();
ShowMessage("NET_DVR_SaveRealData 录像失败,错误代码" + String(iLastErr)) ;
}
else
{
btnRecord->Caption = "客户端录像停止" ;
m_bRecord = true ;
}
}
else
{
//停止录像
if (!NET_DVR_StopSaveRealData(m_lRealHandle))
{
iLastErr = NET_DVR_GetLastError();
ShowMessage("NET_DVR_StopSaveRealData 停止录像失败,错误代码" + String(iLastErr)) ;
}
else
{
ShowMessage("客户端录像停止,文件保存为" + sVideoFileName) ;
btnRecord->Caption = "客户端录像" ;
m_bRecord = false;
}
}
}
//---------------------------------------------------------------------------
//摄像机云台-转动
void __fastcall TfrmMain::btnPTZMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TComponent *cpt = dynamic_cast(Sender) ;
if(cpt == 0 || cpt->Tag < 0 || cpt->Tag > 10 )
return ;
DWORD dwPTZCommand[11] = {PAN_AUTO,TILT_UP,TILT_DOWN,PAN_LEFT,PAN_RIGHT,ZOOM_IN,ZOOM_OUT,FOCUS_NEAR,FOCUS_FAR,IRIS_OPEN,IRIS_CLOSE} ;
if(cpt->Tag == 0)
{
if (chkPreview->Checked)
{
if (!bAuto)
{
NET_DVR_PTZControlWithSpeed(m_lRealHandle, PAN_AUTO, 0, cbbSpeed->ItemIndex + 1) ;
bAuto = true;
}
else
{
NET_DVR_PTZControlWithSpeed(m_lRealHandle, PAN_AUTO, 1, cbbSpeed->ItemIndex + 1) ;
bAuto = false;
}
}
else
{
if (!bAuto)
{
NET_DVR_PTZControlWithSpeed_Other(m_lUserID, m_lChannel, PAN_AUTO, 0, cbbSpeed->ItemIndex + 1) ;
bAuto = true;
}
else
{
NET_DVR_PTZControlWithSpeed_Other(m_lUserID, m_lChannel, PAN_AUTO, 1, cbbSpeed->ItemIndex + 1) ;
bAuto = false;
}
}
}
else
{
if(chkPreview->Checked)
NET_DVR_PTZControlWithSpeed(m_lRealHandle,dwPTZCommand[cpt->Tag],0,cbbSpeed->ItemIndex+1) ;
else
NET_DVR_PTZControlWithSpeed_Other(m_lUserID,m_lChannel,dwPTZCommand[cpt->Tag],0,cbbSpeed->ItemIndex+1) ;
}
}
//---------------------------------------------------------------------------
//摄像机云台-停止
void __fastcall TfrmMain::btnPTZMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TComponent *cpt = dynamic_cast(Sender) ;
if(cpt == 0 || cpt->Tag < 0 || cpt->Tag > 10 )
return ;
DWORD dwPTZCommand[11] = {PAN_AUTO,TILT_UP,TILT_DOWN,PAN_LEFT,PAN_RIGHT,ZOOM_IN,ZOOM_OUT,FOCUS_NEAR,FOCUS_FAR,IRIS_OPEN,IRIS_CLOSE} ;
if(chkPreview->Checked)
NET_DVR_PTZControlWithSpeed(m_lRealHandle,dwPTZCommand[cpt->Tag],1,cbbSpeed->ItemIndex+1) ;
else
NET_DVR_PTZControlWithSpeed_Other(m_lUserID,m_lChannel,dwPTZCommand[cpt->Tag],1,cbbSpeed->ItemIndex+1) ;
}
//---------------------------------------------------------------------------
Main.h代码:
//---------------------------------------------------------------------------
#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include "HCNetSDK.h"
#include "PreviewDemoPCH.h"
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TfrmMain : public TForm
{
__published: // IDE-managed Components
TPanel *pnlPreview;
TLabel *lblIP;
TEdit *edtIPAddress;
TEdit *edtPortNumber;
TLabel *lblPort;
TEdit *edtUserName;
TLabel *lblUser;
TEdit *edtPassword;
TLabel *lblPsw;
TButton *btnLogin;
TButton *btnPreview;
TLabel *lblChannel;
TEdit *edtChannel;
TButton *btnBMP;
TButton *btnJPEG;
TButton *btnRecord;
TSpeedButton *btnPTZUp;
TSpeedButton *btnPTZLeft;
TSpeedButton *btnPTZHome;
TSpeedButton *btnPTZRight;
TSpeedButton *btnPTZDown;
TCheckBox *chkPreview;
TLabel *lblSpeed;
TComboBox *cbbSpeed;
TSpeedButton *btnZoomAdd;
TSpeedButton *btnFocusAdd;
TSpeedButton *btnBrightAdd;
TSpeedButton *btnBrightDec;
TSpeedButton *btnFocusDec;
TSpeedButton *btnZoomDec;
TLabel *lblZoom;
TLabel *lblFocus;
TLabel *lblBright;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall btnLoginClick(TObject *Sender);
void __fastcall btnPreviewClick(TObject *Sender);
void __fastcall btnBMPClick(TObject *Sender);
void __fastcall btnJPEGClick(TObject *Sender);
void __fastcall btnRecordClick(TObject *Sender);
void __fastcall btnPTZMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall btnPTZMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
private: // User declarations
public: // User declarations
__fastcall TfrmMain(TComponent* Owner);
unsigned int iLastErr;
int m_lUserID,m_lRealHandle,m_lChannel,DVRPortNumber ;
bool m_bInitSDK,m_bRecord,bAuto;
AnsiString Dir,DVRIPAddress,DVRUserName,DVRPassword;
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmMain *frmMain;
//---------------------------------------------------------------------------
#endif