/// <summary> /// Constructor. /// </summary> /// <param name="fromState">State before event.</param> /// <param name="toState">State after event.</param> /// <param name="msg">A freeform string message for the user.</param> /// <param name="causingException">Possible exception that caused an error.</param> public ScallopSensorStatusChangedEventArgs(ScallopSensorState fromState, ScallopSensorState toState, string msg, Exception causingException) { this.oldState = fromState; this.newState = toState; this.causingException = causingException; this.msg = msg; }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void getFrames_completed(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) // CancelAsync called { this.streaming = false; this.doClosed(this, EventArgs.Empty); return; } else if (e.Error != null) // Exception occured { this.doError(this, new ScallopSensorStatusChangedEventArgs(myState, ScallopSensorState.Error, "", e.Error)); this.myState = ScallopSensorState.Error; this.doClosed(this, EventArgs.Empty); this.streaming = false; this.doInfo(this, new ScallopInfoEventArgs("Retrying in 5 seconds...")); Thread.Sleep(5000); this.Start(); return; } else { System.Diagnostics.Debug.Assert(false, /* we should never get here! */ "An impossible event happened at getFrames_completed"); } }
private void doOpened(object sender, EventArgs e) { if (this.StatusChanged != null) { this.StatusChanged(this, new ScallopSensorStatusChangedEventArgs(myState, ScallopSensorState.Active)); } myState = ScallopSensorState.Active; }
/// <summary> /// Stops receiving sensor data. /// </summary> public void Stop() { if (this.frameHandlerThread != null) { this.frameHandlerThread.CancelAsync(); } this.myState = ScallopSensorState.Idle; }
private void doError(object sender, ScallopSensorStatusChangedEventArgs e) { if (this.StatusChanged != null) { this.StatusChanged(sender, e); } myState = ScallopSensorState.Error; }
private void doStatusChanged(object sender, ScallopSensorStatusChangedEventArgs e) { ScallopSensorState oldState = this.myState; this.myState = e.NewState; if (this.StatusChanged != null) { this.StatusChanged(sender, e); } }
/// <summary> /// Constructor. Sets up the configuration schema. /// </summary> /// <exception cref="ApplicationException">Thrown when there is an error with getting the schema from the assembly.</exception> public AxisCamera() { try { Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Scallop.Sensor.Axis.AxisCameraConfig.xsd"); XmlSerializer serializer = new XmlSerializer(typeof(XmlSchema)); this.configSchema = (XmlSchema)(serializer.Deserialize(stream)); this.myState = ScallopSensorState.Idle; } catch (Exception ex) { throw new ScallopException("Reading Axis configuration schema failed.", ex); } }
/// <summary> /// Constructor. /// </summary> /// <param name="fromState">State before event.</param> /// <param name="toState">State after event.</param> public ScallopSensorStatusChangedEventArgs(ScallopSensorState fromState, ScallopSensorState toState) : this(fromState, toState, null, null) { }
private void doStatusChanged(object sender, ScallopSensorStatusChangedEventArgs e) { ScallopSensorState oldState = this.myState; this.myState = e.NewState; if (this.StatusChanged != null) this.StatusChanged(sender, e); }
private void doError(object sender, ScallopSensorStatusChangedEventArgs e) { if (this.StatusChanged != null) this.StatusChanged(sender, e); myState = ScallopSensorState.Error; }
/// <summary> /// Stops receiving sensor data. /// </summary> public void Stop() { if (this.frameHandlerThread != null) this.frameHandlerThread.CancelAsync(); this.myState = ScallopSensorState.Idle; }
/// <summary> /// Constructor. /// </summary> /// <param name="fromState">State before event.</param> /// <param name="toState">State after event.</param> /// <param name="msg">A freeform string message for the user.</param> public ScallopSensorStatusChangedEventArgs(ScallopSensorState fromState, ScallopSensorState toState, string msg) : this(fromState, toState, msg, null) { }
void frameThread_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = (BackgroundWorker)sender; MediaPlayer myPlayer = new MediaPlayer(); myPlayer.Open(parameters.SourceUri); myPlayer.Volume = 0; myPlayer.Play(); while (myPlayer.NaturalVideoWidth < 1) { System.Threading.Thread.Sleep(100); } myState = ScallopSensorState.Active; this.StatusChanged(this, new ScallopSensorStatusChangedEventArgs(ScallopSensorState.Idle, ScallopSensorState.Active)); RenderTargetBitmap rtb = new RenderTargetBitmap(myPlayer.NaturalVideoWidth, myPlayer.NaturalVideoHeight, 96, 96, PixelFormats.Pbgra32); while (true) { if (bw.CancellationPending == true) { myPlayer.Stop(); e.Cancel = true; return; } DrawingVisual dv = new DrawingVisual(); DrawingContext dc = dv.RenderOpen(); dc.DrawVideo(myPlayer, new Rect(0, 0, myPlayer.NaturalVideoWidth, myPlayer.NaturalVideoHeight)); dc.Close(); rtb.Clear(); rtb.Render(dv); BitmapFrame bmp = BitmapFrame.Create(rtb); switch (parameters.FrameFormat) { case ("System.Drawing.Bitmap"): using (MemoryStream memoryStream = new MemoryStream()) { //bitmapEncoder.Frames.Add(BitmapFrame.Create(bmp)); PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(bmp); bitmapEncoder.Save(memoryStream); memoryStream.Flush(); using (Bitmap bmp2 = Bitmap.FromStream(memoryStream) as Bitmap, bmp3 = new Bitmap(bmp2.Width, bmp2.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { bmp3.SetResolution(bmp2.HorizontalResolution, bmp2.VerticalResolution); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp3)) { g.DrawImage(bmp2, 0, 0); } if (this.Data != null) { this.Data(this, new ScallopSensorDataEventArgs((System.Drawing.Bitmap)bmp3.Clone(), "new frame")); } } } break; case ("System.Windows.Media.Imaging.BitmapSource"): if (this.Data != null) { this.Data(this, new ScallopSensorDataEventArgs(bmp as BitmapSource, "new frame")); } break; } if (myPlayer.Position >= myPlayer.NaturalDuration) { myPlayer.Stop(); myPlayer.Position = TimeSpan.Zero; myPlayer.Play(); } } }
void frameThread_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = (BackgroundWorker)sender; MediaPlayer myPlayer = new MediaPlayer(); myPlayer.Open(parameters.SourceUri); myPlayer.Volume = 0; myPlayer.Play(); while (myPlayer.NaturalVideoWidth < 1) System.Threading.Thread.Sleep(100); myState = ScallopSensorState.Active; this.StatusChanged(this, new ScallopSensorStatusChangedEventArgs(ScallopSensorState.Idle, ScallopSensorState.Active)); RenderTargetBitmap rtb = new RenderTargetBitmap(myPlayer.NaturalVideoWidth, myPlayer.NaturalVideoHeight, 96, 96, PixelFormats.Pbgra32); while (true) { if (bw.CancellationPending == true) { myPlayer.Stop(); e.Cancel = true; return; } DrawingVisual dv = new DrawingVisual(); DrawingContext dc = dv.RenderOpen(); dc.DrawVideo(myPlayer, new Rect(0, 0, myPlayer.NaturalVideoWidth, myPlayer.NaturalVideoHeight)); dc.Close(); rtb.Clear(); rtb.Render(dv); BitmapFrame bmp = BitmapFrame.Create(rtb); switch (parameters.FrameFormat) { case ("System.Drawing.Bitmap"): using (MemoryStream memoryStream = new MemoryStream()) { //bitmapEncoder.Frames.Add(BitmapFrame.Create(bmp)); PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(bmp); bitmapEncoder.Save(memoryStream); memoryStream.Flush(); using (Bitmap bmp2 = Bitmap.FromStream(memoryStream) as Bitmap, bmp3 = new Bitmap(bmp2.Width,bmp2.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb)) { bmp3.SetResolution(bmp2.HorizontalResolution, bmp2.VerticalResolution); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp3)) { g.DrawImage(bmp2, 0, 0); } if (this.Data != null) this.Data(this, new ScallopSensorDataEventArgs((System.Drawing.Bitmap)bmp3.Clone(), "new frame")); } } break; case ("System.Windows.Media.Imaging.BitmapSource"): if (this.Data != null) this.Data(this, new ScallopSensorDataEventArgs(bmp as BitmapSource, "new frame")); break; } if (myPlayer.Position >= myPlayer.NaturalDuration) { myPlayer.Stop(); myPlayer.Position = TimeSpan.Zero; myPlayer.Play(); } } }