12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using AI.Common;
- using AIDiagnosis.Common.Interfaces;
- using AIDiagnosisDemo.Infrastucture;
- using System;
- using System.Drawing;
- namespace AIDiagnosisDemo.Service
- {
- internal class ImageProvider : IImageProvider, IDisposable
- {
- private IPlayer _player;
- /// <summary>
- /// Raised when a bitmap buffer provided by the provider.
- /// </summary>
- public event EventHandler<byte[]> ImageProvided;
- /// <summary>
- /// Raised when a RawImage provided by the provider.
- /// </summary>
- public event EventHandler<RawImage> RawImageProvided;
- public ImageProvider()
- {
- PlayerManager.Instance.PlayerChanged += OnPlayerChanged;
- OnPlayerChanged(null, null);
- }
- /// <summary>
- /// 开始监听
- /// </summary>
- public void Start()
- {
- if (_player != null)
- {
- _player.InputFrameReceived += OnInputFrameReceived;
- }
- }
- /// <summary>
- /// 停止监听
- /// </summary>
- public void Stop()
- {
- if (_player != null)
- {
- _player.InputFrameReceived -= OnInputFrameReceived;
- }
- }
- private void OnInputFrameReceived(object sender, Bitmap bitmap)
- {
- var buffer = BitmapHelper.BitmapToBytes(bitmap);
- ImageProvided?.Invoke(this, buffer);
- }
- private void OnPlayerChanged(object sender, EventArgs e)
- {
- if (_player != null)
- {
- _player.InputFrameReceived -= OnInputFrameReceived;
- }
- _player = PlayerManager.Instance.Player;
- if (_player != null)
- {
- _player.InputFrameReceived += OnInputFrameReceived;
- }
- }
- public void Dispose()
- {
- PlayerManager.Instance.PlayerChanged -= OnPlayerChanged;
- }
- }
- }
|