在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 打开摄像头 拍照,生成二维码,UDP通信 实例源码下载

C# 打开摄像头 拍照,生成二维码,UDP通信 实例源码下载

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:5.04M
  • 下载次数:106
  • 浏览次数:573
  • 发布时间:2017-01-15
  • 实例类别:C#语言基础
  • 发 布 人:hhaitao
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 二维码 摄像头 UDP 通信

实例介绍

【实例简介】

【实例截图】

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.IO;
using System.Windows.Media.Imaging;
using AForge;//
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using Size = System.Drawing.Size;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Drawing.Imaging;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using System.Drawing;
using Aspose.Cells;
using System.Threading;
using System.Net;      
using System.Net.Sockets;



namespace Rbcc
{
    public partial class Form1 : Form
    {
        static Socket client;
        Thread t1;
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource;
        public Form1()
        {
            InitializeComponent();
        }
        public class myclass  
        {
            public static Int64 DT100;
            public static Int64 DT101;
            public static string  Dt100;
            public static string Dt101;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
            try
            {
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevices.Count == 0)
                    throw new ApplicationException();
                foreach (FilterInfo device in videoDevices)
                {
                    comboBox1.Items.Add(device.Name);
                }
                comboBox1.SelectedIndex = 0;
                ccw();
                
            }
            catch (ApplicationException)
            {
                comboBox1.Items.Add("No local capture devices");
                videoDevices = null;
            }
        }
        private void ccw()
        {
            
            try
            {

                Workbook wb = new Workbook();
                wb.Open("D:\\MyBook.xls");
                Worksheet sheet = wb.Worksheets[0];
                string d0 = sheet.Cells[0, 1].Value.ToString();
                myclass.DT100 = Int32.Parse(d0);
                myclass.DT101 = myclass.DT100;
            }
            catch
            {
                MessageBox.Show("D:\\MyBook.xls:数据被清空;或没有xls");
            }
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            CameraConn();
        }
        private void CameraConn()
        {
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
          //  videoSource.DesiredFrameSize = new System.Drawing.Size(600, 600);
           // videoSource.DesiredFrameRate = 1;

            videoSourcePlayer1.VideoSource = videoSource;
            videoSourcePlayer1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            videoSourcePlayer1.SignalToStop();
            videoSourcePlayer1.WaitForStop();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SS4();
        }

        private void SS4()
        {
            try
            {
                if (videoSourcePlayer1.IsRunning)
                {
                    BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                                       videoSourcePlayer1.GetCurrentVideoFrame().GetHbitmap(),
                                                       IntPtr.Zero,
                                                        Int32Rect.Empty,
                                                       BitmapSizeOptions.FromEmptyOptions());
                    PngBitmapEncoder pE = new PngBitmapEncoder();
                    pE.Frames.Add(BitmapFrame.Create(bitmapSource));                    
                    myclass.DT101 = myclass.DT101   1;
                    string picName = GetImagePath()   "\\"   myclass.DT101   ".jpg";

                    if (File.Exists(picName))
                    {
                        File.Delete(picName);
                    }
                    using (Stream stream = File.Create(picName))
                    {
                        pE.Save(stream);
                    }

                    string dt2 = " C:\\Users\\Administrator\\Desktop\\Rbcc\\Rbcc\\bin\\Debug\\PersonImg\\";             
                    string dt3 = myclass.DT101.ToString();
                    string dt4 = ".jpg";
                    string dt5 = dt2   dt3   dt4;
                    this.pictureBox1.Image = System.Drawing.Image.FromFile(dt5);
                   
                    ss2();


                    myclass.DT100 = myclass.DT100   1;
                    myclass.Dt100 = "A";
                    string d2 = myclass.Dt100   myclass.DT100;
                    Workbook wb = new Workbook();
                    wb.Open("d:\\MyBook.xls");
                    Worksheet sheet = wb.Worksheets[0];
                    Cell cell = sheet.Cells[d2];
                    cell.PutValue(textBox1.Text);
                    sheet.Cells["B1"].PutValue(myclass.DT100);
                    wb.Save("d:\\MyBook.xls", SaveFormat.Excel97To2003);
                    myclass.Dt101 = "";
                   textBox6 .Text = "";
                   
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("摄像头异常:"   ex.Message);
            }
        }
        private string GetImagePath()
        {
            string personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)
                           Path.DirectorySeparatorChar.ToString()   "PersonImg";
            if (!Directory.Exists(personImgPath))
            {
                Directory.CreateDirectory(personImgPath);
            }

            return personImgPath;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                videoSourcePlayer1.SignalToStop();
                videoSourcePlayer1.WaitForStop();
                t1.Abort();
                System.Environment.Exit(0);
            }
            catch { }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                string enCodeString = textBox1.Text.Trim();
                QRCodeEncoder codeEncoder = new QRCodeEncoder();
                pictureBox1.Image = codeEncoder.Encode(enCodeString, Encoding.UTF8);
            }
        }
        string filename = string.Empty; 
        private void button5_Click(object sender, EventArgs e)
        {
           
            OpenFileDialog p = new OpenFileDialog();
            p.Title = "请选择二维码图片";
            p.Filter = "图片格式(*.png)|*.jpg";
            p.Multiselect = false;
            if (p.ShowDialog() == DialogResult.OK)
            {
                filename = p.FileName;
                 ss();
            }
        }
        private void ss()
        {
            try
            {
                pictureBox1.Image = new Bitmap(filename);
                QRCodeDecoder qrDecoder = new QRCodeDecoder();
                string msg = qrDecoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)), Encoding.UTF8);
                textBox1.Text = msg;
            }
            catch
            {
                MessageBox.Show("解析不了,请重新打开图片");
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                SaveFileDialog s = new SaveFileDialog();
                s.Title = "保存二维码图片";
                s.Filter = "图片文件(*.jpg)|*.jpg";
                if (s.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    MessageBox.Show("保存成功");
                }
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            ss2();
        }
        private void ss2()
        {
           
            try
            {
                QRCodeDecoder qrDecoder = new QRCodeDecoder();
                string msg = qrDecoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)), Encoding.UTF8);
                textBox1.Text = msg;
                
            }
            catch
            {
               
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
           
            Workbook wb = new Workbook();           
            Worksheet sheet = wb.Worksheets[0];
            sheet.Cells["A1"].PutValue("");
            wb.Save("d:\\MyBook.xls", SaveFormat.Excel97To2003);
        }
       
       
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != "")
                {
                    myclass.DT100 = myclass.DT100   1;
                    myclass.Dt100 = "A";
                    string d0 = myclass.Dt100   myclass.DT100;
                    Workbook wb = new Workbook();
                    wb.Open("d:\\MyBook.xls");
                    Worksheet sheet = wb.Worksheets[0];
                    Cell cell = sheet.Cells[d0];
                    cell.PutValue(textBox1.Text);
                    sheet.Cells["B1"].PutValue(myclass .DT100);
                    wb.Save("d:\\MyBook.xls", SaveFormat.Excel97To2003);
                    textBox8.Text = textBox1.Text;
                }
                }
            catch 
            { 
            }
            

        }

        private void button9_Click(object sender, EventArgs e)
        {
            string stringType = textBox2.Text;
            int dt0 = Int32.Parse(stringType); 
            client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            client.Bind(new IPEndPoint(IPAddress.Parse(textBox7.Text), dt0));
            t1 = new Thread(new ThreadStart(ReciveMsg)); 
            t1.Priority = ThreadPriority.Highest; 
            t1.Start();
            //下面这句代码,解决:线程间操作无效: 从不是创建控件
            Control.CheckForIllegalCrossThreadCalls = false;
            
        }

        private void ReciveMsg()
        
        {
            for (bool i = false; i == false; )
            {
                Thread.Sleep(1); 

                EndPoint point = new IPEndPoint(IPAddress.Any, 0);
                byte[] buffer = new byte[1024];
                int length = client.ReceiveFrom(buffer, ref point);
                string message = Encoding.UTF8.GetString(buffer, 0, length);
                
                myclass.Dt101 = message;
               
                Thread.Sleep(10);
            }
        }
    

        private void button11_Click(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            try
            {
                string stringType = textBox3.Text;
                int dt0 = Int32.Parse(stringType); 
                EndPoint point = new IPEndPoint(IPAddress.Parse(textBox4.Text), dt0);
                string msg = textBox5.Text;
                client.SendTo(Encoding.UTF8.GetBytes(msg), point);
            }
            catch
            {
            }
        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

            int d0 = 1, d1 = 1;
            do
            {
                d0 = d1   1;
                d0  ;



            }
            while (d0 == 0);
            {

                if (textBox6.Text == "0011")
                {

                    myclass.DT100 = myclass.DT100   1;
                    myclass.Dt100 = "A";
                    string d2 = myclass.Dt100   myclass.DT100;
                    Workbook wb = new Workbook();
                    wb.Open("d:\\MyBook.xls");
                    Worksheet sheet = wb.Worksheets[0];
                    Cell cell = sheet.Cells[d2];
                    cell.PutValue(textBox1.Text);
                    sheet.Cells["B1"].PutValue(myclass.DT100);
                    wb.Save("d:\\MyBook.xls", SaveFormat.Excel97To2003);
                    SS4();

                }


            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox6.Text = myclass.Dt101;
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            Thread.Sleep(1000); 
            textBox1.Text = "";
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
 }
} 

实例下载地址

C# 打开摄像头 拍照,生成二维码,UDP通信 实例源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警