摄像头每执行 test 方法,调用一次 VideoDevice_NewFrame 事件。



//所有摄像设备
private FilterInfoCollection videoDevices;
//摄像设备
private VideoCaptureDevice videoDevice;
//摄像头分辨率
private VideoCapabilities[] videoCapabilities;



// 测试方法
public void test()
{
    if (videoDevices.Count == 0)
    {
        GLog.WriteBalanceLog("没有找到摄像头");
        return;
    }
    //获取摄像头
    videoDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
    //设备的摄像头分辨率数组 
    videoCapabilities = videoDevice.VideoCapabilities;

    //摄像头分辨率 最小的
    int index = videoCapabilities.Length - 1;                    
    VideoCapabilities v = videoCapabilities[index];
    videoDevice.VideoResolution = v;

    videoDevice.NewFrame += VideoDevice_NewFrame;
    videoDevice.Start();  
}


/// <summary>
/// 相机抓拍图片
/// 创迹商品图片识别
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void VideoDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    try
    {
         GLog.WriteBalanceLog("相机抓拍图片");
         
        // 捕获新的图像帧
        Bitmap frame = (Bitmap)eventArgs.Frame.Clone();                

         
        // 关闭相机 
        if (videoDevice != null && videoDevice.IsRunning) {
            GLog.WriteBalanceLog("关闭相机"); 
            videoDevice.SignalToStop();
            videoDevice.WaitForStop();
        }
    }
    catch (Exception ex)
    { 
        GLog.WriteBalanceLog("识别过程出现异常:" + ex.Message);
    }  
} 

问题一
执行 test 方法, 注册 VideoDevice_NewFrame 事件。
VideoDevice_NewFrame 方法执行了 无数次 n 次,直到程序死掉为止。

问题二

// 关闭相机 
if (videoDevice != null && videoDevice.IsRunning) {
    GLog.WriteBalanceLog("关闭相机"); 
    videoDevice.SignalToStop();
    videoDevice.WaitForStop();
}

如果加上关闭相机的代码。
第一次执行test 方法,会调用 VideoDevice_NewFrame。
第二 三 四次 后面所有执行 test 方法, 不会调用 VideoDevice_NewFrame 事件。

我想要的效果
每执行 test 方法,调用一次 VideoDevice_NewFrame 事件。

测试了下再NewFrame事件中无法停止VideoSource,可以用一个线程来检查拍照后停止可以



        bool stop = false;//是否拍照标志////////////////////////////
        // 测试方法
        public void test()
        {
            stop = false;///重置
            if (videoDevices.Count == 0)
            {
                GLog.WriteBalanceLog("没有找到摄像头");
                return;
            }
            //获取摄像头
            videoDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
            //设备的摄像头分辨率数组 
            videoCapabilities = videoDevice.VideoCapabilities;

            //摄像头分辨率 最小的
            int index = videoCapabilities.Length - 1;
            VideoCapabilities v = videoCapabilities[index];
            videoDevice.VideoResolution = v;

            videoDevice.NewFrame += VideoDevice_NewFrame;
            videoDevice.Start();

            //线程检查////////////////////////////////////////////
            new Task(() => {
                while (!stop)
                {

                    System.Threading.Thread.CurrentThread.Join(100);
                }

                this.Invoke(new Action(delegate
                {
                    videoDevice.SignalToStop();
                    videoDevice.WaitForStop();
                }));
            }).Start();
        }


        /// <summary>
        /// 相机抓拍图片
        /// 创迹商品图片识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void VideoDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {
                GLog.WriteBalanceLog("相机抓拍图片");

                // 捕获新的图像帧
                Bitmap frame = (Bitmap)eventArgs.Frame.Clone();
                ////////////////
                stop = true;///通过线程来关闭,而不是在NewFrame事件中关闭,测试了下无法关闭
                // 关闭相机 
                /*if (videoDevice != null && videoDevice.IsRunning)
                {
                    GLog.WriteBalanceLog("关闭相机");
                    videoDevice.SignalToStop();
                    videoDevice.WaitForStop();
                }*/
            }
            catch (Exception ex)
            {
                GLog.WriteBalanceLog("识别过程出现异常:" + ex.Message);
            }
        }

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632