예제 #1
0
        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
                    });
                }
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
 private static Bitmap ConvertMJPGToColorBitmap(UnprocessedImage unprocessedImage)
 {
     byte[] jpegData = MJpegUtilities.ConvertMJpegBufferToJpeg(unprocessedImage.Data);
     return((Bitmap)Image.FromStream(new MemoryStream(jpegData)));
 }
예제 #4
0
        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);
        }
예제 #5
0
 private static Bitmap ConvertMJPGToColorBitmap(UnprocessedImage unprocessedImage)
 {
     byte[] jpegData = MJpegUtilities.ConvertMJpegBufferToJpeg(unprocessedImage.Data);
     return (Bitmap)Image.FromStream(new MemoryStream(jpegData));
 }