Beispiel #1
0
        private void _JpegLiveSource_LiveContentEvent(object sender, EventArgs e)
        {
            LiveContentEventArgs args = e as LiveContentEventArgs;

            if (args != null)
            {
                if (args.LiveContent != null)
                {
                    int width  = args.LiveContent.Width;
                    int height = args.LiveContent.Height;

                    MemoryStream ms        = new MemoryStream(args.LiveContent.Content);
                    Bitmap       newBitmap = new Bitmap(ms);
                    if (ImageIsReady != null)
                    {
                        ImageIsReady(newBitmap);
                    }
                    else
                    {
                        _JpegLiveSource.LiveContentEvent -= _JpegLiveSource_LiveContentEvent;
                    }

                    ms.Close();
                    ms.Dispose();
                    args.LiveContent.Dispose();
                }
                else if (args.Exception != null)
                {
                    // Обработать любые исключения

                    Bitmap   bitmap = new Bitmap(_Width, _Height);
                    Graphics g      = Graphics.FromImage(bitmap);
                    g.FillRectangle(Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                    g.DrawString("Нет соединения ...", new Font(FontFamily.GenericMonospace, 12), Brushes.White, new PointF(20, _Height / 2 - 20));
                    g.Dispose();
                    //Готовое сообщение с ошибкой
                    ImageIsReady(new Bitmap(bitmap, new Size(_Width, _Height)));
                    bitmap.Dispose();
                }
            }
        }
        void BitmapLiveSourceLiveContentEvent(object sender, EventArgs e)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    // Make sure we execute on the UI thread before updating UI Controls
                    BeginInvoke(new EventHandler(BitmapLiveSourceLiveContentEvent), new[] { sender, e });
                }
                else
                {
                    LiveContentEventArgs args = e as LiveContentEventArgs;
                    if (args != null)
                    {
                        if (args.LiveContent != null)
                        {
                            LiveSourceBitmapContent bitmapContent = args.LiveContent as LiveSourceBitmapContent;
                            if (bitmapContent != null)
                            {
                                labelCount.Text = "" + (_counter++);
                                if (_stopped)
                                {
                                    bitmapContent.Dispose();
                                }
                                else
                                {
                                    // The following code does these functions:
                                    //    Get a IntPtr to the start of the GBR bitmap
                                    //    Transform via sample transformation (To be replaced with your C++ code)
                                    //    Create a Bitmap with the result
                                    //    Create a new Bitmap scaled to visible area on screen
                                    //    Assign new Bitmap into PictureBox
                                    //    Dispose first Bitmap
                                    //
                                    // The transformation is therefore done on the original image, but if the transformation is
                                    // keeping to the same size, then this would be much more effective if the resize was done first,
                                    // and the transformation afterwards.
                                    // Scaling can be done by setting the Width and Height on the

                                    int width  = bitmapContent.GetPlaneWidth(0);
                                    int height = bitmapContent.GetPlaneHeight(0);
                                    int stride = bitmapContent.GetPlaneStride(0);

                                    // When using RGB / BGR bitmaps, they have all bytes continues in memory.  The PlanePointer(0) is used for all planes:
                                    IntPtr plane0 = bitmapContent.GetPlanePointer(0);

                                    IntPtr newPlane0 = transform.Perform(plane0, stride, width, height);                                                // Make the sample transformation / color change

                                    Image myImage = new Bitmap(width, height, stride, PixelFormat.Format24bppRgb, newPlane0);

                                    // We need to resize to the displayed area
                                    pictureBoxEnhanced.Image = new Bitmap(myImage, pictureBoxEnhanced.Width, pictureBoxEnhanced.Height);

                                    myImage.Dispose();
                                    bitmapContent.Dispose();
                                    transform.Release(newPlane0);
                                }
                            }
                        }
                        else if (args.Exception != null)
                        {
                            // Handle any exceptions occurred inside toolkit or on the communication to the VMS

                            Bitmap   bitmap = new Bitmap(320, 240);
                            Graphics g      = Graphics.FromImage(bitmap);
                            g.FillRectangle(Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                            if (args.Exception is CommunicationMIPException)
                            {
                                g.DrawString("Connection lost to server ...", new Font(FontFamily.GenericMonospace, 12),
                                             Brushes.White, new PointF(20, pictureBoxEnhanced.Height / 2 - 20));
                            }
                            else
                            {
                                g.DrawString(args.Exception.Message, new Font(FontFamily.GenericMonospace, 12),
                                             Brushes.White, new PointF(20, pictureBoxEnhanced.Height / 2 - 20));
                            }
                            g.Dispose();
                            pictureBoxEnhanced.Image = new Bitmap(bitmap, pictureBoxEnhanced.Size);
                            bitmap.Dispose();
                        }
                    }
                }
            } catch (Exception ex)
            {
                EnvironmentManager.Instance.ExceptionDialog("BitmapLiveSourceLiveContentEvent", ex);
            }
        }
Beispiel #3
0
        /// <summary>
        /// This event is called when JPEG is available or some exception has occurred
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void JpegLiveSource1LiveNotificationEvent(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                if (OnMainThread)
                {
                    LiveContentEventArgs args = e as LiveContentEventArgs;
                    if (args != null && args.LiveContent != null)
                    {
                        // UI thread is too busy - discard this frame from display
                        args.LiveContent.Dispose();
                    }
                    return;
                }
                OnMainThread = true;
                // Make sure we execute on the UI thread before updating UI Controls
                BeginInvoke(new EventHandler(JpegLiveSource1LiveNotificationEvent), new[] { sender, e });
            }
            else
            {
                LiveContentEventArgs args = e as LiveContentEventArgs;
                if (args != null)
                {
                    if (args.LiveContent != null)
                    {
                        // Display the received JPEG
                        labelSize.Text = "" + args.LiveContent.Content.Length;

                        int width  = args.LiveContent.Width;
                        int height = args.LiveContent.Height;

                        MemoryStream ms        = new MemoryStream(args.LiveContent.Content);
                        Bitmap       newBitmap = new Bitmap(ms);
                        labelResolution.Text = "" + width + "x" + height;
                        if (pictureBox1.Size.Width != 0 && pictureBox1.Size.Height != 0)
                        {
                            if (!checkBoxAspect.Checked && (newBitmap.Width != pictureBox1.Width || newBitmap.Height != pictureBox1.Height))
                            {
                                pictureBox1.Image = new Bitmap(newBitmap, pictureBox1.Size);
                            }
                            else
                            {
                                pictureBox1.Image = newBitmap;
                            }
                        }
                        if (args.LiveContent.CroppingDefined)
                        {
                            labelCropRect.Text = "" + args.LiveContent.CropWidth + "x" + args.LiveContent.CropHeight;
                        }
                        else
                        {
                            labelCropRect.Text = "--";
                        }
                        textBoxDecodingStatus.Text = args.LiveContent.HardwareDecodingStatus;

                        ms.Close();
                        ms.Dispose();

                        _count++;
                        labelCount.Text = "" + _count;

                        args.LiveContent.Dispose();
                    }
                    else if (args.Exception != null)
                    {
                        // Handle any exceptions occurred inside toolkit or on the communication to the VMS

                        Bitmap   bitmap = new Bitmap(320, 240);
                        Graphics g      = Graphics.FromImage(bitmap);
                        g.FillRectangle(Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                        g.DrawString("Connection lost to server ...", new Font(FontFamily.GenericMonospace, 12), Brushes.White, new PointF(20, pictureBox1.Height / 2 - 20));
                        g.Dispose();
                        pictureBox1.Image = new Bitmap(bitmap, pictureBox1.Size);
                        bitmap.Dispose();
                    }
                }
                OnMainThread = false;
            }
        }
        private void JpegLiveSource1LiveNotificationEvent(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                if (OnMainThread)
                {
                    LiveContentEventArgs args = e as LiveContentEventArgs;
                    if (args != null && args.LiveContent != null)
                    {
                        // UI thread is too busy - discard this frame from display
                        args.LiveContent.Dispose();
                    }
                    return;
                }
                OnMainThread = true;
                // Make sure we execute on the UI thread before updating UI Controls
                BeginInvoke(new EventHandler(JpegLiveSource1LiveNotificationEvent), new[] { sender, e });
            }
            else
            {
                LiveContentEventArgs args = e as LiveContentEventArgs;
                if (args != null)
                {
                    if (args.LiveContent != null)
                    {
                        // Display the received JPEG
                        //textBoxLength.Text = "" + args.LiveContent.Content.Length;

                        int width  = args.LiveContent.Width;
                        int height = args.LiveContent.Height;

                        MemoryStream ms = new MemoryStream(args.LiveContent.Content);
                        //Bitmap newBitmap = testBox();
                        Bitmap newBitmap = new Bitmap(ms);

                        if (referenceBitmap == null)
                        {
                            referenceBitmap = newBitmap;
                        }

                        textBoxResolution.Text = "" + width + "x" + height;

                        if (pictureBoxOriginal.Size.Width != 0 && pictureBoxOriginal.Size.Height != 0)
                        {
                            if ((newBitmap.Width != pictureBoxOriginal.Width || newBitmap.Height != pictureBoxOriginal.Height))
                            {
                                pictureBoxOriginal.Image = new Bitmap(newBitmap, pictureBoxOriginal.Size);
                            }
                            else
                            {
                                pictureBoxOriginal.Image = newBitmap;
                            }
                        }

                        textBoxDecodingStatus.Text = args.LiveContent.HardwareDecodingStatus;



                        ms.Close();
                        ms.Dispose();

                        _count++;
                        textBoxCount.Text = "" + _count;

                        args.LiveContent.Dispose();



                        /// Star processing frame
                        grayImage            = gfilter.Apply(newBitmap);
                        pictureBoxGray.Image = grayImage;

                        try
                        {
                            if (counter == 4000)
                            {
                                counter    = 0;
                                background = null;
                                analyticsImageProcessing.resetBackground();
                            }

                            if (background == null)
                            {
                                background = analyticsImageProcessing.GetBackGound(grayImage);

                                /// Show mensaje
                                Bitmap   bitmap = new Bitmap(320, 240);
                                Graphics g      = Graphics.FromImage(bitmap);
                                g.FillRectangle(System.Drawing.Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                                g.DrawString("Processing Background...", new Font(FontFamily.GenericMonospace, 12), Brushes.White, new PointF(20, pictureBoxOriginal.Height / 2 - 20));
                                g.Dispose();
                                pictureBoxBackgound.Image = new Bitmap(bitmap, pictureBoxOriginal.Size);
                                bitmap.Dispose();
                            }
                            else
                            {
                                pictureBoxBackgound.Image = background;
                                background2 = gfilter.Apply(background);

                                Bitmap backgroundMask = analyticsImageProcessing.diff(grayImage, background2);
                                pictureBox1.Image = backgroundMask;


                                // create filter
                                //   Median filter = new Median();
                                // apply the filter
                                //   Bitmap backgroundMask2 = filter.Apply(backgroundMask);

                                //     pictureBox2.Image = backgroundMask2;

                                // create filter
                                Pixellate pxfilter = new Pixellate(20);
                                // apply the filter
                                Bitmap result = pxfilter.Apply(backgroundMask);
                                //      pictureBox3.Image = result;

                                // create filter
                                Threshold thfilter = new Threshold(1);
                                // apply the filter
                                Bitmap result2 = thfilter.Apply(result);

                                pictureBox4.Image = result2;

                                ApplyMask appmask = new ApplyMask(result2);
                                foreground = appmask.Apply(newBitmap);
                            }


                            /// PocessImage
                            if (foreground != null)
                            {
                                Blob[] blobs = analyticsImageProcessing.GetBlobs(foreground, blobCounter);
                                textBoxMetadata.Text = metadataHandler.SendMetadataBox(blobs, _jpegLiveSource.Width, _jpegLiveSource.Height);
                                PaintHeatMap(blobs);
                                pictureBoxHeatmap.Image = bitmapHeatMap;

                                /// Debug tool


                                if (blobs[0] != null && blobs.Length > 0)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[0], false);
                                    pictureBoxBlob1.Image = blobs[0].Image.ToManagedImage();
                                    textBoxAreaBlob1.Text = blobs[0].Area.ToString();
                                    textBoxXBlob1.Text    = blobs[0].CenterOfGravity.X.ToString();
                                    textBoxYBlob1.Text    = blobs[0].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob1.Image = null;
                                }

                                if (blobs[1] != null && blobs.Length > 1)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[1], false);
                                    pictureBoxBlob2.Image = blobs[1].Image.ToManagedImage();
                                    textBoxAreaBlob2.Text = blobs[1].Area.ToString();
                                    textBoxXBlob2.Text    = blobs[1].CenterOfGravity.X.ToString();
                                    textBoxYBlob2.Text    = blobs[1].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob2.Image = null;
                                }

                                if (blobs[2] != null && blobs.Length > 2)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[2], false);
                                    pictureBoxBlob3.Image = blobs[2].Image.ToManagedImage();
                                    textBoxAreaBlob3.Text = blobs[2].Area.ToString();
                                    textBoxXBlob3.Text    = blobs[2].CenterOfGravity.X.ToString();
                                    textBoxYBlob3.Text    = blobs[2].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob3.Image = null;
                                }

                                if (blobs[3] != null && blobs.Length > 3)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[3], false);
                                    pictureBoxBlob4.Image = blobs[3].Image.ToManagedImage();
                                    textBoxAreaBlob4.Text = blobs[3].Area.ToString();
                                    textBoxXBlob4.Text    = blobs[3].CenterOfGravity.X.ToString();
                                    textBoxYBlob4.Text    = blobs[3].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob4.Image = null;
                                }
                            }
                        }
                        catch (Exception r)
                        {
                            Console.WriteLine(r.Message);
                        }
                    }
                    else if (args.Exception != null)
                    {
                        // Handle any exceptions occurred inside toolkit or on the communication to the VMS

                        Bitmap   bitmap = new Bitmap(320, 240);
                        Graphics g      = Graphics.FromImage(bitmap);
                        g.FillRectangle(System.Drawing.Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                        g.DrawString("Connection lost to server ...", new Font(FontFamily.GenericMonospace, 12), Brushes.White, new PointF(20, pictureBoxOriginal.Height / 2 - 20));
                        g.Dispose();
                        pictureBoxOriginal.Image = new Bitmap(bitmap, pictureBoxOriginal.Size);
                        bitmap.Dispose();
                    }
                }
                OnMainThread = false;
            }
        }
        /// <summary>
        /// This event is called when JPEG is available or some exception has occurred
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void JpegLiveSource1LiveNotificationEvent2(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                // Make sure we execute on the UI thread before updating UI Controls
                BeginInvoke(new EventHandler(JpegLiveSource1LiveNotificationEvent2), new[] { sender, e });
            }
            else
            {
                LiveContentEventArgs args = e as LiveContentEventArgs;
                if (args != null)
                {
                    if (args.LiveContent != null)
                    {
                        // Display the received JPEG
                        //labelSize.Text = "" + args.LiveContent.Content.Length;

                        int width  = args.LiveContent.Width;
                        int height = args.LiveContent.Height;

                        MemoryStream ms        = new MemoryStream(args.LiveContent.Content);
                        Bitmap       newBitmap = new Bitmap(ms);
                        //labelResolution.Text = "" + width + "x" + height;
                        if (newBitmap.Width != pictureBoxUC2.Width || newBitmap.Height != pictureBoxUC2.Height)
                        {
                            pictureBoxUC2.Image = new Bitmap(newBitmap, pictureBoxUC2.Size);
                        }
                        else
                        {
                            pictureBoxUC2.Image = newBitmap;
                        }
                        ms.Close();
                        ms.Dispose();

                        //_count++;
                        //labelCount.Text = "" + _count;

                        args.LiveContent.Dispose();
                    }
                    else if (args.Exception != null)
                    {
                        // Handle any exceptions occurred inside toolkit or on the communication to the VMS
                        Bitmap   bitmap = new Bitmap(320, 240);
                        Graphics g      = Graphics.FromImage(bitmap);
                        g.FillRectangle(Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                        if (args.Exception is CommunicationMIPException)
                        {
                            g.DrawString("Connection lost to server ...", new Font(FontFamily.GenericMonospace, 12),
                                         Brushes.White, new PointF(20, pictureBoxUC2.Height / 2 - 20));
                        }
                        else
                        {
                            g.DrawString(args.Exception.Message, new Font(FontFamily.GenericMonospace, 12),
                                         Brushes.White, new PointF(20, pictureBoxUC2.Height / 2 - 20));
                        }
                        g.Dispose();
                        pictureBoxUC2.Image = new Bitmap(bitmap, pictureBoxUC2.Size);
                        bitmap.Dispose();
                    }
                }
            }
        }