private void Slider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { RxVideo.Seek((int)Slider.Value); if (active) { ReceiveVideoStream(); } }
//Rxの1つ目、映像配信のObservableを呼ぶところ private void ReceiveVideoStream() { vidDisposer = RxVideo.CaptureStream() .Where(obj => !obj.Frame.Empty()) //もし空の画像が飛んで来たらシャットアウト .Select(obj => //画像にちょっとおめかし { Cv2.PutText(obj.Frame, Path.GetFileNameWithoutExtension(_videoName), new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyPlain, 2, new Scalar(0, 0, 70), 3); return(obj); }) .ObserveOnDispatcher() //別スレッドからUIにアクセスできないため、部分的にスレッドを戻す .Subscribe(obj => //以下、メインスレッド { Slider.Maximum = obj.FrameCount; Slider.Value = obj.CurrentFrameNumber; Image.Source = obj.Frame.ToBitmapSource(); if (obj.CurrentFrameNumber == obj.FrameCount - 1) { RxVideo.Close(); Slider.Value = 0; active = false; ChangeStatus(Mode.Video); } }); }
private void OpenCloseButton_Click(object sender, RoutedEventArgs e) { var status = (Mode)ModeCombo.SelectedIndex + 1; switch (status) { case Mode.Video: if (!active) { var op = new OpenFileDialog(); op.Title = "動画ファイルを開く"; op.Filter = "(*.mp4)|*.mp4"; if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK) { active = true; _videoName = op.FileName; ChangeStatus(status); RxVideo.OpenFile(_videoName, 640, 480); ReceiveVideoStream(); PauseButton.IsEnabled = true; Slider.IsEnabled = true; } } else { active = false; ChangeStatus(status); PauseButton.IsEnabled = false; Slider.IsEnabled = false; vidDisposer?.Dispose(); RxVideo.Close(); } break; case Mode.Chart: if (!active) { active = true; ChangeStatus(status); var timeLoc = new OpenCvSharp.Point(1100, 30); var sinLoc = new OpenCvSharp.Point(1100, 60); var noisySinLoc = new OpenCvSharp.Point(1100, 90); var fontscale = 1.0; var color = new Scalar(100, 100, 255); //Rxの2つ目、数値のストリームを受け取る部分 RxLogger.MakeStream(10) .Select(obj => { Cv2.PutText(obj.Frame, $"Time : {obj.Time:f1}", timeLoc, HersheyFonts.HersheySimplex, fontscale, color, 2); Cv2.PutText(obj.Frame, $"Sin : {obj.Sin:f2}", sinLoc, HersheyFonts.HersheySimplex, fontscale, color, 2); Cv2.PutText(obj.Frame, $"NoisySin : {obj.NoisySin:f2}", noisySinLoc, HersheyFonts.HersheySimplex, fontscale, color, 2); return(obj.Frame); }) .ObserveOnDispatcher() .Subscribe(frame => { Image.Source = frame.ToBitmapSource(); }); } else { RxLogger.Close(); active = false; ChangeStatus(status); } break; default: break; } }