Winforms中的简单摄像头捕获

zxlwwiss  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(156)

我只想在WinForms C#应用程序中预览并从设备摄像头(网络摄像头)捕获快照。
我用过这个:WebEye WebCameraControl,但它似乎在一些机器/相机上失败。描述暗示有更多的负载在那里,但我不能在NuGet上找到任何为WinForms。
有什么建议吗?我觉得我错过了一些明显的东西,比如一个内置的windows控件,它可以做到这一点!
编辑:
在尝试添加OpenCVSharp时,我得到的是:

jtw3ybtb

jtw3ybtb1#

尝试OpenCVSharp。一段带有PictureBox和Button的代码片段:

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)
        {
        }
    }

这里保存按钮保存图像到D:\位置如果你觉得任何错误尝试运行为管理员
您可以在此处找到解决方案:-working example

相关问题