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;
///
/// Raised when a bitmap buffer provided by the provider.
///
public event EventHandler ImageProvided;
///
/// Raised when a RawImage provided by the provider.
///
public event EventHandler RawImageProvided;
public ImageProvider()
{
PlayerManager.Instance.PlayerChanged += OnPlayerChanged;
OnPlayerChanged(null, null);
}
///
/// 开始监听
///
public void Start()
{
if (_player != null)
{
_player.InputFrameReceived += OnInputFrameReceived;
}
}
///
/// 停止监听
///
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;
}
}
}