CameraVideoRecording.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using Microsoft.Win32;
  2. using PaintDotNet.Camera;
  3. using PaintDotNet.Camera;
  4. using PaintDotNet.SystemLayer;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Diagnostics;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace PaintDotNet.ImageCollect
  17. {
  18. internal partial class CameraVideoRecording : FloatingToolForm
  19. {
  20. /// <summary>
  21. /// 视频格式
  22. /// </summary>
  23. private string[] fmtArray = new string[] { "avi" };
  24. // 保存路径
  25. private string m_filePath = Application.StartupPath + "\\Video";
  26. // 持续时间
  27. private int m_duration = 0;
  28. private ICamera m_camera;
  29. private Timer timer1;
  30. bool _recording = false;
  31. bool _done = false;
  32. DateTime _timeLog;
  33. string _filename;
  34. public CameraVideoRecording()
  35. {
  36. InitializeComponent();
  37. InitializeLanguageText();
  38. InitializeControlData();
  39. this.Load += (s, e) =>
  40. {
  41. m_filePath = Settings.CurrentUser.GetString("CameraRecordPath", "");
  42. txtSavePath.Text = m_filePath;
  43. txtPrefix.Text = Settings.CurrentUser.GetString("CameraRecordPrefix", "");
  44. };
  45. this.FormClosing += (s, e) =>
  46. {
  47. Settings.CurrentUser.SetString("CameraRecordPrefix", txtPrefix.Text);
  48. Settings.CurrentUser.SetString("CameraRecordPath", txtSavePath.Text);
  49. };
  50. }
  51. /// <summary>
  52. /// 设置下拉等数据源
  53. /// </summary>
  54. private void InitializeControlData()
  55. {
  56. this.cmbVideoFormat.Items.AddRange(fmtArray);
  57. this.cmbVideoFormat.SelectedIndex = 0;
  58. this.txtSavePath.Text = m_filePath;
  59. m_camera = CameraManager.CurrentCamera;
  60. }
  61. private void btnSavePath_Click(object sender, EventArgs e)
  62. {
  63. FolderBrowserDialog folderBroeser = new FolderBrowserDialog();
  64. if (folderBroeser.ShowDialog() == DialogResult.OK)
  65. {
  66. m_filePath = folderBroeser.SelectedPath;
  67. txtSavePath.Text = m_filePath;
  68. }
  69. }
  70. /// <summary>
  71. /// 开始录制
  72. /// </summary>
  73. /// <param name="sender"></param>
  74. /// <param name="e"></param>
  75. private void btnStart_Click(object sender, EventArgs e)
  76. {
  77. try
  78. {
  79. if (!Directory.Exists(m_filePath))
  80. {
  81. Directory.CreateDirectory(m_filePath);
  82. }
  83. int bLapse = 1;
  84. if (rdoS.Checked)
  85. {
  86. bLapse = 1000;
  87. }
  88. m_duration = (int)(nudDuration.Value * bLapse);
  89. if (m_duration < 1000)
  90. {
  91. MessageBox.Show(PdnResources.GetString("Menu.Therecordingtimeannotbelessthansecond.Text"));
  92. return;
  93. }
  94. StartRecord();
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageBox.Show(ex.Message);
  99. }
  100. }
  101. void StartRecord()
  102. {
  103. string prefix = txtPrefix.Text.Trim();
  104. _filename = m_filePath + "\\" + prefix + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".avi";
  105. m_camera.RecStart(_filename);
  106. _recording = true;
  107. _timeLog = DateTime.Now;
  108. }
  109. /// <summary>
  110. /// 结束录制
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void btnStop_Click(object sender, EventArgs e)
  115. {
  116. stopRecording();
  117. _recording = false;
  118. }
  119. private void stopRecording()
  120. {
  121. _recording = false;
  122. _done = true;
  123. m_camera.RecStop();
  124. }
  125. private void CameraVideoRecording_FormClosing(object sender, FormClosingEventArgs e)
  126. {
  127. stopRecording();
  128. }
  129. private void btnPlay_Click(object sender, EventArgs e)
  130. {
  131. if (_filename != null)
  132. {
  133. Process.Start(_filename);
  134. }
  135. }
  136. #region 控件
  137. /// <summary>
  138. /// Required designer variable.
  139. /// </summary>
  140. private System.ComponentModel.IContainer components = null;
  141. /// <summary>
  142. /// Clean up any resources being used.
  143. /// </summary>
  144. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  145. protected override void Dispose(bool disposing)
  146. {
  147. if (disposing && (components != null))
  148. {
  149. components.Dispose();
  150. }
  151. base.Dispose(disposing);
  152. }
  153. // Windows Form Designer generated code
  154. private void InitializeLanguageText()
  155. {
  156. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  157. this.btnPlay.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Play.text");
  158. this.btnStop.Text = PdnResources.GetString("Menu.stop.text");
  159. this.btnStart.Text = PdnResources.GetString("Menu.Started.text");
  160. this.groupBox2.Text = PdnResources.GetString("Menu.Setting.Text");
  161. this.rdoM.Text = PdnResources.GetString("Menu.millisecond.text");
  162. this.rdoS.Text = PdnResources.GetString("Menu.second.text");
  163. this.btnSavePath.Text = PdnResources.GetString("Menu.alter.text");
  164. this.label4.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Shootingformat.text") + ":";
  165. this.label3.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.shootingtime.text") + ":";
  166. this.label2.Text = PdnResources.GetString("Menu.Nameprefix.text") + ":";
  167. this.label1.Text = PdnResources.GetString("Menu.saveroute.text") + ":";
  168. this.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Videorecording.text");
  169. }
  170. /// <summary>
  171. /// Required method for Designer support - do not modify
  172. /// the contents of this method with the code editor.
  173. /// </summary>
  174. private void InitializeComponent()
  175. {
  176. this.components = new System.ComponentModel.Container();
  177. this.groupBox1 = new System.Windows.Forms.GroupBox();
  178. this.btnPlay = new System.Windows.Forms.Button();
  179. this.btnStop = new System.Windows.Forms.Button();
  180. this.btnStart = new System.Windows.Forms.Button();
  181. this.groupBox2 = new System.Windows.Forms.GroupBox();
  182. this.cmbVideoFormat = new System.Windows.Forms.ComboBox();
  183. this.rdoM = new System.Windows.Forms.RadioButton();
  184. this.rdoS = new System.Windows.Forms.RadioButton();
  185. this.nudDuration = new System.Windows.Forms.NumericUpDown();
  186. this.btnSavePath = new System.Windows.Forms.Button();
  187. this.txtPrefix = new System.Windows.Forms.TextBox();
  188. this.txtSavePath = new System.Windows.Forms.TextBox();
  189. this.label4 = new System.Windows.Forms.Label();
  190. this.label3 = new System.Windows.Forms.Label();
  191. this.label2 = new System.Windows.Forms.Label();
  192. this.label1 = new System.Windows.Forms.Label();
  193. this.timer1 = new System.Windows.Forms.Timer(this.components);
  194. this.groupBox1.SuspendLayout();
  195. this.groupBox2.SuspendLayout();
  196. ((System.ComponentModel.ISupportInitialize)(this.nudDuration)).BeginInit();
  197. this.SuspendLayout();
  198. //
  199. // groupBox1
  200. //
  201. this.groupBox1.Controls.Add(this.btnPlay);
  202. this.groupBox1.Controls.Add(this.btnStop);
  203. this.groupBox1.Controls.Add(this.btnStart);
  204. this.groupBox1.Location = new System.Drawing.Point(12, 12);
  205. this.groupBox1.Name = "groupBox1";
  206. this.groupBox1.Size = new System.Drawing.Size(428, 75);
  207. this.groupBox1.TabIndex = 0;
  208. this.groupBox1.TabStop = false;
  209. this.groupBox1.Text = "操作";
  210. //
  211. // btnPlay
  212. //
  213. this.btnPlay.Enabled = false;
  214. this.btnPlay.Location = new System.Drawing.Point(340, 26);
  215. this.btnPlay.Name = "btnPlay";
  216. this.btnPlay.Size = new System.Drawing.Size(68, 30);
  217. this.btnPlay.TabIndex = 2;
  218. this.btnPlay.Text = "播放";
  219. this.btnPlay.UseVisualStyleBackColor = true;
  220. this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
  221. //
  222. // btnStop
  223. //
  224. this.btnStop.Enabled = false;
  225. this.btnStop.Location = new System.Drawing.Point(266, 26);
  226. this.btnStop.Name = "btnStop";
  227. this.btnStop.Size = new System.Drawing.Size(68, 30);
  228. this.btnStop.TabIndex = 1;
  229. this.btnStop.Text = "停止";
  230. this.btnStop.UseVisualStyleBackColor = true;
  231. this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
  232. //
  233. // btnStart
  234. //
  235. this.btnStart.Location = new System.Drawing.Point(192, 26);
  236. this.btnStart.Name = "btnStart";
  237. this.btnStart.Size = new System.Drawing.Size(68, 30);
  238. this.btnStart.TabIndex = 0;
  239. this.btnStart.Text = "开始";
  240. this.btnStart.UseVisualStyleBackColor = true;
  241. this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
  242. //
  243. // groupBox2
  244. //
  245. this.groupBox2.Controls.Add(this.cmbVideoFormat);
  246. this.groupBox2.Controls.Add(this.rdoM);
  247. this.groupBox2.Controls.Add(this.rdoS);
  248. this.groupBox2.Controls.Add(this.nudDuration);
  249. this.groupBox2.Controls.Add(this.btnSavePath);
  250. this.groupBox2.Controls.Add(this.txtPrefix);
  251. this.groupBox2.Controls.Add(this.txtSavePath);
  252. this.groupBox2.Controls.Add(this.label4);
  253. this.groupBox2.Controls.Add(this.label3);
  254. this.groupBox2.Controls.Add(this.label2);
  255. this.groupBox2.Controls.Add(this.label1);
  256. this.groupBox2.Location = new System.Drawing.Point(12, 100);
  257. this.groupBox2.Name = "groupBox2";
  258. this.groupBox2.Size = new System.Drawing.Size(428, 249);
  259. this.groupBox2.TabIndex = 1;
  260. this.groupBox2.TabStop = false;
  261. this.groupBox2.Text = "设置";
  262. //
  263. // cmbVideoFormat
  264. //
  265. this.cmbVideoFormat.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  266. this.cmbVideoFormat.FormattingEnabled = true;
  267. this.cmbVideoFormat.Location = new System.Drawing.Point(266, 173);
  268. this.cmbVideoFormat.Name = "cmbVideoFormat";
  269. this.cmbVideoFormat.Size = new System.Drawing.Size(95, 20);
  270. this.cmbVideoFormat.TabIndex = 10;
  271. //
  272. // rdoM
  273. //
  274. this.rdoM.AutoSize = true;
  275. this.rdoM.Location = new System.Drawing.Point(324, 127);
  276. this.rdoM.Name = "rdoM";
  277. this.rdoM.Size = new System.Drawing.Size(47, 16);
  278. this.rdoM.TabIndex = 9;
  279. this.rdoM.Text = "毫秒";
  280. this.rdoM.UseVisualStyleBackColor = true;
  281. //
  282. // rdoS
  283. //
  284. this.rdoS.AutoSize = true;
  285. this.rdoS.Checked = true;
  286. this.rdoS.Location = new System.Drawing.Point(260, 127);
  287. this.rdoS.Name = "rdoS";
  288. this.rdoS.Size = new System.Drawing.Size(35, 16);
  289. this.rdoS.TabIndex = 8;
  290. this.rdoS.TabStop = true;
  291. this.rdoS.Text = "秒";
  292. this.rdoS.UseVisualStyleBackColor = true;
  293. //
  294. // nudDuration
  295. //
  296. this.nudDuration.Location = new System.Drawing.Point(177, 125);
  297. this.nudDuration.Maximum = new decimal(new int[] {
  298. 276447231,
  299. 23283,
  300. 0,
  301. 0});
  302. this.nudDuration.Minimum = new decimal(new int[] {
  303. 1,
  304. 0,
  305. 0,
  306. 0});
  307. this.nudDuration.Name = "nudDuration";
  308. this.nudDuration.Size = new System.Drawing.Size(72, 21);
  309. this.nudDuration.TabIndex = 7;
  310. this.nudDuration.Value = new decimal(new int[] {
  311. 1,
  312. 0,
  313. 0,
  314. 0});
  315. //
  316. // btnSavePath
  317. //
  318. this.btnSavePath.Location = new System.Drawing.Point(367, 32);
  319. this.btnSavePath.Name = "btnSavePath";
  320. this.btnSavePath.Size = new System.Drawing.Size(55, 25);
  321. this.btnSavePath.TabIndex = 6;
  322. this.btnSavePath.Text = "更改";
  323. this.btnSavePath.UseVisualStyleBackColor = true;
  324. this.btnSavePath.Click += new System.EventHandler(this.btnSavePath_Click);
  325. //
  326. // txtPrefix
  327. //
  328. this.txtPrefix.Location = new System.Drawing.Point(103, 75);
  329. this.txtPrefix.Name = "txtPrefix";
  330. this.txtPrefix.Size = new System.Drawing.Size(258, 21);
  331. this.txtPrefix.TabIndex = 5;
  332. //
  333. // txtSavePath
  334. //
  335. this.txtSavePath.Location = new System.Drawing.Point(103, 32);
  336. this.txtSavePath.Name = "txtSavePath";
  337. this.txtSavePath.ReadOnly = true;
  338. this.txtSavePath.Size = new System.Drawing.Size(258, 21);
  339. this.txtSavePath.TabIndex = 4;
  340. this.txtSavePath.Text = "r";
  341. //
  342. // label4
  343. //
  344. this.label4.AutoSize = true;
  345. this.label4.Location = new System.Drawing.Point(15, 176);
  346. this.label4.Name = "label4";
  347. this.label4.Size = new System.Drawing.Size(65, 12);
  348. this.label4.TabIndex = 3;
  349. this.label4.Text = "拍摄格式:";
  350. //
  351. // label3
  352. //
  353. this.label3.AutoSize = true;
  354. this.label3.Location = new System.Drawing.Point(15, 127);
  355. this.label3.Name = "label3";
  356. this.label3.Size = new System.Drawing.Size(65, 12);
  357. this.label3.TabIndex = 2;
  358. this.label3.Text = "拍摄时间:";
  359. //
  360. // label2
  361. //
  362. this.label2.AutoSize = true;
  363. this.label2.Location = new System.Drawing.Point(15, 80);
  364. this.label2.Name = "label2";
  365. this.label2.Size = new System.Drawing.Size(65, 12);
  366. this.label2.TabIndex = 1;
  367. this.label2.Text = "名称前缀:";
  368. //
  369. // label1
  370. //
  371. this.label1.AutoSize = true;
  372. this.label1.Location = new System.Drawing.Point(15, 35);
  373. this.label1.Name = "label1";
  374. this.label1.Size = new System.Drawing.Size(65, 12);
  375. this.label1.TabIndex = 0;
  376. this.label1.Text = "保存路径:";
  377. //
  378. // timer1
  379. //
  380. this.timer1.Enabled = true;
  381. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  382. //
  383. // CameraVideoRecording
  384. //
  385. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  386. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  387. this.ClientSize = new System.Drawing.Size(459, 373);
  388. this.Controls.Add(this.groupBox2);
  389. this.Controls.Add(this.groupBox1);
  390. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  391. this.Location = new System.Drawing.Point(0, 0);
  392. this.Name = "CameraVideoRecording";
  393. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  394. this.Text = "视频录制";
  395. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraVideoRecording_FormClosing);
  396. this.Controls.SetChildIndex(this.groupBox1, 0);
  397. this.Controls.SetChildIndex(this.groupBox2, 0);
  398. this.groupBox1.ResumeLayout(false);
  399. this.groupBox2.ResumeLayout(false);
  400. this.groupBox2.PerformLayout();
  401. ((System.ComponentModel.ISupportInitialize)(this.nudDuration)).EndInit();
  402. this.ResumeLayout(false);
  403. }
  404. private System.Windows.Forms.GroupBox groupBox1;
  405. private System.Windows.Forms.Button btnPlay;
  406. private System.Windows.Forms.Button btnStop;
  407. private System.Windows.Forms.Button btnStart;
  408. private System.Windows.Forms.GroupBox groupBox2;
  409. private System.Windows.Forms.ComboBox cmbVideoFormat;
  410. private System.Windows.Forms.RadioButton rdoM;
  411. private System.Windows.Forms.RadioButton rdoS;
  412. private System.Windows.Forms.NumericUpDown nudDuration;
  413. private System.Windows.Forms.Button btnSavePath;
  414. private System.Windows.Forms.TextBox txtPrefix;
  415. private System.Windows.Forms.TextBox txtSavePath;
  416. private System.Windows.Forms.Label label4;
  417. private System.Windows.Forms.Label label3;
  418. private System.Windows.Forms.Label label2;
  419. private System.Windows.Forms.Label label1;
  420. #endregion
  421. private void timer1_Tick(object sender, EventArgs e)
  422. {
  423. btnStart.Enabled = !_recording;
  424. btnStop.Enabled = _recording;
  425. btnPlay.Enabled = _done && !_recording;
  426. if (_recording && (DateTime.Now - _timeLog).TotalMilliseconds > m_duration)
  427. {
  428. stopRecording();
  429. }
  430. }
  431. }
  432. }