コード例 #1
0
ファイル: FrameGrabber.cs プロジェクト: wyj64852999/Kinovea
        public void Start()
        {
            if (!imageProvider.IsOpen)
            {
                Open();
            }

            if (!deviceHandle.IsValid || !imageProvider.IsOpen)
            {
                return;
            }

            log.DebugFormat("Starting device {0}, {1}", summary.Alias, summary.Identifier);

            imageProvider.GrabErrorEvent       += imageProvider_GrabErrorEvent;
            imageProvider.GrabbingStartedEvent += imageProvider_GrabbingStartedEvent;
            imageProvider.DeviceRemovedEvent   += imageProvider_DeviceRemovedEvent;
            imageProvider.ImageReadyEvent      += imageProvider_ImageReadyEvent;

            try
            {
                imageProvider.ContinuousShot();
            }
            catch (Exception e)
            {
                LogError(e, imageProvider.GetLastErrorMessage());
            }
        }
コード例 #2
0
        public SnapshotRetriever(CameraSummary summary, uint deviceIndex)
        {
            this.summary = summary;

            imageProvider.GrabErrorEvent     += imageProvider_GrabErrorEvent;
            imageProvider.DeviceRemovedEvent += imageProvider_DeviceRemovedEvent;
            imageProvider.ImageReadyEvent    += imageProvider_ImageReadyEvent;

            try
            {
                imageProvider.Open(deviceIndex);
            }
            catch (Exception e)
            {
                LogError(e, imageProvider.GetLastErrorMessage());
            }
        }
コード例 #3
0
        /* Handles the event related to an image having been taken and waiting for processing. */
        private void OnImageReadyEventCallback()
        {
            if (InvokeRequired)
            {
                /* Suspend the grab thread for a while to avoid blocking the computer by using up all processor resources. */
                System.Threading.Thread.Sleep(20); /* This is only required for this sample. */
                /* If called from a different thread, we must use the Invoke method to marshal the call to the proper thread. */
                BeginInvoke(new ImageProvider.ImageReadyEventHandler(OnImageReadyEventCallback));
                return;
            }

            try
            {
                /* Acquire the image from the image provider. */
                ImageProvider.Image image = m_imageProvider.GetCurrentImage();

                /* Check if the image has been removed in the meantime. */
                if (image != null)
                {
                    /* Check if the image is compatible with the currently used bitmap. */
                    if (BitmapFactory.IsCompatible(m_bitmap, image.Width, image.Height, image.Color))
                    {
                        /* Update the bitmap with the image data. */
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        /* To show the new image, request the display control to update itself. */
                        pictureBox.Refresh();
                    }
                    else /* A new bitmap is required. */
                    {
                        BitmapFactory.CreateBitmap(ref m_bitmap, image.Width, image.Height, image.Color);
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        /* Provide the display control with the new bitmap. This action automatically updates the display. */
                        pictureBox.Image = m_bitmap;
                    }
                    if (is_recording == true)
                    {
                        RotateFlipType t = RotateFlipType.RotateNoneFlipY;
                        m_bitmap.RotateFlip(t);
                        aw.AddFrame(m_bitmap);
                    }

                    /* The processing of the image is done. Release the image buffer. */
                    m_imageProvider.ReleaseImage();

                    /* The buffer can be used for the next image grabs.
                     * If another image is in the output queue it can be acquired now using GetCurrentImage(). */
                }
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }
コード例 #4
0
ファイル: SnapshotRetriever.cs プロジェクト: weblate/Kinovea
        public SnapshotRetriever(CameraSummary summary, uint deviceIndex)
        {
            this.summary = summary;

            imageProvider.GrabErrorEvent     += imageProvider_GrabErrorEvent;
            imageProvider.DeviceRemovedEvent += imageProvider_DeviceRemovedEvent;
            imageProvider.ImageReadyEvent    += imageProvider_ImageReadyEvent;

            try
            {
                stopwatch.Start();
                imageProvider.Open(deviceIndex);
                log.DebugFormat("{0} opened in {1} ms.", summary.Alias, stopwatch.ElapsedMilliseconds);
                stopwatch.Stop();
            }
            catch (Exception e)
            {
                LogError(e, imageProvider.GetLastErrorMessage());
            }
        }
コード例 #5
0
ファイル: MainForm.cs プロジェクト: fanchaodaxia/WLCamera
        /* Handles the event related to an image having been taken and waiting for processing. */
        private void OnImageReadyEventCallback()
        {
            if (InvokeRequired)
            {
                /* If called from a different thread, we must use the Invoke method to marshal the call to the proper thread. */
                BeginInvoke(new ImageProvider.ImageReadyEventHandler(OnImageReadyEventCallback));
                return;
            }

            try
            {
                /* Acquire the image from the image provider. Only show the latest image. The camera may acquire images faster than images can be displayed*/
                ImageProvider.Image image = m_imageProvider.GetLatestImage();

                /* Check if the image has been removed in the meantime. */
                if (image != null)
                {
                    /* Check if the image is compatible with the currently used bitmap. */
                    if (BitmapFactory.IsCompatible(m_bitmap, image.Width, image.Height, image.Color))
                    {
                        /* Update the bitmap with the image data. */
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        /* To show the new image, request the display control to update itself. */
                        pictureBox.Refresh();
                    }
                    else /* A new bitmap is required. */
                    {
                        BitmapFactory.CreateBitmap(out m_bitmap, image.Width, image.Height, image.Color);
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        /* We have to dispose the bitmap after assigning the new one to the display control. */
                        Bitmap bitmap = pictureBox.Image as Bitmap;
                        /* Provide the display control with the new bitmap. This action automatically updates the display. */
                        pictureBox.Image = m_bitmap;
                        if (bitmap != null)
                        {
                            /* Dispose the bitmap. */
                            bitmap.Dispose();
                        }
                    }
                    /* The processing of the image is done. Release the image buffer. */
                    m_imageProvider.ReleaseImage();
                    /* The buffer can be used for the next image grabs. */
                }
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }
コード例 #6
0
        /// <summary>
        /// 影像擷取完成通知
        /// </summary>
        private void OnImageReadyEventCallback()
        {
            try
            {
                /* Acquire the image from the image provider. Only show the latest image. The camera may acquire images faster than images can be displayed*/
                ImageProvider.Image image = m_imageProvider.GetLatestImage();

                /* Check if the image has been removed in the meantime. */
                if (image != null)
                {
                    //利用 GC Handle , 釘住記憶體不讓釋放,取得該記憶體位址
                    GCHandle pinnedArray = GCHandle.Alloc(image.Buffer, GCHandleType.Pinned);
                    IntPtr   pointer     = pinnedArray.AddrOfPinnedObject();
                    try
                    {
                        //'byte', 'complex', 'cyclic', 'direction', 'int1', 'int2', 'int4', 'int8', 'real', 'uint2'
                        var    imgType = "byte";
                        HImage img     = new HImage(imgType, image.Width, image.Height, pointer);

                        if (GrabImageChanged != null)
                        {
                            GrabImageChanged(m_imageProvider
                                             , new System.ComponentModel.ProgressChangedEventArgs(1, img));
                        }
                    }
                    catch (Exception ex)
                    {
                        errorNotify(ex);
                    }
                    finally
                    {
                        //用完要釋放,否則該記憶體不會被回收
                        pinnedArray.Free();
                    }

                    /* The processing of the image is done. Release the image buffer. */
                    m_imageProvider.ReleaseImage();
                    /* The buffer can be used for the next image grabs. */
                }
            }
            catch (Exception e)
            {
                OnGrabErrorEventCallback(e, m_imageProvider.GetLastErrorMessage());
            }
        }