【问题标题】:Simple camera capture in WinformsWinforms 中的简单相机捕捉
【发布时间】:2018-06-12 08:48:39
【问题描述】:

我只想在 WinForms C# 应用程序中预览然后从设备摄像头(网络摄像头)捕获快照。

我用过这个:WebEye WebCameraControl,但它似乎在某些机器/相机上失败了。该描述暗示那里还有更多负载,但我在 NuGet 上找不到任何适用于 WinForms 的内容。

有什么建议吗?我觉得我错过了一些明显的东西,比如内置的 Windows 控件就可以做到这一点!


编辑:
在尝试添加 OpenCVSharp 这就是我得到的:

【问题讨论】:

    标签: c# winforms camera opencvsharp


    【解决方案1】:

    试试OpenCVSharp。一段带有 PictureBox 和 Button 的代码 sn-p:

    VideoCapture capture;
    Mat frame;
    Bitmap image;
    private Thread camera;
    bool isCameraRunning = 0;
    
    private void CaptureCamera() {
        camera = new Thread(new ThreadStart(CaptureCameraCallback));
        camera.Start();
    }
    
    private void CaptureCameraCallback() {
    
        frame = new Mat();
        capture = new VideoCapture(0);
        capture.Open(0);
    
        if (capture.IsOpened()) {
            while (isCameraRunning) {
    
                capture.Read(frame);
                image = BitmapConverter.ToBitmap(frame);
                if (pictureBox1.Image != null) {
                    pictureBox1.Image.Dispose();
                }
                pictureBox1.Image = image;
            }
        }
    }
    
    private void button1_Click(object sender, EventArgs e) {
        if (button1.Text.Equals("Start")) {
            CaptureCamera();
            button1.Text = "Stop";
            isCameraRunning = true;
        }
        else {
            capture.Release();
            button1.Text = "Start";
            isCameraRunning = false;
        }
    }
    

    完整代码

     public partial class Form1 : Form
        {
            // Create class-level accesible variables
            VideoCapture capture;
            Mat frame;
            Bitmap image;
            private Thread camera;
            bool isCameraRunning = false;
    
            // Declare required methods
            private void CaptureCamera()
            {
                camera = new Thread(new ThreadStart(CaptureCameraCallback));
                camera.Start();
            }
    
            private void CaptureCameraCallback()
            {
                frame = new Mat();
                capture = new VideoCapture(0);
                capture.Open(0);
    
                if (capture.IsOpened())
                {
                    while (isCameraRunning)
                    {
                        capture.Read(frame);
                        image = BitmapConverter.ToBitmap(frame);
                        if (pictureBox1.Image != null)
                        {
                            pictureBox1.Image.Dispose();
                        }
    
                        pictureBox1.Image = image;
                    }
                }
            }
    
            public Form1()
            {
                InitializeComponent();
                CaptureCamera();
                button1.Text = "Stop";
                isCameraRunning = true;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (button1.Text.Equals("Start"))
                {
                    CaptureCamera();
                    button1.Text = "Stop";
                    isCameraRunning = true;
                }
                else
                {
                    capture.Release();
                    button1.Text = "Start";
                    isCameraRunning = false;
                }
            }
    
            protected override void OnClosed(EventArgs e)
            {
                base.OnClosed(e);
                try
                {
                    capture.Release();
                    camera.Abort();
                }
                catch (Exception ex)
                {
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (isCameraRunning)
                {
                    Bitmap snapshot = new Bitmap(pictureBox1.Image);
    
    
                    snapshot.Save(string.Format(@"D:\image.jpg", Guid.NewGuid()), ImageFormat.Jpeg);
                    capture.Release();
                    camera.Abort();
                    this.Close();
                }
                else
                {
                    Console.WriteLine("Cannot take picture if the camera isn't capturing image!");
                }
            }
    
            private void Form1_Load_1(object sender, EventArgs e)
            {
            }
        }
    

    在此处 SaveButton 将图像保存到 D:\location 如果您觉得有任何错误,请尝试以管理员身份运行

    您可以在这里找到解决方案:- working example

    【讨论】:

    • 谢谢 - 看起来不错,但我找不到 VideoCapture 的定义......有什么想法吗?
    • 请从 Nuget 获取 OpenCVSharp。它就在那里。
    • OpenCvSharp3-AnyCPU.3.4.1.20180319
    • 好吧,现在我只是在图像的前几行得到一个黑色或偶尔出现的彩色点。真的只为下一个流浪者留下评论。出于某种原因,相机捕捉从来都不是直截了当的......无论如何,谢谢!
    • 我面对的是黑屏而不是视频。它不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多