C# Activex調用USB攝像頭--附帶源碼


前言


 

最近在整理一些自己寫過的東西,也算是重新熟悉一下並且優化一下吧。

需求:獲取本地USB攝像頭視頻顯示,並且獲取圖片數據給底層做人臉識別。

記得當時直接采用H5已經做好了,調試好了。。。。結果放上去使用發現必須需要證書才可以,

然后因為某些原因(沒辦法自己寫一個ssl證書)只能重寫了一個之前使用Activex做的USB控件。

 H5調用USB攝像頭參考:https://segmentfault.com/a/1190000011793960

開發


閑話:DLL缺少搜索找不到,推薦找dll https://www.zhaodll.com/

引用: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);
    }
View Code

添加一個基礎類,用於數據返回處理。

 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; }//錯誤
        }

    }
View Code

用戶控件中實現:

 /// <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
    }
View Code

用於測試的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>
View Code

實現的效果圖:

結語


VS使用的2015社區版,社區版。。。有點尷尬所以打包使用的 Visual Studio Installer

一回生二回熟,第一次寫這個滿臉蒙蔽。

不過這次稍微整理了一下,改動了一點,后續准備找個人臉識別的開源接口融合進來。

下面給個下載地址,可以直接調試,需要注意使用IE,由於沒有加證書,IE需要設置。

工具>Internet選項
                 >安全>加入受信任的站點>  
                 >自定義級別>ActiveX控件和插件>ActivexX控件自動提示>啟用
                                                                  >對未標記為可安全執行的腳本的ActivexX控件初始化並執行腳本>啟用

 

百度雲盤鏈接:下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM