基本信息
源码名称:C# 打开摄像头 拍照,生成二维码,UDP通信 实例源码下载
源码大小:5.04M
文件格式:.rar
开发语言:C#
更新时间:2017-01-15
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

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

        }
 }
}