OpenCV打开摄像头

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;
    }
}

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值