首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从WebCam获取帧

从WebCam获取帧
EN

Stack Overflow用户
提问于 2013-12-05 18:45:14
回答 2查看 1.1K关注 0票数 1

我正在使用MetriCam库中的WebCam类,我需要定期从我的WebCam中获取帧,有人知道怎么做吗?下面是我已经有的代码:

代码语言:javascript
复制
namespace MetriCam_Coding_Example
{
    public partial class Form1 : Form
    {
        #region Private Fields
        private WebCam camera;
        #endregion

        #region Constructor
        public Form1()
        {
            InitializeComponent();
            camera = new WebCam();
        }
        #endregion

        #region Private Methods
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (!camera.IsConnected())
            {
                camera.Connect();
                buttonConnect.Text = "&Disconnect";
                backgroundWorker1.RunWorkerAsync();
            }
            else
            {
                backgroundWorker1.CancelAsync();
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!backgroundWorker1.CancellationPending)
            {
                camera.Update();
                pictureBox1.Image = camera.CalcBitmap();
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            camera.Disconnect();
            buttonConnect.Text = "&Connect";
        }
        #endregion
    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-12-12 00:54:54

尝试以太快的速度绘制位图时,可能会遇到问题。200ms的延迟只是减少了这些问题的可能性,但并没有真正解决它。您可以尝试使用此代码跳过无法绘制的帧:

代码语言:javascript
复制
namespace TestGUI
{
public partial class TestGUIForm : Form
{
    private IAsyncResult setBMPResult;
    private Webcam;

    private delegate void SetBmpDelegate(Bitmap b);

    /// <summary>
    /// Standard constructor.
    /// </summary>
    public TestGUIForm()
    {
        InitializeComponent();
        this.FormClosing += TestGUIForm_FormClosing;

        cam = new WebCam();
    }

    private void TestGUIForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        backgroundWorkerGetFrames.CancelAsync();
    }

    private void buttonConnect_Click(object sender, EventArgs e)
    {
        if (cam.IsConnected())
        {
            // if we are already connected, just disable the button and cancel the display thread, the actual disconnection takes place in the *_RunWorkerCompleted method.
            buttonConnect.Enabled = false;
            backgroundWorkerGetFrames.CancelAsync();
        }
        else
        {
            // connect the camera and start the display background worker.
            buttonConnect.Enabled = false;
            try
            {
                cam.Connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connection error: " + ex.Message);
                buttonConnect.Enabled = true;
                return;
            }
            buttonConnect.Text = "Disconnect";
            backgroundWorkerGetFrames.RunWorkerAsync();
            buttonConnect.Enabled = true;
        }
    }

    private void backgroundWorkerGetFrames_DoWork(object sender, DoWorkEventArgs e)
    {
        while (!backgroundWorkerGetFrames.CancellationPending)
        {
            // capture a new frame
            cam.Update();
            // get the current frame
            Bitmap bitmap = cam.CalcBitmap();
            // set the picturebox-bitmap in the main thread to avoid concurrency issues (a few helper methods required, easier/nicer solutions welcome).
            this.InvokeSetBmp(bitmap);
        }
    }
    private void InvokeSetBmp(Bitmap bmp)
    {
        if (setBMPResult == null || setBMPResult.IsCompleted)
        {
            setBMPResult = this.BeginInvoke(new SetBmpDelegate(this.SetImage), bmp);
        }
    }

    private void SetImage(Bitmap bitmap)
    {
        Bitmap oldBitmap = (Bitmap)pictureBoxImageStream.Image;
        pictureBoxImageStream.Image = bitmap;
        if (oldBitmap != null && oldBitmap != bitmap)
            oldBitmap.Dispose();
    }

    private void backgroundWorkerGetFrames_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // disconnect camera and re-enable button.
        cam.Disconnect();
        buttonConnect.Text = "Connect";
        buttonConnect.Enabled = true;
    }
}
}
票数 0
EN

Stack Overflow用户

发布于 2013-12-12 01:09:08

你可以用你想要的时间创建一个DispatcherTimer。这是一个定时器中断。

代码语言:javascript
复制
DispatcherTimer timer = new DispatcherTimer();

timer.Interval = new TimeSpan(0, 0, 0, 0, 20); // 20ms
timer.Tick += new EventHandler(timer_Tick);
timer.Start();

然后,当计时器滴答作响时,它将到达事件,您将从您的相机获得图像。

代码语言:javascript
复制
void timer_Tick(object sender, EventArgs e)
{
    camera.Update();
    pictureBox1.Image = camera.CalcBitmap();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20397859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档