private void ImageProcessorMain() { while (!stopImageProcessing) { // Get an image to process. UnprocessedImage current = null; while (current == null) { lock (unprocessedImageLock) { if (unprocessedImages.Count > 0) { current = unprocessedImages.Dequeue(); } } if (current == null) { try { unprocessedImagesNoLongerEmptyEvent.WaitOne(); } catch (ThreadInterruptedException) { } } if (stopImageProcessing) { return; } } // Process it. Bitmap bitmap = null; switch (current.Format) { case "YUYV": bitmap = YuyvUtilities.ConvertYUYVToColorBitmap(current.Width, current.Height, current.Data); break; case "MJPG": bitmap = ConvertMJPGToColorBitmap(current); break; default: Debug.WriteLine(String.Format("ERROR: unknown image type {0}.", current.Format)); break; } if (bitmap != null && ImageArrived != null) { ImageArrived(this, new ImageArrivedEventArgs() { Bitmap = bitmap, ImageNumber = current.ImageNumber, Timestamp = current.TimeStamp }); } } }
void ProcessDataBlock(SocketAsyncEventArgs args) { numImagesBytesReceived += args.BytesTransferred; if (numImagesBytesReceived < imageBuffer.Length) { // No, so wait till we get some more data. args.SetBuffer(numImagesBytesReceived, imageBuffer.Length - numImagesBytesReceived); ReceiveNextPacketAsync(args); return; } numImagesSuccesfullyReceived++; var image = new UnprocessedImage(); image.Width = imageHeader.Width; image.Height = imageHeader.Height; image.Format = "" + (char)imageHeader.Format1 + (char)imageHeader.Format2 + (char)imageHeader.Format3 + (char)imageHeader.Format4; image.TimeStamp = imageHeader.TimeStamp; image.Data = imageBuffer; lock (unprocessedImageLock) { unprocessedImages.Enqueue(image); if (unprocessedImages.Count == 1) { unprocessedImagesNoLongerEmptyEvent.Set(); } while (unprocessedImages.Count > MaxQueueSize) { unprocessedImages.Dequeue(); } } imageBuffer = null; PrepareToReceiveImageHeader(args); }
private static Bitmap ConvertMJPGToColorBitmap(UnprocessedImage unprocessedImage) { byte[] jpegData = MJpegUtilities.ConvertMJpegBufferToJpeg(unprocessedImage.Data); return((Bitmap)Image.FromStream(new MemoryStream(jpegData))); }
void ProcessDataBlock(SocketAsyncEventArgs args) { numImagesBytesReceived += args.BytesTransferred; if (numImagesBytesReceived < imageBuffer.Length) { // No, so wait till we get some more data. args.SetBuffer(numImagesBytesReceived, imageBuffer.Length - numImagesBytesReceived); ReceiveNextPacketAsync(args); return; } numImagesSuccesfullyReceived++; var image = new UnprocessedImage(); image.Width = imageHeader.Width; image.Height = imageHeader.Height; image.Format = "" + (char)imageHeader.Format1 + (char)imageHeader.Format2 + (char)imageHeader.Format3 +(char)imageHeader.Format4; image.TimeStamp = imageHeader.TimeStamp; image.Data = imageBuffer; lock (unprocessedImageLock) { unprocessedImages.Enqueue(image); if (unprocessedImages.Count == 1) unprocessedImagesNoLongerEmptyEvent.Set(); while (unprocessedImages.Count > MaxQueueSize) unprocessedImages.Dequeue(); } imageBuffer = null; PrepareToReceiveImageHeader(args); }
private static Bitmap ConvertMJPGToColorBitmap(UnprocessedImage unprocessedImage) { byte[] jpegData = MJpegUtilities.ConvertMJpegBufferToJpeg(unprocessedImage.Data); return (Bitmap)Image.FromStream(new MemoryStream(jpegData)); }