Camera.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. using System.Drawing.Imaging;
  10. namespace CameraDemo
  11. {
  12. /// <summary>
  13. /// 申明委托
  14. /// </summary>
  15. /// <param name="e"></param>
  16. /// <returns></returns>
  17. public delegate void EventPhotographedHandler(EventPhotographed e);
  18. public partial class Camera : UserControl
  19. {
  20. private int hHwnd;
  21. public string fileName = "*";//照片的文件名,一般为考生的流水号
  22. public int width = 310*4;//照片的尺寸为宽度:240,高度:320,即:240*320
  23. public int height = 275*4;
  24. private bool isOpen = false;
  25. /// <summary>
  26. /// 必需的设计器变量。
  27. /// </summary>
  28. [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  29. public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
  30. [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  31. public static extern bool DestroyWindow(int hndw);
  32. [DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  33. public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
  34. [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  35. public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
  36. public Camera()
  37. {
  38. InitializeComponent();
  39. if (fileName == "*")
  40. {
  41. fileName = DateTime.Now.ToString("yyMMddHHmmssfff");
  42. }
  43. Clipboard.Clear();
  44. }
  45. /// <summary>
  46. /// 拍照事件
  47. /// </summary>
  48. public event EventPhotographedHandler EventPhotographeded;
  49. int OpenCount = 0;
  50. private void OpenCapture()
  51. {
  52. string msg="未找到可用摄像头,请检查设备是否正常后重试!";
  53. try
  54. {
  55. int intWidth = this.videoWindow.Width;
  56. int intHeight = this.videoWindow.Height;
  57. int intDevice = 0;
  58. string refDevice = intDevice.ToString();
  59. hHwnd = Camera.capCreateCaptureWindowA(ref refDevice, 1342177280, 0, 0, 640, 480, this.videoWindow.Handle.ToInt32(), 0);
  60. int cameraCount = Camera.SendMessage(this.hHwnd, 0x40a, intDevice, 0);
  61. if (cameraCount > 0)
  62. {
  63. try
  64. {
  65. Camera.SendMessage(this.hHwnd, 0x435, -1, 0);
  66. Camera.SendMessage(this.hHwnd, 0x434, 0x42, 0);
  67. Camera.SendMessage(this.hHwnd, 0x432, -1, 0);
  68. Camera.SetWindowPos(this.hHwnd, 1, 0, 0, intWidth, intHeight, 6);
  69. isOpen = true;
  70. }
  71. catch { LYFZ.MessageBoxCustom.Show(msg); return; }
  72. }
  73. else
  74. {
  75. Camera.DestroyWindow(this.hHwnd);
  76. }
  77. if (!isOpen)
  78. {
  79. System.Threading.Thread.Sleep(10);
  80. OpenCount++;
  81. if (OpenCount < 30)
  82. {
  83. OpenCapture();
  84. }
  85. else {
  86. LYFZ.MessageBoxCustom.Show(msg); return;
  87. }
  88. }
  89. }
  90. catch {
  91. LYFZ.MessageBoxCustom.Show(msg); return;
  92. }
  93. }
  94. /// <summary>
  95. /// 打开摄像头
  96. /// </summary>
  97. /// <returns></returns>
  98. public bool OpenCamera()
  99. {
  100. if (!isOpen)
  101. this.OpenCapture();
  102. return isOpen;
  103. }
  104. private void btn_OpenCapture_Click(object sender, EventArgs e)//打开摄像头,显示视频
  105. {
  106. if (!isOpen)
  107. this.OpenCapture();
  108. }
  109. bool isSaveImageFile = true;
  110. /// <summary>
  111. /// 是否直接保存Image文件
  112. /// </summary>
  113. public bool IsSaveImageFile
  114. {
  115. get { return isSaveImageFile; }
  116. set { isSaveImageFile = value; }
  117. }
  118. private void btn_GetCapture_Click(object sender, EventArgs e)//抓取图像
  119. {
  120. try
  121. {
  122. Camera.SendMessage(this.hHwnd, 0x41e, 0, 0);
  123. IDataObject obj_camera = Clipboard.GetDataObject();
  124. if (obj_camera.GetDataPresent(typeof(Bitmap)))
  125. {
  126. Image image_camera = ((Image)obj_camera.GetData(typeof(Bitmap))).GetThumbnailImage(width, height, null, IntPtr.Zero);
  127. //设置图片的尺寸为240*320
  128. if (IsSaveImageFile)
  129. {
  130. SaveFileDialog SaveFileDialog_camera = new SaveFileDialog();
  131. SaveFileDialog_camera.FileName = fileName + ".jpg";
  132. SaveFileDialog_camera.Filter = "Image Files(*.JPG)|*.JPG";
  133. if (SaveFileDialog_camera.ShowDialog() == DialogResult.OK)
  134. {
  135. image_camera.Save(SaveFileDialog_camera.FileName, ImageFormat.Jpeg);
  136. }
  137. }
  138. else {
  139. EventPhotographed eventPhtoto = new EventPhotographed(image_camera);
  140. eventPhtoto.IsPhotographedSuccess = true;
  141. this.EventPhotographeded(eventPhtoto);
  142. }
  143. }
  144. else
  145. {
  146. MessageBox.Show("摄像头没有开启,不能抓图,请开启摄像头!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  147. }
  148. }
  149. catch
  150. {
  151. MessageBox.Show("抓取图像时出现错误!", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
  152. }
  153. }
  154. private void btn_CloseCapture_Click(object sender, EventArgs e)//关闭摄像头
  155. {
  156. Camera.SendMessage(this.hHwnd, 0x40b, 0, 0);
  157. Camera.DestroyWindow(this.hHwnd);
  158. Clipboard.Clear();//清除剪切板中的内容
  159. isOpen = false;
  160. }
  161. private void btn_set_Click(object sender, EventArgs e)//对摄像头进行设置
  162. {
  163. if (isOpen)
  164. Camera.SendMessage(this.hHwnd, 0x42a, 0, 0);
  165. else
  166. MessageBox.Show("摄像头没有开启,不能设置,请开启摄像头!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  167. }
  168. private void btn_kinescope_Click(object sender, EventArgs e)//开始录像,录像过程中鼠标成漏斗形,对着图像显示区域单击鼠标左键或右键就结束了录像。
  169. {
  170. if (isOpen)
  171. {
  172. SaveFileDialog SaveFileDialog_Video = new SaveFileDialog();
  173. SaveFileDialog_Video.FileName = fileName + ".avi";
  174. SaveFileDialog_Video.Filter = "Video Files(*.avi)|*.avi";
  175. if (SaveFileDialog_Video.ShowDialog() == DialogResult.OK)
  176. {
  177. SendMessage(this.hHwnd, 0x414, 0, SaveFileDialog_Video.FileName);
  178. SendMessage(this.hHwnd, 0x43e, 0, 0);
  179. }
  180. }
  181. else
  182. MessageBox.Show("摄像头没有开启,不能录像,请开启摄像头!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  183. }
  184. private void btn_stopKinescope_Click(object sender, EventArgs e)//停止录像
  185. {
  186. SendMessage(this.hHwnd, 0x444, 0, 0);
  187. }
  188. }
  189. /// <summary>
  190. /// 拍照事件对象
  191. /// </summary>
  192. public class EventPhotographed : EventArgs
  193. {
  194. private Image _image;
  195. /// <summary>
  196. /// 输出图像
  197. /// </summary>
  198. public Image OutputImage
  199. {
  200. get { return _image; }
  201. set { _image = value; }
  202. }
  203. public EventPhotographed(Image image)
  204. {
  205. this._image = image;
  206. }
  207. bool isPhotographedSuccess = false;
  208. /// <summary>
  209. /// 是否拍照成功
  210. /// </summary>
  211. public bool IsPhotographedSuccess
  212. {
  213. get { return isPhotographedSuccess; }
  214. set { isPhotographedSuccess = value; }
  215. }
  216. }
  217. }