C# Activex调用USB摄像头--附带源码
前言
最近在整理一些自己写过的东西,也算是重新熟悉一下并且优化一下吧。
需求:获取本地usb摄像头视频显示,并且获取图片数据给底层做人脸识别。
记得当时直接采用h5已经做好了,调试好了。。。。结果放上去使用发现必须需要证书才可以,
然后因为某些原因(没办法自己写一个ssl证书)只能重写了一个之前使用activex做的usb控件。
h5调用usb摄像头参考:
开发
闲话:dll缺少搜索找不到,推荐找dll
引用:aforge.dll,aforge.video.directshow.dll,aforge.video.dll
首先创建一个用户控件>放入一个picturebox1>放入一个lable
将用户控件调成灰色(明显。。。)大小随便后面可以调整,picturebox1在父容器停靠,lab用于错误提示,效果如下:
添加一个 iobjectsafety 接口。
/* iobjectsafety 接口用于通知浏览器:“浏览器,我是安全的” */ [comimport, guid("887fc3c3-a970-4a36-92ef-d4eb31541c40")] [interfacetypeattribute(cominterfacetype.interfaceisiunknown)] public interface iobjectsafety { [preservesig] int getinterfacesafetyoptions(ref guid riid, [marshalas(unmanagedtype.u4)] ref int pdwsupportedoptions, [marshalas(unmanagedtype.u4)] ref int pdwenabledoptions); [preservesig()] int setinterfacesafetyoptions(ref guid riid, [marshalas(unmanagedtype.u4)] int dwoptionsetmask, [marshalas(unmanagedtype.u4)] int dwenabledoptions); }
添加一个基础类,用于数据返回处理。
public class usbinfo { /// <summary> /// 返回消息 /// </summary> public class returnmessage { public bool result { get; set; }//结果 public string message { get; set; }//消息 } /// <summary> /// 返回视频分辨率 /// </summary> public class getresolvingpower { public int index { get; set; }//下标 -1(不存在摄像头),-2(发生意外错误) public videocapabilities capabilities { get; set; }//信息 public string error { get; set; }//错误 } }
用户控件中实现:
/// <summary> /// 继承iobjectsafety /// </summary> [progid("usbcamera")] [classinterface(classinterfacetype.autodual)] [guid("887fc3c3-a970-4a36-92ef-d4eb31541c40")] [comvisible(true)] public partial class usbcamera : usercontrol, iobjectsafety { filterinfocollection videodevices;//设备 videocapturedevice videosource;//设备信息 memorystream framedata;//图片数据 static thread th_usb;//监听线程 static int sl_usb = 10000;//多久检查一次 static int index_usb;//第一次打开选择的分辨率 public usbcamera() { initializecomponent(); framedata = new memorystream(); } //句柄销毁 protected override void onhandledestroyed(eventargs e) { stop(); base.onhandledestroyed(e); } /// <summary> /// 设置窗体相关大小 /// </summary> /// <param name="width">宽</param> /// <param name="height">高</param> /// <param name="fontformat">字体熟悉</param> /// <param name="fontsize">字体大小</param> /// <param name="promptinfo">提示信息</param> /// <param name="sleep">监听间隔</param> public void formsize(int width, int height, string fontformat, int fontsize, string promptinfo, int sleep) { this.size = new size(width, height);//窗体大小 this.lab_prompt.font = new font(fontformat, fontsize, fontstyle.bold); //字体熟悉 this.lab_prompt.forecolor = color.red;//字体颜色 this.lab_prompt.text = promptinfo;//提示信息 //设置字体高宽 var lab_width = (width / 3); var lab_height = (height / 3); this.lab_prompt.location = new system.drawing.point(lab_width, lab_height); sl_usb = sleep;//监听时间 } /// <summary> /// 打开视频 /// </summary> /// <param name="index">usb可用分辨率数组下标</param> public void start(int index) { index_usb = index; if (videosource != null) { videosource.newframe -= new newframeeventhandler(video_newframe); videosource.signaltostop(); videosource = null; } // 创建视频源 trace.writeline("usbcamera have cameras" + videodevices.count.tostring()); videosource = new videocapturedevice(videodevices[0].monikerstring); // 设置新帧事件处理程序 videosource.newframe += new newframeeventhandler(video_newframe); // 启动视频源 try { videosource.videoresolution = this.videosource.videocapabilities[index]; videosource.start(); if (th_usb == null || !th_usb.isalive) { th_usb = new thread(getusb); th_usb.start(); } this.lab_prompt.text = null; } catch (exception ex) { trace.writeline("usbcamera start()遇到错误" + ex.message + ex.stacktrace); } } /// <summary> /// 停止视频 /// </summary> public void stop() { if (videosource != null) { videosource.newframe -= new newframeeventhandler(video_newframe); //当不再需要捕捉时,发出停止的信号 videosource.signaltostop(); videosource = null; trace.writeline("usbcamera stop()...."); if (picturebox1.image != null)//清空控件 { picturebox1.image.dispose(); picturebox1.image = null; } this.lab_prompt.text = null; } } /// <summary> /// 视频帧获取回调 /// </summary> /// <param name="sender"></param> /// <param name="eventargs"></param> private void video_newframe(object sender, newframeeventargs eventargs) { try { lab_prompt.text = null; trace.writeline("usbcamera video_newframe()...."); eventargs.frame.save(framedata, imageformat.bmp); if (picturebox1.invokerequired) { //写入数据 picturebox1.begininvoke((methodinvoker)delegate () { try { if (picturebox1.image != null) { image img = picturebox1.image; img.dispose(); } picturebox1.image = new bitmap(framedata); } catch (exception ex) { if (picturebox1.image != null) { picturebox1.image.dispose(); picturebox1.image = null; } } }); } else { picturebox1.image = new bitmap(framedata); } } catch (exception ex) { if (picturebox1.image != null) { picturebox1.image.dispose(); picturebox1.image = null; } } } /// <summary> /// 获取usb(usb连接不正常),不存在给出提示,存在重新start /// </summary> public void getusb() { while (true) { var usb = new filterinfocollection(filtercategory.videoinputdevice); if (usb.count <= 0) { stop(); lab_prompt.begininvoke((methodinvoker)delegate () { this.lab_prompt.text = "未连接上摄像头,检查连接是否正常。"; }); } else { //重新连接上 执行start if (!string.isnullorwhitespace(this.lab_prompt.text)) { start(index_usb); } } thread.sleep(sl_usb); } } #region usb视频操作 /// <summary> /// 测试activex是否可用 /// </summary> /// <returns>true/false</returns> public bool connection() { return true; } /// <summary> /// 获取截图 /// </summary> /// <returns></returns> public returnmessage getscreenshots() { try { return new returnmessage() { result = true, message = convert.tobase64string(framedata.toarray()) }; } catch (exception ex) { trace.writeline("usbcamera getimage() 错误...." + ex.message + ex.stacktrace); return new returnmessage() { result = false, message = ex.message + " " + ex.stacktrace }; } } /// <summary> /// 获取usb摄像头分辨率 /// </summary> /// <returns>json(getresolvingpower)</returns> public string getresolution() { var list = new list<getresolvingpower>(); try { videodevices = new filterinfocollection(filtercategory.videoinputdevice); if (videodevices.count > 0) { videosource = new videocapturedevice(videodevices[0].monikerstring); for (int i = 0; i < videosource.videocapabilities.length; i++) { list.add(new getresolvingpower { index = i, capabilities = videosource.videocapabilities[i], }); } } else { list.add(new getresolvingpower { index = -1, capabilities = null, error = "不存在usb摄像头", }); } } catch (exception ex) { list.add(new getresolvingpower { index = -2, capabilities = null, error = ex.source + " " + ex.message }); } return newtonsoft.json.jsonconvert.serializeobject(list); } #endregion #region iobjectsafety 成员直接复制 private const string _iid_idispatch = "{00020400-0000-0000-c000-000000000046}"; private const string _iid_idispatchex = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}"; private const string _iid_ipersiststorage = "{0000010a-0000-0000-c000-000000000046}"; private const string _iid_ipersiststream = "{00000109-0000-0000-c000-000000000046}"; private const string _iid_ipersistpropertybag = "{37d84f60-42cb-11ce-8135-00aa004bb851}"; private const int interfacesafe_for_untrusted_caller = 0x00000001; private const int interfacesafe_for_untrusted_data = 0x00000002; private const int s_ok = 0; private const int e_fail = unchecked((int)0x80004005); private const int e_nointerface = unchecked((int)0x80004002); private bool _fsafeforscripting = true; private bool _fsafeforinitializing = true; public int getinterfacesafetyoptions(ref guid riid, ref int pdwsupportedoptions, ref int pdwenabledoptions) { int rslt = e_fail; string strguid = riid.tostring("b"); pdwsupportedoptions = interfacesafe_for_untrusted_caller | interfacesafe_for_untrusted_data; switch (strguid) { case _iid_idispatch: case _iid_idispatchex: rslt = s_ok; pdwenabledoptions = 0; if (_fsafeforscripting == true) pdwenabledoptions = interfacesafe_for_untrusted_caller; break; case _iid_ipersiststorage: case _iid_ipersiststream: case _iid_ipersistpropertybag: rslt = s_ok; pdwenabledoptions = 0; if (_fsafeforinitializing == true) pdwenabledoptions = interfacesafe_for_untrusted_data; break; default: rslt = e_nointerface; break; } return rslt; } public int setinterfacesafetyoptions(ref guid riid, int dwoptionsetmask, int dwenabledoptions) { int rslt = e_fail; string strguid = riid.tostring("b"); switch (strguid) { case _iid_idispatch: case _iid_idispatchex: if (((dwenabledoptions & dwoptionsetmask) == interfacesafe_for_untrusted_caller) && (_fsafeforscripting == true)) rslt = s_ok; break; case _iid_ipersiststorage: case _iid_ipersiststream: case _iid_ipersistpropertybag: if (((dwenabledoptions & dwoptionsetmask) == interfacesafe_for_untrusted_data) && (_fsafeforinitializing == true)) rslt = s_ok; break; default: rslt = e_nointerface; break; } return rslt; } #endregion }
用于测试的html页面代码:
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <script> //初始化 设置usb控件大小和显示 var array = new array(); window.onload = function () { var usbcamera = document.getelementbyid("usbcamera"); usbcamera.stop(); //停止视频 array = json.parse(document.getelementbyid("usbcamera").getresolution());//获取usb分辨率 /* 判断获取数据是否成功(usb连接是否正常) 大于1为成功,摄像头基本有多个分辨率 也可以判断返回值中index是否为负数 获取到的分辨率最大的为第一个下标为:0 */ if (array.length > 1) { //分辨率下标 宽 高 var index = 0, width = 0, height = 0; for (var i = 0; i < array.length; i++) { var size = array[i].capabilities.framesize; if (size.width > width && size.height > height) { index = i; width = size.width; height = size.height; } } //此处我获取分辨率的最大值赋值,实际可以更改 usbcamera.formsize(width, height, "宋体", 9, "", 10000);//初始化 usbcamera.start(index);//打开摄像头 } else { if (array[0].index == -1) { //-1未连接 usbcamera.fromsize(400, 400, "宋体", 9, "摄像头未连接", 10000); } if (array[0].index == -2) { //-2 获取是发生错误 usbcamera.fromsize(400, 400, "宋体", 9, "摄像头连接有异常,请联系管理人员!", 10000); } } } //关闭时执行stop window.beforeunloadevent = function () { document.getelementbyid("usbcamera").stop(); } //测试连接 function testconnection() { alert(document.getelementbyid("usbcamera").connection()); } //打开摄像头 function startcamera() { document.getelementbyid("usbcamera").start(0); } //关闭摄像头 function stopcamera() { document.getelementbyid("usbcamera").stop(); } //获取截图 function getscreenshots() { var imginfo = document.getelementbyid("usbcamera").getscreenshots(); if (imginfo.result) { document.getelementbyid("displayscreenshots").src = "data:image/jpg;base64," + imginfo.message; } else { alert("截图获取失败"); } } //获取分辨率 function getresolution() { console.log(document.getelementbyid("usbcamera").getresolution()); } </script> </head> <body> <div> <button onclick="testconnection()">测试连接</button> <button onclick="startcamera()">打开摄像头</button> <button onclick="stopcamera()">关闭摄像头</button> <button onclick="getscreenshots()">获取截图</button> <button onclick="getresolution()">获取分辨率</button> </div> <hr /> <h3>截图显示</h3> <img style="width:300px;height:300px;" id="displayscreenshots" /> <div style="float:right;width:70%"> <object id="usbcamera" classid="clsid:887fc3c3-a970-4a36-92ef-d4eb31541c40"> <div class="alert alert-danger">视频控件载入失败!如未安装,请<a href="installationpackage/setup1.msi">下载安装</a>。</div> </object> </div> </body> </html>
实现的效果图:
结语
vs使用的2015社区版,社区版。。。有点尴尬所以打包使用的 visual studio installer
一回生二回熟,第一次写这个满脸蒙蔽。
不过这次稍微整理了一下,改动了一点,后续准备找个人脸识别的开源接口融合进来。
下面给个下载地址,可以直接调试,需要注意使用ie,由于没有加证书,ie需要设置。
工具>internet选项
>安全>加入受信任的站点>
>自定义级别>activex控件和插件>activexx控件自动提示>启用
>对未标记为可安全执行的脚本的activexx控件初始化并执行脚本>启用
百度云盘链接:下载
下一篇: 这几种西瓜的种类很多人都不知道