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 camera2 = null;
private Camera camera1 = null;
private CameraMgr cameraMgr = null;
#endregion
#region 事件
private void Form2_Load(object sender, EventArgs e)
{
//在調用SharpCamera類庫其他任何類和方法之前,必須先調用本方法設置授權Key,如果是試用,則傳入Test即可。
//試用版有時間限制,只能運行十分鐘,之後會被加水印,正式授權請訪問: http://sharpcamera.zzsgzn.com/?MultiCameraDemo
KeyMgr.SetKey("Test");
//實例化一個CameraMgr
cameraMgr = new CameraMgr();
//得到當前計算機的所有已安裝攝像頭
List<string> lstCameraNameInstalled = cameraMgr.GetCameraNameList();
if(lstCameraNameInstalled.Count < 2)
{
throw new Exception("您的計算機上已安裝的UVC攝像頭少於2個,本程序無法正常運行!");
}
#region 初始化攝像頭1
//選定該攝像頭,攝像頭索引爲0,您也可以用攝像頭名稱的方式來選定
camera1 = cameraMgr.ChooseCamera(0);
//訂閱幀圖片上報的事件
camera1.OnVideoFrameCaptrue += VideoFrameCaptrue1;
//打開該攝像頭
camera1.Open();
//修改當前分辨率
camera1.Resolution = camera1.AllSupportedResolution[0];
#endregion
#region 初始化攝像頭2
//選定該攝像頭,這裏攝像頭索引爲1
camera2 = cameraMgr.ChooseCamera(1);
//訂閱幀圖片上報的事件
camera2.OnVideoFrameCaptrue += VideoFrameCaptrue2;
//打開該攝像頭
camera2.Open();
//修改當前分辨率
camera2.Resolution = camera2.AllSupportedResolution[0];
#endregion
}
public void VideoFrameCaptrue1(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;
}
}
public void VideoFrameCaptrue2(Bitmap img)
{
Bitmap temp = ((Bitmap)img).Clone(new Rectangle(0, 0, img.Width, img.Height), img.PixelFormat);
if (this.InvokeRequired)
{
this.pictureBox2.BeginInvoke(new MethodInvoker(delegate ()
{
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
pictureBox2.Image = null;
}
this.pictureBox2.Image = temp;
}));
}
else
{
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
pictureBox2.Image = null;
}
this.pictureBox2.Image = temp;
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//退出時釋放資源
if (camera1 != null)
{
camera1.Close();
camera1.Dispose();
camera1 = null;
}
if (camera2 != null)
{
camera2.Close();
camera2.Dispose();
camera2 = null;
}
if (cameraMgr != null)
{
cameraMgr.Dispose();
cameraMgr = null;
}
}
#endregion
#region 屬性設置面板
private void Button1_Click(object sender, EventArgs e)
{
if (camera1 != null)
{
//顯示設置對話框
camera1.ShowPropertyPage();
}
}
private void Button2_Click(object sender, EventArgs e)
{
if (camera2 != null)
{
//顯示設置對話框
camera2.ShowPropertyPage();
}
}
#endregion
#region 版權
private void button5_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://sharpcamera.zzsgzn.com/?MultiCameraDemo");
}
private void button4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.zzsgzn.com/?MultiCameraDemo");
}
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 (camera1 != null)
{
//獲取亮度的屬性對象
CameraProperty brightnessPro = camera1.Brightness;
//調整其值到最大值,也可以指定爲其他合法值
brightnessPro.Current = brightnessPro.Max;
}
}
private void btnBrightness2_Click(object sender, EventArgs e)
{
if (camera1 != null)
{
//獲取亮度的屬性對象
CameraProperty brightnessPro = camera1.Brightness;
//調整其值到最小值,也可以指定爲其他合法值
brightnessPro.Current = brightnessPro.Min;
}
}
private void btnBrightness3_Click(object sender, EventArgs e)
{
if (camera1 != null)
{
//獲取亮度的屬性對象
CameraProperty brightnessPro = camera1.Brightness;
//調整其值到默認值,也可以指定爲其他合法值
brightnessPro.Current = brightnessPro.Default;
}
}
//下邊爲第二個攝像頭的亮度控制
private void Button11_Click(object sender, EventArgs e)
{
if (camera2 != null)
{
//獲取亮度的屬性對象
CameraProperty brightnessPro = camera2.Brightness;
//調整其值到最大值,也可以指定爲其他合法值
brightnessPro.Current = brightnessPro.Max;
}
}
private void Button12_Click(object sender, EventArgs e)
{
if (camera2 != null)
{
//獲取亮度的屬性對象
CameraProperty brightnessPro = camera2.Brightness;
//調整其值到最小值,也可以指定爲其他合法值
brightnessPro.Current = brightnessPro.Min;
}
}
private void Button21_Click(object sender, EventArgs e)
{
if (camera2 != null)
{
//獲取亮度的屬性對象
CameraProperty brightnessPro = camera2.Brightness;
//調整其值到默認值,也可以指定爲其他合法值
brightnessPro.Current = brightnessPro.Default;
}
}
#endregion
}
}