1

我有一个小项目,我从网络摄像头捕获图像并解码 qr。

以下代码捕获图像并将其存储到本地文件,仅当它不在 while 循环中时。system.threading 似乎使捕获的图像变黑。如果我不使用它(循环),它每秒会捕获太多图像。

那么有没有办法改变 aforge.video 帧率,这样我就可以每 x 秒捕捉一次图像而无需 while 循环?

   public partial class WebForm1 : System.Web.UI.Page
{
    public int FrameRate { get; set; }

    private FilterInfoCollection VideoCaptureDevices;
    private VideoCaptureDevice FinalVideo;

    protected void Page_Load(object sender, EventArgs e)
    {
        inputDevices.Items.Clear();

        VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
        {
            inputDevices.Items.Add(VideoCaptureDevice.Name);
        }
        inputDevices.SelectedIndex = 0;     
    }

    public void Start_OnClick(object sender, EventArgs e)
    {
        FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[inputDevices.SelectedIndex].MonikerString);
        FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
        FinalVideo.Start();
    }
    void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        int i = 0;
        while (i < 10)
        {
            Bitmap video = (Bitmap)eventArgs.Frame.Clone();
            video.Save("C:\\Users\\Wayneio\\Desktop\\image\\test" + i + ".jpg");
            i++;
            System.Threading.Thread.Sleep(5000);
        }

    }
    public void Stop_OnClick(object sender, EventArgs e)
    {
        this.FinalVideo.Stop();
    }
}

此外,在尝试通过 asp 按钮停止捕获时出现此错误:

 Object reference not set to an instance of an object on this.FinalVideo.Stop();

试过这个无济于事: ((VideoCaptureDevice)FinalVideo).DesiredFrameRate = 10;

4

1 回答 1

-1

在你编码开始视频之前,像这样设置帧速率

 FinalVideo.DesiredFrameRate = 10;
 FinalVideo.Start();

跳过保存的帧的另一个选项是使用一个函数,如果您有一个全局计数器值 myCounter,则该函数并不总是正确的

进行模计算,如下所示 ix mycounter 除以 10 等于 1

 myCounter++
 if (m(ycounter %% 10))==1) { //code to save your bitmap   }
于 2012-11-06T19:00:25.917 回答