前言
本章將服務器端幾個非主要但常用的功能分享一下。
一、驗證
1.1 IP驗證
VC++ Code:
OnInitDialog方法
MP4_ServerCheckIP(CheckIP);
CheckIP委托
int CALLBACK CheckIP(DWORD nChannel,char* nIP)
{
/*
CString ctemp;
ctemp.Format("% s",nIP);
if (ctemp == "192.0.0.215")
return -1;
*/
return 0;
}
代碼說明:
1.自帶的例子IP驗證部分注釋掉了,但是可以看得出返回-1表示錯誤,返回0表示驗證通過,並且實際證明 如果返回-1的話客戶端是不出圖像的。
C# Code:
/// <summary>
/// 驗證IP
/// </summary>
/// <param name="nChannel">通道號</param>
/// <param name="nIP">ip地址</param>
/// <returns></returns>
public int CheckIP(int nChannel, string nIP)
{
//驗證代碼
return 0;
}
代碼說明:
1.比較簡單,填寫自己的驗證代碼,正確返 回0,不正確反正-1就可以了。
1.2 用戶名密碼驗證
VC++ Code:
OnInitDialog方法
MP4_ServerCheckPassword(checkpassword);
CheckPassword委托
int CALLBACK checkpassword (char *username,WORD namelen,char *password,WORD passlen)
{
/*
if ((username[0] == '1') && (username[1] == '2') && (username[2] == '3') && (password[0] == 'w'))
return 0;
else
return -1;
*/
return 0;
}
C# Code:
/// <summary>
/// 驗證用戶名密碼
/// </summary>
/// <param name="username">用戶名</param>
/// <param name="namelen">用戶名長度</param>
/// <param name="password">密碼 </param>
/// <param name="passlen">密碼長度</param>
/// <returns></returns>
public int CheckPassword(string username, ushort namelen, string password, ushort passlen)
{
//驗證代碼
return 0;
}
這部分和驗證IP差不多,唯一需要注意的是把委托寫成成員變 量!!
二、設置畫面采集質量
從VC++例子中Settings中可以看到這個設置:
VC++ Code:
方法OnSettings()
switch(dlg.m_iEncodeType)
{
case 0:
encodeType = ENC_4CIF_FORMAT;
break;
case 1:
encodeType = ENC_2CIF_FORMAT;
break;
case 2:
encodeType = ENC_DCIF_FORMAT;
break;
case 3:
encodeType = ENC_CIF_FORMAT;
break;
case 4:
encodeType = ENC_QCIF_FORMAT;
break;
default:
encodeType = ENC_CIF_FORMAT;
break;
}
for(i = 0; i < GetTotalDSPs(); i++)
{
if(encodeType != ENC_4CIF_FORMAT)
{
SetEncoderPictureFormat(ChannelHandle[i],encodeType);
}
else if ((i%4==0)||(i%4==1))
{
SetEncoderPictureFormat(ChannelHandle[i],encodeType);
}
}
C# Code:
/// <summary>
/// 設置當前所有攝頭錄制的畫面質量
/// </summary>
/// <param name="ChannelHandle"></param>
/// <param name="t">編碼圖像分辨率</param>
public static void SetCurrentEncoderPictureFormat(IntPtr[] ChannelHandle, PictureFormat_t t)
{
for (int i = 0; i < ChannelHandle.Length; i++)
{
HikVisionSDK.SetEncoderPictureFormat(ChannelHandle[i], t);
}
}
代碼說明:
1.枚舉PictureFormat_t說明:
PictureFormat_t.ENC_QCIF_FORMAT 低質量
PictureFormat_t.ENC_CIF_FORMAT 中質量
PictureFormat_t.ENC_DCIF_FORMAT 中 高質量
PictureFormat_t.ENC_4CIF_FORMAT 高質量
2.設置後畫面效果能實時改變。
三、設置字幕疊 加
一般用於顯示公司LOGO和日期,下圖黃色區域所示
VC ++ Code:
方法OnSettings()
USHORT Format1[40] = {48, 16, 'H','I','K','V', 'I','S','I','O','N', '\0'};
USHORT Format2[40] = {24, 50, _OSD_YEAR4, '-',_OSD_MONTH2,'-',_OSD_DAY,'-', _OSD_HOUR24, ':', _OSD_MINUTE,':', _OSD_SECOND, '\0'};
//其他代碼
for(i = 0; i < GetTotalDSPs(); i++){
SetOsdDisplayMode(ChannelHandle[i], 255, TRUE, 0, Format1, Format2);
SetOsd(ChannelHandle[i], TRUE);
}
C# Code:
private const ushort _OSD_BASE = 0x9000;
private const ushort _OSD_YEAR4 = _OSD_BASE + 0;
private const ushort _OSD_YEAR2 = _OSD_BASE + 1;
private const ushort _OSD_MONTH3 = _OSD_BASE + 2;
private const ushort _OSD_MONTH2 = _OSD_BASE + 3;
private const ushort _OSD_DAY = _OSD_BASE + 4;
private const ushort _OSD_WEEK3 = _OSD_BASE + 5;
private const ushort _OSD_CWEEK1 = _OSD_BASE + 6;
private const ushort _OSD_HOUR24 = _OSD_BASE + 7;
private const ushort _OSD_HOUR12 = _OSD_BASE + 8;
private const ushort _OSD_MINUTE = _OSD_BASE + 9;
private const ushort _OSD_SECOND = _OSD_BASE + 10;
/// <summary>
/// 設置Osd
/// </summary>
/// <param name="ChannelHandle"></param>
/// <param name="setValue"></param>
public void SetOsd(IntPtr[] ChannelHandle, bool setValue)
{
if (setValue)
{
ushort[] Format1 = new ushort[]{ 48, 16, 'H', 'I', 'K', 'V', 'I', 'S', 'I', 'O', 'N', '\0' };
ushort[] Format2 = new ushort[]{ 24, 50, _OSD_YEAR4, '-', _OSD_MONTH2, '-', _OSD_DAY, '-', _OSD_HOUR24, ':', _OSD_MINUTE, ':', _OSD_SECOND, '\0' };
for (int i = 0; i < ChannelHandle.Length; i++)
{
HikVisionSDK.SetOsdDisplayMode(ChannelHandle[i], 255, true, 0, Format1, Format2);
HikVisionSDK.SetOsd(ChannelHandle[i], true);
}
}
else
{
for (int i = 0; i < ChannelHandle.Length; i++)
{
HikVisionSDK.SetOsd(ChannelHandle[i], false);
}
}
}
代碼說明:
1.注意設置中文字符有問題,暫時沒有解決。
2.關於時間顯示可以翻閱API文檔,說明得很詳細。
四、獲取板卡序列號
VC++ Code: 這段代碼並不在Net Server Demo項目裡面,而是在System Demo(Src\SysDemoSource)項目裡。
BoardList.cpp
char str[100];
char sn[12+1];
m_list.DeleteAllItems();
boardCount=GetBoardCount();
for(UINT board=0;board<boardCount;board++)
{
GetBoardDetail (board,&boardDetail);
for(UINT i=0;i<12;i++)
sn[i]=boardDetail.sn[i]+0x30;
sn[12]=0;
sprintf (str,"Board:%2u Type:%s,SN:%s",
board,boardType[boardDetail.type],sn);
C# Code:
/// <summary>
/// 獲取板卡序號
/// </summary>
/// <returns></returns>
public static string GetBoardSN()
{
uint boardCount = HikVisionSDK.GetBoardCount();
StringBuilder sn = new StringBuilder ();
for (uint i = 0; i < boardCount; )
{
DS_BOARD_DETAIL boardDetail = new DS_BOARD_DETAIL();
HikVisionSDK.GetBoardDetail(i, ref boardDetail);
for (int j = 0; j < 12; j++)
{
sn.Append((char) (boardDetail.sn[j] + 0x30));
}
break;
}
return sn.ToString();
}
代碼說明:
1.注意 結構體用ref;以及boardDetail.sn[j]的類型轉換。
結束
服務器端和客戶端一樣三篇文章,至此結束,接下來是播放器。感謝 大家繼續關注該系列文章!!
五、文章後期維護
5.1 2010-10-9 OSD顯示漢字辦法
來自博客園的另一朋友站在巨人的肩上,如下可顯示漢字:
漢字"保"的機器內碼是"B1A3H",將如下代碼替換:
替換前:new ushort[]{ 48, 16, 'H', 'I', 'K', 'V', 'I', 'S', 'I', 'O', 'N', '\0' };
替換後:new ushort[]{ 48, 16, 0xB1A3, 'I', 'K', 'V', 'I', 'S', 'I', 'O', 'N', '\0' };
這樣就能顯示漢字了,這裡再次感謝,也歡迎大家分享和交流!