C#使⽤WebService调⽤海康SDK实现抓图与录像实时下载
1.海康设备⽹络SDK下载
下载完毕解压后的⽬录结构如下:
2.代码实现
通过参考“CH-HCNetSDKV6.0.2.35_build20190411_Win32\Demo⽰例\3- C# 开发⽰例\1-实时预览⽰例代码⼀”路径下的⽰例项⽬,控制海康录像机进⾏抓拍图⽚与实时下载录像功能
2.1 算了不贴代码了,这个MarkDown编辑器真tm难⽤,为什么复制过来的代码变成这样?
2.2 主要代码
2.2.fig
<appSettings>
<!--海康摄像机配置-->
<add key="dvrIp" value="10.14.8.70"/>
<add key="dvrPort" value="8000"/>
<add key="dvrUserName" value="admin"/>
<add key="dvrPassword" value="12345"/>
<!--图⽚视频存储路径配置-->
<add key="picturePath" value="C:\\HYKJ\\Pictures\\"/>
<add key="videoPath" value="C:\\HYKJ\\Videos\\"/>
</appSettings>
2.2.2 WebService1.asmx
public static string dvrIp = ConfigurationManager.AppSettings["dvrIp"].ToString(); //录像机IP
public static string dvrPort = ConfigurationManager.AppSettings["dvrPort"].ToString(); //录像机端⼝
public static string dvrUserName = ConfigurationManager.AppSettings["dvrUserName"].ToString(); //录像机⽤户名
public static string dvrPassword = ConfigurationManager.AppSettings["dvrPassword"].ToString(); //录像机密码
public static string picPath = ConfigurationManager.AppSettings["picturePath"].ToString(); //图⽚存储路径 C:\\HYKJ\\Pictures\\ public static string vidPath = ConfigurationManager.AppSettings["videoPath"].ToString(); //录像存储路径 C:\\HYKJ\\Videos\\
/// <summary>
/// 海康录像机登录实例
/// </summary>
private static Int32 m_lUserID = -1;
/// <summary>
/// 海康错误号
/// </summary>
private static uint iLastErr = 0;
/// <summary>
/// ⽤于接收海康调⽤接⼝返回信息
/// </summary>
private static string str;
/// <summary>
/// 海康SDK是否初始化
/
// </summary>
private static bool m_bInitSDK = false;
/// <summary>
/// 海康预览数据回调
/// </summary>
private static CHCNetSDK.REALDATACALLBACK RealData = null;
/// <summary>
/// 海康预览集合
/// </summary>
private static Dictionary<string, Int32> realHandleDic = new Dictionary<string, Int32>();
/// <summary>
/
// 海康录像集合
/// </summary>
private static Dictionary<string, bool> recordDic = new Dictionary<string, bool>();
/// <summary>
/// 海康录像机拍照
/// </summary>
/// <param name="channelNum">摄像机通道号</param>
/// <returns></returns>
public bool TakePhoto(C2SJsonMessage c2sjm)
{
string channelNum = c2sjm.channelNum;
string pictureFileName = c2sjm.pictureFileName;
DateTime date = DateTime.Now;
string year = date.Year.ToString() + "\\";
string month = date.Month.ToString() + "\\";
string day = date.Day.ToString() + "\\";
bool flag = false;
string sJpegPicDirectory = picPath + year + month + day;
if (!Directory.Exists(sJpegPicDirectory))
{
Directory.CreateDirectory(sJpegPicDirectory);
}
/
/图⽚保存路径和⽂件名 C:\\HYKJ\\Pictures\\2019\\08\\08\\jylsh_jccs_hphm_channelNum_1.jpg
string sJpegPicFileName = sJpegPicDirectory + pictureFileName;
int lChannel = Int16.Parse(channelNum); //通道号 Channel number
CHCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new CHCNetSDK.NET_DVR_JPEGPARA();
lpJpegPara.wPicQuality = 0; //图像质量 Image quality
lpJpegPara.wPicSize = 0xff; //抓图分辨率 Picture size: 2- 4CIF,0xff- Auto(使⽤当前码流分辨率),抓图分辨率需要设备⽀持,更多取值请参考SDK⽂档
//JPEG抓图 Capture a JPEG picture
if (!CHCNetSDK.NET_DVR_CaptureJPEGPicture(m_lUserID, lChannel, ref lpJpegPara, sJpegPicFileName))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_CaptureJPEGPicture failed, error code= " + iLastErr;
}
else
{
str = "Successful to capture the JPEG file and the saved file is " + sJpegPicFileName;
flag = true;
}
return flag;
}
/// <summary>
/// 海康录像机开始录像
/
// </summary>
/// <param name="sVideoFileName">录像保存路径和⽂件名</param>
/// <param name="channelNum">摄像机通道号</param>
/// <returns></returns>
public bool StartTakeVideo(C2SJsonMessage c2sjm)
{
/**
* 录像的前提是进⾏了预览,录像的流程应该是:
* 1.开始预览
* 2.开始录像
* 3.结束录像
* 4.停⽌预览
* */
string channelNum = c2sjm.channelNum;
string videoFileName = c2sjm.videoFileName;
DateTime date = DateTime.Now;
string year = date.Year.ToString() + "\\";
string month = date.Month.ToString() + "\\";
string day = date.Day.ToString() + "\\";
bool flag = false;
string sVideoFiledDirectory = vidPath + year + month + day;
if (!Directory.Exists(sVideoFiledDirectory))
{
Directory.CreateDirectory(sVideoFiledDirectory);
}
//录像保存路径和⽂件名 C:\\HYKJ\\Videos\\2019\\08\\08\\jylsh_jccs_hphm_channelNum.mp4
string sVideoFileName = sVideoFiledDirectory + videoFileName;
if (!recordDic.ContainsKey(channelNum))
{
//强制I帧 Make a I frame
int lChannel = Int16.Parse(channelNum); //通道号 Channel number
CHCNetSDK.NET_DVR_MakeKeyFrame(m_lUserID, lChannel);
//开始录像 Start recording
if (!CHCNetSDK.NET_DVR_SaveRealData(realHandleDic[channelNum], sVideoFileName)) {
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_SaveRealData failed, error code= " + iLastErr;
}
else
{
recordDic.Add(channelNum, true);
flag = true;
}
}
return flag;
}
/// <summary>
/// 海康录像机结束录像
/// </summary>
/// <param name="sVideoFileName">录像保存路径和⽂件名</param>
/// <param name="channelNum">摄像机通道号</param>
/// <returns></returns>
public bool StopTakeVideo(C2SJsonMessage c2sjm)
{
string channelNum = c2sjm.channelNum;
string videoFileName = c2sjm.videoFileName;
DateTime date = DateTime.Now;
string year = date.Year.ToString() + "\\";
string month = date.Month.ToString() + "\\";
string day = date.Day.ToString() + "\\";
bool flag = false;
string sVideoFiledDirectory = vidPath + year + month + day;
if (!Directory.Exists(sVideoFiledDirectory))
{
Directory.CreateDirectory(sVideoFiledDirectory);
}
//录像保存路径和⽂件名 C:\\HYKJ\\Videos\\2019\\08\\08\\jylsh_jccs_hphm_channelNum.mp4 string sVideoFileName = sVideoFiledDirectory + videoFileName;
if (recordDic.ContainsKey(channelNum))
{
//停⽌录像 Stop recording
if (!CHCNetSDK.NET_DVR_StopSaveRealData(realHandleDic[channelNum]))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_StopSaveRealData failed, error code= " + iLastErr;
}
else
{
HKStopPreview(channelNum);//关闭预览
str = "Successful to stop recording and the saved file is " + sVideoFileName;
recordDic.Remove(channelNum);
flag = true;
}
webservice用户名密码调用}
return flag;
}
2.2.3 C2SJonsMessage
public class C2SJsonMessage
{
/// <summary>
/
// 接⼝id
/// </summary>
public string jkid;
/// <summary>
/// 检验机构编号
/// </summary>
public string jyjgbh;
/// <summary>
/// 检验流⽔号
/// </summary>
public string jylsh;
/
// <summary>
/// 检测线代号
/// </summary>
public string jcxdh;
/// <summary>
/// 检测次数
/// </summary>
public string jccs;
/// <summary>
/// 号牌号码
/// </summary>
public string hphm;
/// <summary>
/// 号牌种类
/// </summary>
public string hpzl;
/// <summary>
/// 车辆识别代号
/// </summary>
public string clshbh;
/// <summary>
/// 检验项⽬
/
// </summary>
public string jyxm;
/// <summary>
/// 海康录像机摄像机通道号
/// </summary>
public string channelNum;
/// <summary>
/// 同⼀个流⽔号和检测次数的检测记录调⽤接⼝的次数,主要给抓拍接⼝⽤,⽤于区分第⼏张抓拍图⽚ /// </summary>
public string cs;
/// <summary>
/// 图⽚⽂件名称,建议命名规则jylsh_jccs_hphm_channelNum_cs.jpg,且只能是jpg格式
/// </summary>
public string pictureFileName;
/// <summary>
/// 视频⽂件名称,建议命名规则jylsh_jccs_hphm_channelNum.mp4,且只能是mp4格式
/// </summary>
public string videoFileName;
}
3.我的实例资源下载
有兴趣的可以看看,没兴趣的就算了,就是个⼩例⼦,反正也没什么技术含量
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论