所以我正在为 onvif IP 摄像机构建一个应用程序,我将其设置如下:
public partial class MainWindow : Window
{
private VideoViewerWPF _videoViewerWpf;
private BitmapSourceProvider _provider;
private IIPCamera _ipCamera;
private MediaConnector _connector;
private MotionDetector _detector;
public MainWindow()
{
InitializeComponent();
_connector = new MediaConnector();
_provider = new BitmapSourceProvider();
_detector = new MotionDetector();
SetVideoViewer();
}
private void SetVideoViewer()
{
_videoViewerWpf = new VideoViewerWPF
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Background = Brushes.Black,
Width = 1280,
Height = 720
};
CameraBox.Children.Add(_videoViewerWpf);
_videoViewerWpf.SetImageProvider(_provider);
}
private void CameraStateChanged(object sender, CameraStateEventArgs e)
{
Dispatcher.Invoke(() => {
labeltest.Content = e.State.ToString();
});
}
private void ConnectIPCamera_Click(object sender, RoutedEventArgs e)
{
var host = HostTextBox.Text;
var user = UserTextBox.Text;
var pass = Password.Password;
_ipCamera = IPCameraFactory.GetCamera(host, user, pass);
if (_ipCamera == null) return;
_connector.Connect(_ipCamera.VideoChannel, _detector);
_connector.Connect(_detector, _provider);
_ipCamera.Connect();
_ipCamera.CameraStateChanged += CameraStateChanged;
_videoViewerWpf.Start();
}
private void DisconnectIPCamera_Click(object sender, RoutedEventArgs e)
{
_videoViewerWpf.Stop();
_ipCamera.Disconnect();
_ipCamera.Dispose();
_connector.Disconnect(_ipCamera.VideoChannel, _detector);
_connector.Disconnect(_detector, _provider);
}
void GuiThread(Action action)
{
Dispatcher.BeginInvoke(action);
}
private void StartMotionDetection()
{
_detector.HighlightMotion = HighlightMotion.Highlight;
_detector.MotionColor = MotionColor.Red;
_detector.MotionDetection += Detector_MotionDetection;
_detector.Start();
}
void Detector_MotionDetection(object sender, MotionDetectionEvent e)
{
GuiThread(() =>
{
if (e.Detection)
MotionEventLabel.Content = "Motion Detected";
else
MotionEventLabel.Content = "Motion Stopped";
});
}
private void StopMotionDetection()
{
_detector.Stop();
_detector.MotionDetection -= Detector_MotionDetection;
_detector.Dispose();
}
private void MotionChecked(object sender, RoutedEventArgs e)
{
MotionEventLabel.Content = String.Empty;
if (sender is CheckBox check)
{
if ((bool)check.IsChecked)
StartMotionDetection();
else
StopMotionDetection();
}
}
private void Start_Click(object sender, RoutedEventArgs e)
{
if(_ipCamera.State == IPCameraState.Connected)
{
_ipCamera.CurrentStream = _ipCamera.AvailableStreams.FirstOrDefault(x => x.Name == "MJPEG");
_ipCamera.Start();
}
}
它连接得很好,但是当我启动流时,它开始连接流一秒钟,然后断开与网络的连接。我尝试了在互联网上可以找到的东西,但没有解决问题,我觉得我没有正确设置它,但这是第一次使用 onvif 相机和图书馆,任何建议或信息,谢谢!