C#程序摄像头使用实例:照相程序
今天给大家分享一个小程序:调用本机摄像头实现照相的程序:
废话不多说了、直接进入主题吧、
首先、创建一个WinForm程序、添加一个CameraAPI.cs类、用来调用微软提供的摄像头API函数用的、代码如下所示:
CameraAPI.cs(摄像头API调用类)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CameraVideoWithAPI 8 { 9 using System.Runtime.InteropServices; 10 11 /// <summary> 12 /// 摄像头API辅助类 13 /// </summary> 14 public class CameraAPI 15 { 16 // 视频API调用 17 [DllImport("avicap32.dll")] 18 public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); 19 [DllImport("avicap32.dll")] 20 public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer); 21 [DllImport("User32.dll")] 22 public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam); 23 [DllImport("User32.dll")] 24 public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam); 25 26 // 常量 27 public const int WM_USER = 0x400; 28 public const int WS_CHILD = 0x40000000; 29 public const int WS_VISIBLE = 0x10000000; 30 public const int SWP_NOMOVE = 0x2; 31 public const int SWP_NOZORDER = 0x4; 32 public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10; 33 public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11; 34 public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5; 35 public const int WM_CAP_SET_PREVIEW = WM_USER + 50; 36 public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52; 37 public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45; 38 public const int WM_CAP_START = WM_USER; 39 public const int WM_CAP_SAVEDIB = WM_CAP_START + 25; 40 41 } 42 }
然后、新建一个Camera.cs类、用来调用CameraAPI.cs辅助类中定义的函数、实现打开视频设备、关闭视频设备以及拍照的功能,详细代码如下所示:
Camera.cs(功能实现类)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Runtime.InteropServices; 7 8 namespace CameraVideoWithAPI 9 { 10 #region 摄像头类辅助类 11 /// <summary> 12 /// 摄像头类辅助类 13 /// </summary> 14 public class Camera 15 { 16 private IntPtr lwndC; //保存无符号句柄 17 private IntPtr mControlPtr; //保存管理指示器 18 private int mWidth; 19 private int mHeight; 20 21 #region 默认构造 22 /// <summary> 23 /// 默认构造 24 /// </summary> 25 /// <param name="handle"></param> 26 /// <param name="width"></param> 27 /// <param name="height"></param> 28 public Camera(IntPtr handle, int width, int height) 29 { 30 mControlPtr = handle; //显示视频控件的句柄 31 mWidth = width; //视频宽度 32 mHeight = height; //视频高度 33 } 34 #endregion 35 36 #region 打开视频设备 37 /// <summary> 38 /// 打开视频设备 39 /// </summary> 40 public void StartWebCam() 41 { 42 byte[] lpszName = new byte[100]; 43 byte[] lpszVer = new byte[100]; 44 CameraAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100); 45 this.lwndC = CameraAPI.capCreateCaptureWindowA(lpszName, CameraAPI.WS_CHILD | CameraAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0); 46 if (CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_DRIVER_CONNECT, 0, 0)) 47 { 48 CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SET_PREVIEWRATE, 100, 0); 49 CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SET_PREVIEW, true, 0); 50 } 51 } 52 #endregion 53 54 #region 关闭视频设备 55 /// <summary> 56 /// 关闭视频设备 57 /// </summary> 58 public void CloseWebcam() 59 { 60 CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0); 61 } 62 #endregion 63 64 #region 拍照 65 /// <summary> 66 /// 拍照 67 /// </summary> 68 /// <param name="path">要保存bmp文件的路径</param> 69 public void GrabImage(IntPtr hWndC, string path) 70 { 71 IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); 72 CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32()); 73 } 74 #endregion 75 } 76 #endregion 77 }
然后、打开窗体、放入一个PictureBox控件、命名为“pic_camera”,用来在打开视频设备后显示实时图像的区域;
在拖入三个Button按钮,分别命名为“btn_openCamera”(打开视频设备,默认启用)、“btn_closeCamera”(关闭视频设备,默认禁用)、“btn_cameraPic”(拍照,默认禁用);
frmMain.cs(窗体的后置代码)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace CameraVideoWithAPI 12 { 13 public partial class frmMain : Form 14 { 15 public frmMain() 16 { 17 InitializeComponent(); 18 } 19 20 Camera camera; //摄像头辅助类对象 21 22 #region 打开视频设备按钮的事件 23 /// <summary> 24 /// 打开视频设备按钮的事件 25 /// </summary> 26 /// <param name="sender"></param> 27 /// <param name="e"></param> 28 private void btn_openCamera_Click(object sender, EventArgs e) 29 { 30 this.btn_openCamera.Enabled = false; //打开视频设备后,禁用“打开视频设备”按钮 31 this.btn_closeCamera.Enabled = true; //打开视频设备后,启用“关闭视频设备”按钮 32 this.btn_cameraPic.Enabled = true; //打开视频设备后,启用“拍照”按钮 33 34 camera = new Camera(this.pic_camera.Handle, this.pic_camera.Width, this.pic_camera.Height); //实例化Camera对象,传入要显示区域(PictureBox)的句柄及宽高 35 camera.StartWebCam(); //打开视频设备 36 } 37 #endregion 38 39 #region 关闭视频设备的事件 40 /// <summary> 41 /// 关闭视频设备的事件 42 /// </summary> 43 /// <param name="sender"></param> 44 /// <param name="e"></param> 45 private void btn_closeCamera_Click(object sender, EventArgs e) 46 { 47 this.btn_openCamera.Enabled = true; //点击关闭视频设备后,启用“打开视频设备”按钮 48 this.btn_closeCamera.Enabled = false; //点击关闭视频设备后,禁用“关闭视频设备”按钮 49 this.btn_cameraPic.Enabled = false; //点击关闭视频设备后,禁用“拍照”按钮 50 51 camera.CloseWebcam(); //关闭视频设备 52 } 53 #endregion 54 55 #region 拍照按钮的事件 56 /// <summary> 57 /// 拍照按钮的事件 58 /// </summary> 59 /// <param name="sender"></param> 60 /// <param name="e"></param> 61 private void btn_cameraPic_Click(object sender, EventArgs e) 62 { 63 camera.GrabImage(this.pic_camera.Handle, "guying.bmp"); //拍照功能、前一个参数是显示图像的PictureBox句柄,后一个参数是要保存图片的路径。 64 } 65 #endregion 66 } 67 }
至此、这个简单的拍照程序就搞定了、、
以上代码主要讲的是基础、因此功能不完善、你也可以扩展一下功能、完善一下、例如:加一个保存图片位置的功能等等、、、
【程序BUG:】
点击“打开视频设备”按钮后,有时不会及时打开、需要多试几次、一般3-5次。
好了、不费话了、欢迎提交您对本程序的一些建议及思路。
【原来来自:HackerGuying的博客:http://www.cnblogs.com/HackerGuying】