在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → UVC演示程序

UVC演示程序

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:7.40M
  • 下载次数:25
  • 浏览次数:356
  • 发布时间:2021-08-23
  • 实例类别:C#语言基础
  • 发 布 人:宇多田鼠
  • 文件格式:.zip
  • 所需积分:2
 相关标签: UVC 演示 VC 程序

实例介绍

【实例简介】

UVC演示程序

【实例截图】

from clipboard

【核心代码】

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



标签: UVC 演示 VC 程序

实例下载地址

网友评论

发表评论

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

查看所有0条评论>>

小贴士

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

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

关于好例子网

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

;
报警