欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenCV打开摄像头

程序员文章站 2024-03-25 10:32:52
...

OpenCV打开摄像头

话不多说,看代码,用的是C#+Opencv免费库

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Face_Recognition
{
public partial class Form1 : Form
{
public VideoCapture m_vCapture;
public bool bPlayflag;
public bool bTakePicture;
public Thread ThreadCamera;

    public IntPtr FTtrackingEngine = IntPtr.Zero;
    public int WORKBUF_SIZE = 40 * 1024 * 1024;
    public IntPtr pWorkMem_FT;

    private static string APPID = "CuCHJK3hb7b13GuHGprd7B7ZVEcvgd132mbPXWpbEPoA";
    private static string SDKKEY="AU6a2j3aaoaqxhSFX53gszXLjZFJR4neE6UY1opVtkBq";

    public Form1()
    {
        InitializeComponent();

        pictureBox1.Image = pictureBox1.InitialImage;
        pictureBox2.Image = pictureBox2.InitialImage;

        pWorkMem_FT = Marshal.AllocHGlobal(WORKBUF_SIZE);
        //int retCode=ArcsoftFace


    }

    private void btOpenCamera_Click(object sender, EventArgs e)
    {
        if (!bPlayflag)
        {
            m_vCapture = new VideoCapture(CaptureDevice.Any);
            if (!m_vCapture.IsOpened())
            {
                MessageBox.Show("摄像头打不开", "摄像头故障", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            m_vCapture.Set(CaptureProperty.FrameWidth, 640);
            m_vCapture.Set(CaptureProperty.FrameHeight, 480);

            bPlayflag = true;
            ThreadCamera = new Thread(Play_Camera);
            ThreadCamera.Start();

            pictureBox1.Image = null;
            btOpenCamera.Text = "关闭摄像头";
        }
        else
        {
            bPlayflag = false;
            ThreadCamera.Abort();
            m_vCapture.Release();
            pictureBox1.Image = pictureBox1.InitialImage;
            btOpenCamera.Text = "打开摄像头";
        }
    }

    private void Play_Camera()
    {
        while (bPlayflag)
        {
            Mat cFrame = new Mat();
            m_vCapture.Read(cFrame);
            int sleepTime = (int)Math.Round(1000 / m_vCapture.Fps);
            Cv2.WaitKey(sleepTime);

            if (cFrame.Empty())
            {
                continue;
            }
            Cv2.Flip(cFrame, cFrame, OpenCvSharp.FlipMode.Y);
            Rect cMaxrect = new Rect(100, 90, 300, 300);
            if (bTakePicture)
            {
                string time = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
                Mat cHead = new Mat(cFrame, cMaxrect);
                Cv2.ImWrite("C:\\Users\\xujn2\\Documents\\XuJun\\Temp\\" + time + ".jpg", cHead);
                pictureBox2.Image = BitmapConverter.ToBitmap(cHead);

                cHead.Release();
                bTakePicture = false;
            }

            //绘制指定区域(人脸框)
            Scalar color = new Scalar(0, 100, 0);
            Cv2.Rectangle(cFrame, cMaxrect, color, 2);

            pictureBox1.Image = BitmapConverter.ToBitmap(cFrame);

            cFrame.Release();//释放   
        }
    }

    private void btTakePicture_Click(object sender, EventArgs e)
    {
        bTakePicture = true;
    }
}

}

相关标签: c# opencv