星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeviceWindow.xaml.cs 12KB

пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using Accord.Video;
  2. using Accord.Video.DirectShow;
  3. using Common.system;
  4. using NAudio.Wave;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Windows;
  12. using System.Windows.Input;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Threading;
  15. namespace XHWK.WKTool
  16. {
  17. /// <summary>
  18. /// DeviceWindow.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class DeviceWindow : Window
  21. {
  22. private bool cameraGood = false;
  23. private bool microphoneGood = false;
  24. private bool loudspeakerGood = false;
  25. public DeviceWindow()
  26. {
  27. InitializeComponent();
  28. cameraCheck();
  29. }
  30. /// <summary>
  31. /// 初始化
  32. /// </summary>
  33. public void Initialize()
  34. {
  35. TxbCamera.Text = "";
  36. TxbSpeaker.Text = "";
  37. TxbMicrophone.Text = "";
  38. }
  39. /// <summary>
  40. /// 窗体移动
  41. /// </summary>
  42. /// <param name="sender"></param>
  43. /// <param name="e"></param>
  44. private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  45. {
  46. DragMove();
  47. }
  48. /// <summary>
  49. /// 关闭当前页
  50. /// </summary>
  51. /// <param name="sender"></param>
  52. /// <param name="e"></param>
  53. private void BtnDown_Click(object sender, RoutedEventArgs e)
  54. {
  55. imgPlayer.Visibility = Visibility.Hidden;
  56. closeCamera();
  57. if (waveIn != null)
  58. {
  59. waveIn.StopRecording();
  60. }
  61. Close();
  62. }
  63. #region 摄像头
  64. private void cameraCheck()
  65. {
  66. HidePage();
  67. GridCamera.Visibility = Visibility.Visible;
  68. var devs = new FilterInfoCollection(FilterCategory.VideoInputDevice);//获取摄像头列表
  69. CmbCameraList.ItemsSource = devs;
  70. if (devs.Count > 0)
  71. {
  72. CmbCameraList.SelectedIndex = 0;
  73. }
  74. }
  75. private void CmbCameraList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  76. {
  77. Console.WriteLine("选择变化");
  78. checkCamera();
  79. }
  80. private VideoCaptureDevice Camera;//用来操作摄像头
  81. /// <summary>
  82. /// 检测
  83. /// </summary>
  84. /// <param name="sender"></param>
  85. /// <param name="e"></param>
  86. private void checkCamera()
  87. {
  88. try
  89. {
  90. closeCamera();
  91. imgPlayer.Visibility = Visibility.Visible;
  92. if (CmbCameraList.SelectedIndex >= 0)
  93. {
  94. var info = CmbCameraList.SelectedItem as FilterInfo;
  95. Camera = new VideoCaptureDevice(info.MonikerString);
  96. //配置录像参数(宽,高,帧率,比特率等参数)VideoCapabilities这个属性会返回摄像头支持哪些配置
  97. Camera.VideoResolution = Camera.VideoCapabilities[0];
  98. Camera.NewFrame += Camera_NewFrame;//设置回调,aforge会不断从这个回调推出图像数据,SnapshotFrame也是有待比较
  99. Camera.Start();//打开摄像头
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. LogHelper.WriteErrLog("【摄像头检测】(DeviceWindow)摄像头不可用:" + ex.Message, ex);
  105. }
  106. }
  107. private void closeCamera()
  108. {
  109. try
  110. {
  111. if (Camera != null)
  112. {
  113. Camera.SignalToStop();
  114. }
  115. }
  116. catch (Exception)
  117. {
  118. }
  119. }
  120. /// <summary>
  121. /// 摄像头输出回调
  122. /// </summary>
  123. private void Camera_NewFrame(object sender, NewFrameEventArgs eventArgs)
  124. {
  125. Dispatcher.Invoke(new Action(
  126. () =>
  127. {
  128. MemoryStream ms = new MemoryStream();
  129. eventArgs.Frame.Save(ms, ImageFormat.Bmp);
  130. BitmapImage image = new BitmapImage();
  131. image.BeginInit();
  132. image.StreamSource = new MemoryStream(ms.GetBuffer());
  133. ms.Close();
  134. image.EndInit();
  135. imgPlayer.Source = image;
  136. }));//同步显示
  137. }
  138. /// <summary>
  139. /// 停止
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void BtnCameraStop_Click(object sender, RoutedEventArgs e)
  144. {
  145. if (Camera != null)
  146. {
  147. Camera.SignalToStop();
  148. }
  149. }
  150. #endregion 摄像头
  151. #region 扬声器
  152. /// <summary>
  153. /// 扬声器
  154. /// </summary>
  155. /// <param name="sender"></param>
  156. /// <param name="e"></param>
  157. private void Speaker_Check()
  158. {
  159. closeCamera();
  160. CmbCameraList.SelectedIndex = -1;
  161. HidePage();
  162. GridSpeaker.Visibility = Visibility.Visible;
  163. }
  164. /// <summary>
  165. /// 检测扬声器
  166. /// </summary>
  167. /// <param name="sender"></param>
  168. /// <param name="e"></param>
  169. private void BtnSpeakerDetection_Click(object sender, RoutedEventArgs e)
  170. {
  171. Console.WriteLine("播放");
  172. MediaAudio.Source = new Uri(FileToolsCommon.GetFileAbsolutePath("/Resources/audiotest.mp3"));
  173. MediaAudio.Position = TimeSpan.Zero;
  174. MediaAudio.Play();
  175. ImgAcousticWave.Visibility = Visibility.Visible;
  176. }
  177. #region 音频播放
  178. /// <summary>
  179. /// 音频播放 同步进度条和视频进度
  180. /// </summary>
  181. /// <param name="sender"></param>
  182. /// <param name="e"></param>
  183. private void MediaAudio_MediaOpened(object sender, RoutedEventArgs e)
  184. {
  185. Console.WriteLine("播放中。。。");
  186. }
  187. private void MediaAudio_MediaEnded(object sender, RoutedEventArgs e)
  188. {
  189. MediaAudio.Stop();
  190. MediaAudio.Position = TimeSpan.Zero;
  191. }
  192. #endregion 音频播放
  193. #endregion 扬声器
  194. #region 麦克风
  195. /// <summary>
  196. /// 麦克风
  197. /// </summary>
  198. /// <param name="sender"></param>
  199. /// <param name="e"></param>
  200. private void Microphone_Check()
  201. {
  202. MediaAudio.Stop();
  203. ImgAcousticWave.Visibility = Visibility.Collapsed;
  204. HidePage();
  205. GridMicrophone.Visibility = Visibility.Visible;
  206. ThreadPool.QueueUserWorkItem(s =>
  207. {
  208. DataTable data = new DataTable();
  209. data.Columns.Add("Value");
  210. data.Columns.Add("Key");
  211. for (int n = 0; n < WaveIn.DeviceCount; n++)
  212. {
  213. var caps = WaveIn.GetCapabilities(n);
  214. //创建一行
  215. DataRow row = data.NewRow();
  216. //将此行添加到table中
  217. data.Rows.Add(row);
  218. data.Rows[n]["Value"] = caps.ProductName;
  219. data.Rows[n]["Key"] = n + "";
  220. }
  221. Dispatcher.Invoke(() =>
  222. {
  223. CmbMicrophoneList.ItemsSource = data.DefaultView;
  224. if (WaveIn.DeviceCount > 0)
  225. {
  226. CmbMicrophoneList.SelectedIndex = 0;
  227. }
  228. });
  229. });
  230. }
  231. private WaveInEvent waveIn = null;
  232. private void CmbMicrophoneList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  233. {
  234. if (waveIn != null)
  235. {
  236. waveIn.StopRecording();
  237. }
  238. volumeProgressBar.Value = 0;
  239. if (CmbMicrophoneList.SelectedIndex >= 0)
  240. {
  241. waveIn = new WaveInEvent();
  242. waveIn.DeviceNumber = CmbMicrophoneList.SelectedIndex;
  243. //开始录音,写数据
  244. waveIn.DataAvailable += (o, e1) =>
  245. {
  246. byte[] buf = e1.Buffer;
  247. float maxNumber = 0;
  248. for (int index = 0; index < buf.Length; index += 2)
  249. {
  250. short sample = (short)((buf[index + 1] << 8) | buf[index + 0]);
  251. float sample32 = sample / 32768f;
  252. sample32 = Math.Abs(sample32);
  253. if (sample32 > maxNumber)
  254. {
  255. maxNumber = sample32;
  256. }
  257. }
  258. Dispatcher.Invoke(() =>
  259. {
  260. volumeProgressBar.Value = maxNumber * 100;
  261. });
  262. };
  263. //结束录音
  264. waveIn.RecordingStopped += (s, a) =>
  265. {
  266. waveIn.Dispose();
  267. };
  268. waveIn.StartRecording();
  269. }
  270. }
  271. #endregion 麦克风
  272. #region 页面切换
  273. /// <summary>
  274. /// 检测页
  275. /// </summary>
  276. private void result_show()
  277. {
  278. HidePage();
  279. GridDetection.Visibility = Visibility.Visible;
  280. if (cameraGood)
  281. {
  282. TxbCamera.Text = "可用";
  283. }
  284. else
  285. {
  286. TxbCamera.Text = "不可用";
  287. }
  288. if (loudspeakerGood)
  289. {
  290. TxbSpeaker.Text = "可用";
  291. }
  292. else
  293. {
  294. TxbSpeaker.Text = "不可用";
  295. }
  296. if (microphoneGood)
  297. {
  298. TxbMicrophone.Text = "可用";
  299. }
  300. else
  301. {
  302. TxbMicrophone.Text = "不可用";
  303. }
  304. }
  305. /// <summary>
  306. /// 隐藏页
  307. /// </summary>
  308. private void HidePage()
  309. {
  310. GridDetection.Visibility = Visibility.Collapsed;
  311. GridCamera.Visibility = Visibility.Collapsed;
  312. GridSpeaker.Visibility = Visibility.Collapsed;
  313. GridMicrophone.Visibility = Visibility.Collapsed;
  314. try
  315. {
  316. CameraHelper.CloseDevice();
  317. }
  318. catch (Exception)
  319. {
  320. }
  321. }
  322. #endregion 页面切换
  323. #region 事件
  324. private void btn_camera_good_Click(object sender, RoutedEventArgs e)
  325. {
  326. cameraGood = true;
  327. Speaker_Check();
  328. }
  329. private void btn_camera_bad_Click(object sender, RoutedEventArgs e)
  330. {
  331. cameraGood = false;
  332. Speaker_Check();
  333. }
  334. private void btn_speaker_good_Click(object sender, RoutedEventArgs e)
  335. {
  336. loudspeakerGood = true;
  337. Microphone_Check();
  338. }
  339. private void btn_speaker_bad_Click(object sender, RoutedEventArgs e)
  340. {
  341. loudspeakerGood = false;
  342. Microphone_Check();
  343. }
  344. private void btn_microphone_good_Click(object sender, RoutedEventArgs e)
  345. {
  346. if (waveIn != null)
  347. {
  348. waveIn.StopRecording();
  349. }
  350. microphoneGood = true;
  351. result_show();
  352. CmbMicrophoneList.SelectedIndex = -1;
  353. }
  354. private void btn_microphone_bad_Click(object sender, RoutedEventArgs e)
  355. {
  356. if (waveIn != null)
  357. {
  358. waveIn.StopRecording();
  359. }
  360. microphoneGood = false;
  361. result_show();
  362. CmbMicrophoneList.SelectedIndex = -1;
  363. }
  364. /// <summary>
  365. /// 重新
  366. /// </summary>
  367. /// <param name="sender"></param>
  368. /// <param name="e"></param>
  369. private void BtnDetection_Click(object sender, RoutedEventArgs e)
  370. {
  371. cameraCheck();
  372. }
  373. #endregion 事件
  374. }
  375. }