CameraVideoRecording.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using Microsoft.Win32;
  2. using PaintDotNet.SystemLayer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using TUCamera;
  15. using static TUCamera.TUCamera;
  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 TUCamera.TUCamera 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 = TUCameraManager.GetInstance().GetCurrentCamera();
  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. m_camera.StartWaitForFrame();
  105. _filename = m_filePath + "\\" + prefix + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".avi";
  106. m_camera.RecStart(_filename);
  107. _recording = true;
  108. _timeLog = DateTime.Now;
  109. }
  110. /// <summary>
  111. /// 结束录制
  112. /// </summary>
  113. /// <param name="sender"></param>
  114. /// <param name="e"></param>
  115. private void btnStop_Click(object sender, EventArgs e)
  116. {
  117. stopRecording();
  118. _recording = false;
  119. }
  120. private void stopRecording()
  121. {
  122. _recording = false;
  123. _done = true;
  124. m_camera.RecStop();
  125. m_camera.StopWaitForFrame();
  126. }
  127. private void CameraVideoRecording_FormClosing(object sender, FormClosingEventArgs e)
  128. {
  129. stopRecording();
  130. }
  131. private void btnPlay_Click(object sender, EventArgs e)
  132. {
  133. if (_filename != null)
  134. {
  135. Process.Start(_filename);
  136. }
  137. }
  138. #region 控件
  139. /// <summary>
  140. /// Required designer variable.
  141. /// </summary>
  142. private System.ComponentModel.IContainer components = null;
  143. /// <summary>
  144. /// Clean up any resources being used.
  145. /// </summary>
  146. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  147. protected override void Dispose(bool disposing)
  148. {
  149. if (disposing && (components != null))
  150. {
  151. components.Dispose();
  152. }
  153. base.Dispose(disposing);
  154. }
  155. // Windows Form Designer generated code
  156. private void InitializeLanguageText()
  157. {
  158. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  159. this.btnPlay.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Play.text");
  160. this.btnStop.Text = PdnResources.GetString("Menu.stop.text");
  161. this.btnStart.Text = PdnResources.GetString("Menu.Started.text");
  162. this.groupBox2.Text = PdnResources.GetString("Menu.Setting.Text");
  163. this.rdoM.Text = PdnResources.GetString("Menu.millisecond.text");
  164. this.rdoS.Text = PdnResources.GetString("Menu.second.text");
  165. this.btnSavePath.Text = PdnResources.GetString("Menu.alter.text");
  166. this.label4.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Shootingformat.text") + ":";
  167. this.label3.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.shootingtime.text") + ":";
  168. this.label2.Text = PdnResources.GetString("Menu.Nameprefix.text") + ":";
  169. this.label1.Text = PdnResources.GetString("Menu.saveroute.text") + ":";
  170. this.Text = PdnResources.GetString("Menu.imagecapture.Videorecording.Videorecording.text");
  171. }
  172. /// <summary>
  173. /// Required method for Designer support - do not modify
  174. /// the contents of this method with the code editor.
  175. /// </summary>
  176. private void InitializeComponent()
  177. {
  178. this.components = new System.ComponentModel.Container();
  179. this.groupBox1 = new System.Windows.Forms.GroupBox();
  180. this.btnPlay = new System.Windows.Forms.Button();
  181. this.btnStop = new System.Windows.Forms.Button();
  182. this.btnStart = new System.Windows.Forms.Button();
  183. this.groupBox2 = new System.Windows.Forms.GroupBox();
  184. this.cmbVideoFormat = new System.Windows.Forms.ComboBox();
  185. this.rdoM = new System.Windows.Forms.RadioButton();
  186. this.rdoS = new System.Windows.Forms.RadioButton();
  187. this.nudDuration = new System.Windows.Forms.NumericUpDown();
  188. this.btnSavePath = new System.Windows.Forms.Button();
  189. this.txtPrefix = new System.Windows.Forms.TextBox();
  190. this.txtSavePath = new System.Windows.Forms.TextBox();
  191. this.label4 = new System.Windows.Forms.Label();
  192. this.label3 = new System.Windows.Forms.Label();
  193. this.label2 = new System.Windows.Forms.Label();
  194. this.label1 = new System.Windows.Forms.Label();
  195. this.timer1 = new System.Windows.Forms.Timer(this.components);
  196. this.groupBox1.SuspendLayout();
  197. this.groupBox2.SuspendLayout();
  198. ((System.ComponentModel.ISupportInitialize)(this.nudDuration)).BeginInit();
  199. this.SuspendLayout();
  200. //
  201. // groupBox1
  202. //
  203. this.groupBox1.Controls.Add(this.btnPlay);
  204. this.groupBox1.Controls.Add(this.btnStop);
  205. this.groupBox1.Controls.Add(this.btnStart);
  206. this.groupBox1.Location = new System.Drawing.Point(12, 12);
  207. this.groupBox1.Name = "groupBox1";
  208. this.groupBox1.Size = new System.Drawing.Size(428, 75);
  209. this.groupBox1.TabIndex = 0;
  210. this.groupBox1.TabStop = false;
  211. this.groupBox1.Text = "操作";
  212. //
  213. // btnPlay
  214. //
  215. this.btnPlay.Enabled = false;
  216. this.btnPlay.Location = new System.Drawing.Point(340, 26);
  217. this.btnPlay.Name = "btnPlay";
  218. this.btnPlay.Size = new System.Drawing.Size(68, 30);
  219. this.btnPlay.TabIndex = 2;
  220. this.btnPlay.Text = "播放";
  221. this.btnPlay.UseVisualStyleBackColor = true;
  222. this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
  223. //
  224. // btnStop
  225. //
  226. this.btnStop.Enabled = false;
  227. this.btnStop.Location = new System.Drawing.Point(266, 26);
  228. this.btnStop.Name = "btnStop";
  229. this.btnStop.Size = new System.Drawing.Size(68, 30);
  230. this.btnStop.TabIndex = 1;
  231. this.btnStop.Text = "停止";
  232. this.btnStop.UseVisualStyleBackColor = true;
  233. this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
  234. //
  235. // btnStart
  236. //
  237. this.btnStart.Location = new System.Drawing.Point(192, 26);
  238. this.btnStart.Name = "btnStart";
  239. this.btnStart.Size = new System.Drawing.Size(68, 30);
  240. this.btnStart.TabIndex = 0;
  241. this.btnStart.Text = "开始";
  242. this.btnStart.UseVisualStyleBackColor = true;
  243. this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
  244. //
  245. // groupBox2
  246. //
  247. this.groupBox2.Controls.Add(this.cmbVideoFormat);
  248. this.groupBox2.Controls.Add(this.rdoM);
  249. this.groupBox2.Controls.Add(this.rdoS);
  250. this.groupBox2.Controls.Add(this.nudDuration);
  251. this.groupBox2.Controls.Add(this.btnSavePath);
  252. this.groupBox2.Controls.Add(this.txtPrefix);
  253. this.groupBox2.Controls.Add(this.txtSavePath);
  254. this.groupBox2.Controls.Add(this.label4);
  255. this.groupBox2.Controls.Add(this.label3);
  256. this.groupBox2.Controls.Add(this.label2);
  257. this.groupBox2.Controls.Add(this.label1);
  258. this.groupBox2.Location = new System.Drawing.Point(12, 100);
  259. this.groupBox2.Name = "groupBox2";
  260. this.groupBox2.Size = new System.Drawing.Size(428, 249);
  261. this.groupBox2.TabIndex = 1;
  262. this.groupBox2.TabStop = false;
  263. this.groupBox2.Text = "设置";
  264. //
  265. // cmbVideoFormat
  266. //
  267. this.cmbVideoFormat.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  268. this.cmbVideoFormat.FormattingEnabled = true;
  269. this.cmbVideoFormat.Location = new System.Drawing.Point(266, 173);
  270. this.cmbVideoFormat.Name = "cmbVideoFormat";
  271. this.cmbVideoFormat.Size = new System.Drawing.Size(95, 20);
  272. this.cmbVideoFormat.TabIndex = 10;
  273. //
  274. // rdoM
  275. //
  276. this.rdoM.AutoSize = true;
  277. this.rdoM.Location = new System.Drawing.Point(324, 127);
  278. this.rdoM.Name = "rdoM";
  279. this.rdoM.Size = new System.Drawing.Size(47, 16);
  280. this.rdoM.TabIndex = 9;
  281. this.rdoM.Text = "毫秒";
  282. this.rdoM.UseVisualStyleBackColor = true;
  283. //
  284. // rdoS
  285. //
  286. this.rdoS.AutoSize = true;
  287. this.rdoS.Checked = true;
  288. this.rdoS.Location = new System.Drawing.Point(260, 127);
  289. this.rdoS.Name = "rdoS";
  290. this.rdoS.Size = new System.Drawing.Size(35, 16);
  291. this.rdoS.TabIndex = 8;
  292. this.rdoS.TabStop = true;
  293. this.rdoS.Text = "秒";
  294. this.rdoS.UseVisualStyleBackColor = true;
  295. //
  296. // nudDuration
  297. //
  298. this.nudDuration.Location = new System.Drawing.Point(177, 125);
  299. this.nudDuration.Maximum = new decimal(new int[] {
  300. 276447231,
  301. 23283,
  302. 0,
  303. 0});
  304. this.nudDuration.Minimum = new decimal(new int[] {
  305. 1,
  306. 0,
  307. 0,
  308. 0});
  309. this.nudDuration.Name = "nudDuration";
  310. this.nudDuration.Size = new System.Drawing.Size(72, 21);
  311. this.nudDuration.TabIndex = 7;
  312. this.nudDuration.Value = new decimal(new int[] {
  313. 1,
  314. 0,
  315. 0,
  316. 0});
  317. //
  318. // btnSavePath
  319. //
  320. this.btnSavePath.Location = new System.Drawing.Point(367, 32);
  321. this.btnSavePath.Name = "btnSavePath";
  322. this.btnSavePath.Size = new System.Drawing.Size(55, 25);
  323. this.btnSavePath.TabIndex = 6;
  324. this.btnSavePath.Text = "更改";
  325. this.btnSavePath.UseVisualStyleBackColor = true;
  326. this.btnSavePath.Click += new System.EventHandler(this.btnSavePath_Click);
  327. //
  328. // txtPrefix
  329. //
  330. this.txtPrefix.Location = new System.Drawing.Point(103, 75);
  331. this.txtPrefix.Name = "txtPrefix";
  332. this.txtPrefix.Size = new System.Drawing.Size(258, 21);
  333. this.txtPrefix.TabIndex = 5;
  334. //
  335. // txtSavePath
  336. //
  337. this.txtSavePath.Location = new System.Drawing.Point(103, 32);
  338. this.txtSavePath.Name = "txtSavePath";
  339. this.txtSavePath.ReadOnly = true;
  340. this.txtSavePath.Size = new System.Drawing.Size(258, 21);
  341. this.txtSavePath.TabIndex = 4;
  342. //
  343. // label4
  344. //
  345. this.label4.AutoSize = true;
  346. this.label4.Location = new System.Drawing.Point(15, 176);
  347. this.label4.Name = "label4";
  348. this.label4.Size = new System.Drawing.Size(65, 12);
  349. this.label4.TabIndex = 3;
  350. this.label4.Text = "拍摄格式:";
  351. //
  352. // label3
  353. //
  354. this.label3.AutoSize = true;
  355. this.label3.Location = new System.Drawing.Point(15, 127);
  356. this.label3.Name = "label3";
  357. this.label3.Size = new System.Drawing.Size(65, 12);
  358. this.label3.TabIndex = 2;
  359. this.label3.Text = "拍摄时间:";
  360. //
  361. // label2
  362. //
  363. this.label2.AutoSize = true;
  364. this.label2.Location = new System.Drawing.Point(15, 80);
  365. this.label2.Name = "label2";
  366. this.label2.Size = new System.Drawing.Size(65, 12);
  367. this.label2.TabIndex = 1;
  368. this.label2.Text = "名称前缀:";
  369. //
  370. // label1
  371. //
  372. this.label1.AutoSize = true;
  373. this.label1.Location = new System.Drawing.Point(15, 35);
  374. this.label1.Name = "label1";
  375. this.label1.Size = new System.Drawing.Size(65, 12);
  376. this.label1.TabIndex = 0;
  377. this.label1.Text = "保存路径:";
  378. //
  379. // timer1
  380. //
  381. this.timer1.Enabled = true;
  382. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  383. //
  384. // CameraVideoRecording
  385. //
  386. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  387. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  388. this.ClientSize = new System.Drawing.Size(459, 373);
  389. this.Controls.Add(this.groupBox2);
  390. this.Controls.Add(this.groupBox1);
  391. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  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. }