【问题标题】:Macbook camera won't turn off and a minor C# aforge issueMacbook 相机无法关闭和一个小的 C# aforge 问题
【发布时间】:2015-08-13 03:46:11
【问题描述】:

我在带有 bootcamp 的 Macbook pro 上使用 Aforge 框架制作了可以使用 Visual Studio C# 跟踪对象的项目。问题是当我运行代码并关闭它时。然后再次运行它,代码将不再在我的相机上显示图像。此外,停止代码后,我的网络摄像头上的绿灯仍然亮着。

我的第二个问题是,当我单击“跟踪对象按钮”并将过滤器应用于图像时,由于某些原因,它会将过滤器应用于两个图片框,而不仅仅是一个。我需要帮助来调试我的代码。我尝试重新排列应用过滤器的行,但似乎无法正确处理。

这是我的代码

namespace VideoProcessing1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Graphics g;
        Bitmap video;
        bool OnOff;
        int mode;
        int thoigianddemnguoc;

        private FilterInfoCollection CaptureDevice;
        private VideoCaptureDevice FinalFrame;

        private void Form1_Load(object sender, EventArgs e)
        {
            CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo Device in CaptureDevice)
            {
                comboBox1.Items.Add(Device.Name);

            }
            comboBox1.SelectedIndex = 0;
            FinalFrame = new VideoCaptureDevice();
            //FinalFrame.Stop();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
            FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
            FinalFrame.Start();

        }

        void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            video = (Bitmap)eventArgs.Frame.Clone();
            Bitmap video2 = (Bitmap)eventArgs.Frame.Clone();
            g = Graphics.FromImage(video2);
            g.DrawString("Test", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2));
            g.Dispose();

            if (mode == 1)
            {
                ColorFiltering colorfilter = new ColorFiltering();
                colorfilter.Red = new AForge.IntRange(0, 255);
                colorfilter.Green = new AForge.IntRange(0, 75);
                colorfilter.Green = new AForge.IntRange(0, 75);
                colorfilter.ApplyInPlace(video2);
                BlobCounter blobcounter = new BlobCounter();
                blobcounter.MinHeight = 20;
                blobcounter.MaxWidth = 20;
                blobcounter.ObjectsOrder = ObjectsOrder.Size;
                blobcounter.ProcessImage(video2);
                Rectangle[] rect = blobcounter.GetObjectsRectangles();

                if (rect.Length > 0)
                {
                    Rectangle objec = rect[0];
                    Graphics graphic = Graphics.FromImage(video2);
                    using (Pen pen = new Pen(Color.White, 3))
                    {
                        graphic.DrawRectangle(pen, objec);
                    }
                    graphic.Dispose();
                }
                pictureBox2.Image = video2;

            }
            pictureBox1.Image = video2;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          if(FinalFrame.IsRunning==true)
            {
                FinalFrame.Stop();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            mode = 1;
        }
    }
}

【问题讨论】:

    标签: c# webcam aforge bootcamp


    【解决方案1】:

    不建议在您的视频捕获设备上拨打Stop。根据docs:“停止摄像头的正确方法是发出信号停止,然后等待后台线程完成。”

    你应该尝试使用这样的东西来停止:

    FinalFrame.SignalToStop();
    FinalFrame.WaitForStop();
    

    如果使用不当,线程行为可能会变得不可预测,我相信您的问题就在这里。

    关于你的第二个问题,问题线是

    pictureBox1.Image = video2;
    

    您已经将video2 存储在pictureBox2.Image 中,所以我假设您不想对另一个图片框这样做。

    【讨论】:

    • 感谢您的回复。我仍然无法关闭相机。我使用了您建议的代码,甚至为此目的添加了一个按钮,但无济于事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    相关资源
    最近更新 更多