C# Emgucv 打开摄像头 与 二维码读取



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Text.RegularExpressions;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using Emgu.CV.CvEnum;

namespace Open_Camera
{
    public partial class Form1 : Form
    {
        int top = 0;
        private Emgu.CV.Capture capture = null;
        private bool captureinprocess;//判断摄像头的状态
        string path;
        public Form1()
        {
            InitializeComponent();
        }

        private void Open_Camera_Click(object sender, EventArgs e)
        {
            if (capture != null)//摄像头不为空 
            {
                if (captureinprocess)
                {
                    Application.Idle -= new EventHandler(processfram);
                    Open_Camera.Text = "Stop!";
                    Message.Text = "相机已暂停";
                }
                else
                {
                    Application.Idle += new EventHandler(processfram);
                    Open_Camera.Text = "Start!";
                    Message.Text = "相机已工作";
                }
                captureinprocess = !captureinprocess;
                timer.Start();
            }
            else//摄像头为空则通过Capture()方法调用 
            {
                try
                {
                    capture = new Emgu.CV.Capture();
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
               
            }
           
        }
        private void processfram(object sender, EventArgs e)
        {
            Mat frame = new Mat();
            frame = capture.QueryFrame();
            Emgu.CV.Image image = frame.ToImage();
            //Image img = capture.QueryFrame();
            imageShow.Image = frame;


        }

        private void QR_Code_Decode_Click(object sender, EventArgs e)
        {
            string tt = "";
            try
            {
                Invoke((EventHandler)delegate
                {
                    Message.Text = "识别中···";
                });
                //pictureBox1.Size = new Size(new Bitmap(filepath).Size.Width, new Bitmap(filepath).Size.Height); 
                QRCodeDecoder qrDecoder = new QRCodeDecoder();
                string txtMsg = qrDecoder.decode(new QRCodeBitmapImage(imageShow.Image.Bitmap/*imageShow.Image.Bitmap*/), Encoding.UTF8);
                tt = txtMsg;
            }
            catch { tt = "识别失败"; }
            Invoke((EventHandler)delegate
            {
                Message.Text = tt;
            });
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Bitmap img2 = (Bitmap)imageShow.Image.Bitmap.Clone();
            Pen p = new Pen(Color.Red);
            Graphics g = Graphics.FromImage(img2);
            Point p1 = new Point(0, top);
            Point p2 = new Point(imageShow.Width, top);
            g.DrawLine(p, p1, p2);
            g.Dispose();
            top += 2;

            top = top % imageShow.Height;

            Image cvimage = new Image(img2);
            imageShow.Image = cvimage;

        }

        private void StartFace_Click(object sender, EventArgs e)
        {
            String win1 = "Test Window"; //The name of the window
            CvInvoke.NamedWindow(win1); //Create the window using the specific name

            Mat img = new Mat(200, 400, DepthType.Cv8U, 3); //Create a 3 channel image of 400x200
            img.SetTo(new Bgr(255, 0, 0).MCvScalar); // set it to Blue color

            //Draw "Hello, world." on the image using the specific font
            CvInvoke.PutText(
               img,
               "Hello, world",
               new System.Drawing.Point(10, 80),
               FontFace.HersheyComplex,
               1.0,
               new Bgr(0, 255, 0).MCvScalar);


            CvInvoke.Imshow(win1, img); //Show the image
            CvInvoke.WaitKey(0);  //Wait for the key pressing event
            CvInvoke.DestroyWindow(win1); //Destroy the window if key is pressed
        }

        private void LoadPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "请选择图片";
            dlg.Filter = "图片文件(*.jpg)|*.jpg";
            dlg.Multiselect = false;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                path = dlg.FileName;
                Bitmap img = new Bitmap(path);
                Emgu.CV.Image currentFrame = new Emgu.CV.Image(img);
                imageShow.Image = currentFrame;
            }
        }
    }
}

你可能感兴趣的:(C# Emgucv 打开摄像头 与 二维码读取)