Esempio n. 1
0
        /// <summary>
        ///  开启摄像头
        /// </summary>
        public void StartWebCam() //开启摄像头
        {
            this.lwndC = AviCapture.capCreateCaptureWindow("", AviCapture.WS_CHILD | AviCapture.WS_VISIBLE, 0, 0,
                                                           mWidth, mHeight, mControlPtr, 0); //AVICap类的捕捉窗口
            //AviCapture.FrameEventHandler FrameEventHandler = new AviCapture.FrameEventHandler(framecallback);
            //AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_CALLBACK_ERROR, 0, 0);//注册错误回调函数
            //AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_CALLBACK_STATUS, 0, 0);//注册状态回调函数
            //AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);//注册视频流回调函数
            //AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_CALLBACK_FRAME, 0, FrameEventHandler);//注册帧回调函数

            //if (!CapDriverCAPS.fCaptureInitialized)//判断当前设备是否被其他设备连接已经连接
            //{

            if (AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_DRIVER_CONNECT, 0, 0))
            {
                //-----------------------------------------------------------------------
                AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_DRIVER_GET_CAPS, AviCapture.SizeOf(CapDriverCAPS),
                                       ref CapDriverCAPS); //获得当前视频 CAPDRIVERCAPS定义了捕获驱动器的能力,如有无视频叠加能力、有无控制视频源、视频格式的对话框等;
                AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_GET_STATUS, AviCapture.SizeOf(CapStatus),
                                       ref CapStatus);     //获得当前视频流的尺寸 存入CapStatus结构

                AviCapture.BITMAPINFO
                    bitmapInfo = new AviCapture.BITMAPINFO(); //设置视频格式 (height and width in pixels, bits per frame).
                bitmapInfo.bmiHeader            = new AviCapture.BITMAPINFOHEADER();
                bitmapInfo.bmiHeader.biSize     = AviCapture.SizeOf(bitmapInfo.bmiHeader);
                bitmapInfo.bmiHeader.biWidth    = mWidth;
                bitmapInfo.bmiHeader.biHeight   = mHeight;
                bitmapInfo.bmiHeader.biPlanes   = 1;
                bitmapInfo.bmiHeader.biBitCount = 24;
                AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_PREVIEWRATE, 34,
                                       0);                                             //设置在PREVIEW模式下设定视频窗口的刷新率 设置每34毫秒显示一帧,即显示帧速为每秒29帧
                AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_SCALE, 1, 0); //打开预览视频的缩放比例
                AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_VIDEOFORMAT, AviCapture.SizeOf(bitmapInfo),
                                       ref bitmapInfo);


                AviCapture.CAPTUREPARMS captureparms = new AviCapture.CAPTUREPARMS();
                AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_GET_SEQUENCE_SETUP,
                                       AviCapture.SizeOf(captureparms), ref captureparms);
                if (CapDriverCAPS.fHasOverlay)
                {
                    AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_OVERLAY, 1, 0); //启用叠加 注:据说启用此项可以加快渲染速度   
                }

                AviCapture.SendMessage(this.lwndC, AviCapture.WM_CAP_SET_PREVIEW, 1, 0);  //设置显示图像启动预览模式 PREVIEW
                AviCapture.SetWindowPos(this.lwndC, 0, 0, 0, mWidth, mHeight,
                                        AviCapture.SWP_NOZORDER | AviCapture.SWP_NOMOVE); //使捕获窗口与进来的视频流尺寸保持一致
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("未能连接设备,请检查设备连接及是否有其他程序占用!");
                flag = false;
            }

            //}
            //else
            //{
            //    System.Windows.Forms.MessageBox.Show("未能连接设备,请检查设备连接及是否有其他程序占用!");
            //}
        }