基本信息
源码名称:UVC演示程序
源码大小:7.40M
文件格式:.zip
开发语言:C#
更新时间:2021-08-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

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

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

UVC演示程序

using System;
using System.Collections.Generic;
using System.Drawing;
using SharpCamera;
using System.Windows.Forms;

namespace SharpCameraDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region 变量

        private Camera curCamera = null;
        private CameraMgr cameraMgr = null;
        #endregion

        #region 事件
        private void Form2_Load(object sender, EventArgs e)
        {
            //在调用SharpCamera类库其他任何类和方法之前,必须先调用本方法设置授权Key,如果是试用,则传入Test即可。
            //试用版有时间限制,只能运行十分钟,之后会被加水印,正式授权请访问: http://sharpcamera.zzsgzn.com/?SharpCameraDemo
            KeyMgr.SetKey("Test");

            //实例化一个CameraMgr
            cameraMgr = new CameraMgr();

            //得到当前计算机的所有已安装摄像头
            List<string> lstCameraNameInstalled = cameraMgr.GetCameraNameList();

            if (lstCameraNameInstalled != null)
            {
                foreach (string item in lstCameraNameInstalled)
                {
                    cmbBoxCamera.Items.Add(item);
                }
            }

            if (cmbBoxCamera.Items.Count > 0)
            {
                cmbBoxCamera.SelectedIndex = 0;
            }
        }

        public void VideoFrameCaptrue(Bitmap img)
        {
            Bitmap temp = ((Bitmap)img).Clone(new Rectangle(0, 0, img.Width, img.Height), img.PixelFormat);
            if (this.InvokeRequired)
            {
                this.pictureBox1.BeginInvoke(new MethodInvoker(delegate ()
                {
                    if (pictureBox1.Image != null)
                    {
                        pictureBox1.Image.Dispose();
                        pictureBox1.Image = null;
                    }
                    this.pictureBox1.Image = temp;
                }));
            }
            else
            {
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                    pictureBox1.Image = null;
                }
                this.pictureBox1.Image = temp;
            }
        }

        private void cmbBoxCamera_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbBoxCamera.SelectedIndex != -1)
            {
                if (curCamera != null)
                {
                    //取消对当前摄像头帧图片的订阅
                    curCamera.OnVideoFrameCaptrue -= VideoFrameCaptrue;

                    //关掉该摄像头
                    curCamera.Close();

                    //释放资源
                    curCamera.Dispose();

                    curCamera = null;
                }

                //按照当前选定的名字,打开该摄像头
                string name = cmbBoxCamera.Items[cmbBoxCamera.SelectedIndex].ToString().Trim();

                //选定该摄像头
                curCamera = cameraMgr.ChooseCamera(name);

                //打开该摄像头
                curCamera.Open();

                //清空分辨率列表,用当前打开的摄像头的分辨率列表来填充
                cmbBoxResolution.Items.Clear();
                if (curCamera != null)
                {
                    //订阅帧图片上报的事件
                    curCamera.OnVideoFrameCaptrue = VideoFrameCaptrue;

                    foreach (CameraResolution item in curCamera.AllSupportedResolution)
                    {
                        cmbBoxResolution.Items.Add(item.ToString());
                    }

                    if (cmbBoxResolution.Items.Count > 0)
                    {
                        //默认使用第一个分辨率
                        cmbBoxResolution.SelectedIndex = 0;

                        //默认不旋转
                        cmbBoxRotate.SelectedIndex = 0;
                    }


                }
            }
        }

        private void cmbBoxResolution_SelectedIndexChanged(object sender, EventArgs e)
        {
            //切换分辨率
            if (curCamera != null && cmbBoxResolution.SelectedIndex != -1)
            {
                string[] arr = cmbBoxResolution.Text.Split(new char[] { ' ' });
                if (arr.Length == 3)
                {
                    try
                    {
                        //修改当前分辨率
                        curCamera.Resolution = new CameraResolution(int.Parse(arr[0]), int.Parse(arr[2]));

                        //消除翻转,恢复默认的0度
                        curCamera.RotateVideo(VideoRotateTypeEnum.Angle0);
                    }
                    catch { }
                }
            }

        }

        private void cmbBoxRotate_SelectedIndexChanged(object sender, EventArgs e)
        {
            //设置旋转角度
            if (cmbBoxRotate.SelectedIndex != -1)
            {
                if (curCamera != null)
                {
                    if (cmbBoxRotate.SelectedIndex == 0)
                    {
                        curCamera.RotateVideo(VideoRotateTypeEnum.Angle0);
                    }
                    else if (cmbBoxRotate.SelectedIndex == 1)
                    {
                        curCamera.RotateVideo(VideoRotateTypeEnum.Angle90);
                    }
                    else if (cmbBoxRotate.SelectedIndex == 2)
                    {
                        curCamera.RotateVideo(VideoRotateTypeEnum.Angle180);
                    }
                    else if (cmbBoxRotate.SelectedIndex == 3)
                    {
                        curCamera.RotateVideo(VideoRotateTypeEnum.Angle270);
                    }
                }
            }
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            //退出时释放资源
            if (curCamera != null)
            {
                curCamera.Close();
                curCamera.Dispose();
                curCamera = null;
            }

            if (cameraMgr != null)
            {
                cameraMgr.Dispose();
                cameraMgr = null;
            }
        }

        #endregion

        #region 属性设置面板
        private void btnShowSettingDialog_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //显示参数的设置对话框,一般项目里很少使用
                curCamera.ShowPropertyPage();
            }
        }
        #endregion

        #region 版权
        private void button5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://sharpcamera.zzsgzn.com/?SharpCameraDemo");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.zzsgzn.com/?SharpCameraDemo");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://wpa.qq.com/msgrd?v=3&uin=3535600244&site=qq&menu=yes");
        }

        #endregion

        #region 亮度
        private void btnBrightness1_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取亮度的属性对象
                CameraProperty brightnessPro = curCamera.Brightness;

                //调整其值到最大值,也可以指定为其他合法值
                brightnessPro.Current = brightnessPro.Max;
            }
        }

        private void btnBrightness2_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取亮度的属性对象
                CameraProperty brightnessPro = curCamera.Brightness;

                //调整其值到最小值,也可以指定为其他合法值
                brightnessPro.Current = brightnessPro.Min;
            }
        }

        private void btnBrightness3_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取亮度的属性对象
                CameraProperty brightnessPro = curCamera.Brightness;

                //调整其值到默认值,也可以指定为其他合法值
                brightnessPro.Current = brightnessPro.Default;
            }
        }

        #endregion

        #region 对比度
        private void button6_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取对比度的属性对象
                CameraProperty pro = curCamera.Contrast;

                //调整其值到最大值,也可以指定为其他合法值
                pro.Current = pro.Max;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取对比度的属性对象
                CameraProperty pro = curCamera.Contrast;

                //调整其值到最小值,也可以指定为其他合法值
                pro.Current = pro.Min;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取对比度的属性对象
                CameraProperty pro = curCamera.Contrast;

                //调整其值到默认值,也可以指定为其他合法值
                pro.Current = pro.Default;
            }
        }
        #endregion

        #region 清晰度

        private void button9_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取清晰度的属性对象
                CameraProperty pro = curCamera.Sharpness;

                //调整其值到最大值,也可以指定为其他合法值
                pro.Current = pro.Max;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取清晰度的属性对象
                CameraProperty pro = curCamera.Sharpness;

                //调整其值到最小值,也可以指定为其他合法值
                pro.Current = pro.Min;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取清晰度的属性对象
                CameraProperty pro = curCamera.Sharpness;

                //调整其值到默认值,也可以指定为其他合法值
                pro.Current = pro.Default;
            }
        }
        #endregion

        #region 逆光对比
        private void button14_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取逆光对比的属性对象
                CameraProperty pro = curCamera.BacklightCompensation;

                //调整其值到最大值,也可以指定为其他合法值
                pro.Current = pro.Max;
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取逆光对比的属性对象
                CameraProperty pro = curCamera.BacklightCompensation;

                //调整其值,也可以指定为其他合法值
                pro.Current = pro.Min;
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取逆光对比的属性对象
                CameraProperty pro = curCamera.BacklightCompensation;

                //调整其值,也可以指定为其他合法值
                pro.Current = pro.Default;
            }
        }
        #endregion

        #region 曝光
        private void button17_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取曝光的属性对象
                CameraProperty pro = curCamera.Exposure;

                //调整其值,也可以指定为其他合法值
                pro.Current = pro.Max;
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取曝光的属性对象
                CameraProperty pro = curCamera.Exposure;

                //调整其值,也可以指定为其他合法值
                pro.Current = pro.Min;
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取曝光的属性对象
                CameraProperty pro = curCamera.Exposure;

                //调整其值,也可以指定为其他合法值
                pro.Current = pro.Default;
            }
        }
        #endregion

        #region 焦点
        private void button20_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取曝光的属性对象
                CameraProperty pro = curCamera.Focus;

                //调整其值,也可以指定为其他合法值
                pro.Current = pro.Max;
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取曝光的属性对象
                CameraProperty pro = curCamera.Focus;

                //调整其值,也可以指定为其他合法值
                pro.Current = pro.Min;
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (curCamera != null)
            {
                //获取曝光的属性对象
                CameraProperty pro = curCamera.Focus;

                //调整其值,也可以指定为其他合法值
                pro.Current = pro.Default;
            }
        }
        #endregion
    }
}