ImageProvider.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using AI.Common;
  2. using AIDiagnosis.Common.Interfaces;
  3. using AIDiagnosisDemo.Infrastucture;
  4. using System;
  5. using System.Drawing;
  6. namespace AIDiagnosisDemo.Service
  7. {
  8. internal class ImageProvider : IImageProvider, IDisposable
  9. {
  10. private IPlayer _player;
  11. /// <summary>
  12. /// Raised when a bitmap buffer provided by the provider.
  13. /// </summary>
  14. public event EventHandler<byte[]> ImageProvided;
  15. /// <summary>
  16. /// Raised when a RawImage provided by the provider.
  17. /// </summary>
  18. public event EventHandler<RawImage> RawImageProvided;
  19. public ImageProvider()
  20. {
  21. PlayerManager.Instance.PlayerChanged += OnPlayerChanged;
  22. OnPlayerChanged(null, null);
  23. }
  24. /// <summary>
  25. /// 开始监听
  26. /// </summary>
  27. public void Start()
  28. {
  29. if (_player != null)
  30. {
  31. _player.InputFrameReceived += OnInputFrameReceived;
  32. }
  33. }
  34. /// <summary>
  35. /// 停止监听
  36. /// </summary>
  37. public void Stop()
  38. {
  39. if (_player != null)
  40. {
  41. _player.InputFrameReceived -= OnInputFrameReceived;
  42. }
  43. }
  44. private void OnInputFrameReceived(object sender, Bitmap bitmap)
  45. {
  46. var buffer = BitmapHelper.BitmapToBytes(bitmap);
  47. ImageProvided?.Invoke(this, buffer);
  48. }
  49. private void OnPlayerChanged(object sender, EventArgs e)
  50. {
  51. if (_player != null)
  52. {
  53. _player.InputFrameReceived -= OnInputFrameReceived;
  54. }
  55. _player = PlayerManager.Instance.Player;
  56. if (_player != null)
  57. {
  58. _player.InputFrameReceived += OnInputFrameReceived;
  59. }
  60. }
  61. public void Dispose()
  62. {
  63. PlayerManager.Instance.PlayerChanged -= OnPlayerChanged;
  64. }
  65. }
  66. }