public IEnumerator <ITask> QueryFrameHandler(webcam.QueryFrame query) { if (_state.FramesOnDemand) { var request = new InternalFrameRequest { DsspQuery = query }; _frameRequestPort.Post(request); yield return(Arbiter.Receive(false, request.ResponsePort, EmptyHandler)); } else if (_state.Image == null) { query.ResponsePort.Post(new webcam.QueryFrameResponse()); } else { ReplyToQueryFrame(query, _state.Image); } }
void ReplyToQueryFrame(webcam.QueryFrame query, Bitmap image) { Size size = new Size((int)query.Body.Size.X, (int)query.Body.Size.Y); bool noresize = (size == _state.Image.Size || size.Width == 0 || size.Height == 0 || size.Width >= _state.Image.Width || size.Height >= _state.Image.Height); if (query.Body.Format == Guid.Empty) { // raw image requested; BitmapData raw = null; Bitmap bmp = null; try { #if URT_MINCLR noresize = true; bmp = image; #else if (noresize) { bmp = image; } else { bmp = new Bitmap(image, size); } #endif raw = bmp.LockBits( new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb ); int bytesize = raw.Height * Math.Abs(raw.Stride); webcam.QueryFrameResponse response = new webcam.QueryFrameResponse(); response.TimeStamp = _state.LastFrameUpdate; byte[] rawImage = new byte[bytesize]; response.Size = new Size(raw.Width, raw.Height); response.Format = Guid.Empty; System.Runtime.InteropServices.Marshal.Copy(raw.Scan0, rawImage, 0, bytesize); response.Frame = rawImage; query.ResponsePort.Post(response); } finally { if (raw != null) { bmp.UnlockBits(raw); } if (bmp != null && !noresize) { bmp.Dispose(); } } } else { ImageFormat format; if (query.Body.Format == _formatBmp) { format = ImageFormat.Bmp; } else if (query.Body.Format == _formatJpeg) { format = ImageFormat.Jpeg; } else if (query.Body.Format == _formatGif) { format = ImageFormat.Gif; } else if (query.Body.Format == _formatPng) { format = ImageFormat.Png; } else { query.ResponsePort.Post( Fault.FromCodeSubcodeReason( FaultCodes.Receiver, DsspFaultCodes.ActionNotSupported, "Unsupported image format" ) ); return; } using (MemoryStream stream = new MemoryStream()) { if (noresize) { size = image.Size; image.Save(stream, format); } else { #if URT_MINCLR size = _state.Image.Size; _state.Image.Save(stream, format); #else using (Bitmap temp = new Bitmap(image, size)) { temp.Save(stream, format); } #endif } webcam.QueryFrameResponse response = new webcam.QueryFrameResponse(); response.TimeStamp = _state.LastFrameUpdate; response.Frame = new byte[(int)stream.Length]; response.Size = size; response.Format = query.Body.Format; stream.Position = 0; stream.Read(response.Frame, 0, response.Frame.Length); query.ResponsePort.Post(response); } } }
public void QueryFrameHandler(webcam.QueryFrame query) { if (_state.Image == null) { query.ResponsePort.Post(new webcam.QueryFrameResponse()); return; } if (query.Body.Format == Guid.Empty) { // raw image requested; BitmapData raw = null; try { raw = _state.Image.LockBits(new Rectangle(Point.Empty, new Size((int)_state.ImageSize.X, (int)_state.ImageSize.Y)), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); int size = raw.Height * raw.Stride; webcam.QueryFrameResponse response = new webcam.QueryFrameResponse(); response.TimeStamp = _state.LastFrameUpdate; response.Frame = new byte[size]; response.Size = new Size(raw.Width, raw.Height); response.Format = Guid.Empty; System.Runtime.InteropServices.Marshal.Copy(raw.Scan0, response.Frame, 0, size); query.ResponsePort.Post(response); } finally { if (raw != null) { _state.Image.UnlockBits(raw); } } } else { ImageFormat format = new ImageFormat(query.Body.Format); using (MemoryStream stream = new MemoryStream()) { Size size = new Size((int)query.Body.Size.X, (int)query.Body.Size.Y); if (size == _state.Image.Size || size.Width == 0 || size.Height == 0 || size.Width >= _state.Image.Width || size.Height >= _state.Image.Height) { size = _state.Image.Size; _state.Image.Save(stream, format); } else { using (Bitmap temp = new Bitmap( _state.Image, size)) { temp.Save(stream, format); } } webcam.QueryFrameResponse response = new webcam.QueryFrameResponse(); response.TimeStamp = _state.LastFrameUpdate; response.Frame = new byte[(int)stream.Length]; response.Size = size; response.Format = format.Guid; stream.Position = 0; stream.Read(response.Frame, 0, response.Frame.Length); query.ResponsePort.Post(response); } } }
public IEnumerator <ITask> QueryFrameHandler(webcam.QueryFrame query) { if (_state.Image == null || _entity == null) { query.ResponsePort.Post(new webcam.QueryFrameResponse()); yield break; } Size size = new Size((int)query.Body.Size.X, (int)query.Body.Size.Y); if (query.Body.Format == Guid.Empty) { // raw image requested; BitmapData raw = null; try { lock (_state.Image) { bool isResizeRequired = false; // size not specified if (size.Width == 0) { size = _state.Image.Size; } else if (size.Width != _state.Image.Width || size.Height != _state.Image.Height) { isResizeRequired = true; } var image = _state.Image; if (isResizeRequired) { image = new Bitmap(image, size); } raw = image.LockBits(new Rectangle(Point.Empty, size), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); int byteSize = raw.Height * raw.Stride; webcam.QueryFrameResponse response = new webcam.QueryFrameResponse(); response.TimeStamp = _state.LastFrameUpdate; response.Frame = new byte[byteSize]; response.Size = new Size(raw.Width, raw.Height); response.Format = Guid.Empty; System.Runtime.InteropServices.Marshal.Copy(raw.Scan0, response.Frame, 0, byteSize); query.ResponsePort.Post(response); image.UnlockBits(raw); } } catch (Exception ex) { if (raw != null) { _state.Image.UnlockBits(raw); } query.ResponsePort.Post(Fault.FromException(ex)); } } else { ImageFormat format = new ImageFormat(query.Body.Format); using (MemoryStream stream = new MemoryStream()) { try { lock (_state.Image) { if (size == _state.Image.Size || size.Width == 0 || size.Height == 0 || size.Width >= _state.Image.Width || size.Height >= _state.Image.Height) { size = _state.Image.Size; _state.Image.Save(stream, format); } else { using (Bitmap temp = new Bitmap( _state.Image, size)) { temp.Save(stream, format); } } } webcam.QueryFrameResponse response = new webcam.QueryFrameResponse(); response.TimeStamp = _state.LastFrameUpdate; response.Frame = new byte[(int)stream.Length]; response.Size = size; response.Format = format.Guid; stream.Position = 0; stream.Read(response.Frame, 0, response.Frame.Length); query.ResponsePort.Post(response); } catch (Exception ex) { query.ResponsePort.Post(Fault.FromException(ex)); } } } yield break; }