Ejemplo n.º 1
0
        public void StartCapture()
        {
            //Setup specific Device
            try
            {
                //lets be cautious with VIW
                //Check to see if ID is valid first.
                int num = VIWrapper.ListDevices();
                if (deviceId < num)
                {
                    VIWrapper.SetRequestedMediaSubType(mediaSubtype);
                    VIWrapper.SetFormat(deviceId, formatType);
                    VIWrapper.SetupDevice1(deviceId, physicalType);
                    VIWrapper.SetIdealFramerate(deviceId, idealFrameRate);

                    //Start the capture thread
                    captureThread              = new Thread(new ParameterizedThreadStart(_capThread));
                    captureThread.Name         = "CaptureThread";
                    captureThread.IsBackground = true;
                    isCaptureRunning           = true;
                    captureThread.Start(deviceId);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occured while trying to start video capture. \n" + e);
            }
            //Turn on capture thread
        }
 public void RefreshList()
 {
     DeviceList.Clear();
     numDevices = VIWrapper.ListDevices();
     for (int i = 0; i < numDevices; i++)
     {
         DeviceList.Add(VIWrapper.GetDeviceName(i));
     }
 }
Ejemplo n.º 3
0
 public void RefreshList()
 {
     DeviceList.Clear();
     numDevices = VIWrapper.ListDevices();
     for (int i = 0; i < numDevices; i++)
     {
         DeviceList.Add(VIWrapper.GetDeviceName(i));
     }
     OnPropertyChanged("DeviceID");
     OnPropertyChanged("DeviceList");
 }
 /// <summary>
 /// Starts capturing the selected device.
 /// </summary>
 public void StartCapture()
 {
     Console.WriteLine("Starting Capture");
     if (capThread != null)
     {
         capThread.Abort();
     }
     VIWrapper.SetupDevice(selectedDeviceIndex);
     keepGoing = true;
     capThread = new Thread(new ParameterizedThreadStart(_captureThread));
     capThread.Start(selectedDeviceIndex);
 }
Ejemplo n.º 5
0
 public void StopCapture()
 {
     try
     {
         LatencyTesterVM.Instance.KillPreview();
         isCaptureRunning = false;
         while (captureThread.IsAlive)     //let the thread go peacefully.
         {
             Thread.Sleep(5);
         }
         Console.WriteLine("Shutting Down");
         VIWrapper.StopDevice(DeviceID);
     }
     catch (Exception)
     { }
 }
        private void _captureThread(object devIdf)
        {
            int devId = (int)devIdf;

            while (keepGoing)
            {
                FrameRate = CalculateFrameRate();

                Thread.Sleep(1);
                if (VIWrapper.IsFrameReady(selectedDeviceIndex))
                {
                    //
                    //    writeLock = true;

                    output = IntPtr.Zero;
                    VIWrapper.GetImage(devId, ref output, false, true);
                    writeLock = false;
                    int width  = VIWrapper.GetWidth(devId);
                    int height = VIWrapper.GetHeight(devId);
                    try
                    {
                        Application.Current.Dispatcher.BeginInvoke(
                            DispatcherPriority.Background,
                            new Action(() =>
                        {
                            CapturedImage = FromNativePointer(output, width, height, 3);
                        }));
                        if (this.runTest)
                        {
                            Stopwatch sw = new Stopwatch();
                            sw.Start();
                            if (CheckIfRed(this.redThreshold, array, width, height, 3))
                            {
                                redDetected.Stop();
                                EndLatencyTest();
                            }
                            sw.Stop();
                            //          Console.WriteLine("It took " + sw.ElapsedTicks + " to run or" + sw.ElapsedMilliseconds + " Ms");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        private void _capThread(object deviceID)
        {
            int devId = (int)deviceID;

            while (isCaptureRunning)
            {
                UpdateFrameRate();        //update the framerate.
                Thread.Sleep(1);
                if (isCaptureRunning && VIWrapper.IsFrameReady(devId))
                {
                    unmanagedImagePtr = IntPtr.Zero; // Clean the pointer!
                    try                              //Enter exception safetyland!
                    {
                        VIWrapper.GetImage(devId, ref unmanagedImagePtr, false, true);

                        //Update Width and Height - Probably not necessary after capture starts. Move to StartCapture!
                        ImageWidth  = VIWrapper.GetWidth(devId);
                        ImageHeight = VIWrapper.GetHeight(devId);

                        int bytesPerPixel = 3;     //NOTE:Assumption of RGB24 starts here!

                        lock (bufferGuard)         /* Enter critical section to copy data into buffer */
                        {
                            if (unmanagedImagePtr != IntPtr.Zero)
                            {
                                unsafe
                                {
                                    Marshal.Copy(unmanagedImagePtr, buffer1, 0, (imageWidth * imageHeight * bytesPerPixel));
                                }
                            }
                        }

                        //      Marshal.FreeHGlobal(unmanagedImagePtr); //Free the data since it should be managed.
                        GC.Collect();         //Enforced GC because 60frames passes a TON of data

                        //Now, call back registered members!
                        //Maybe Lock each call?? If i get crashes, try that.
                        foreach (Func <byte[], int> function in _callbackMethods)
                        {
                            Application.Current.Dispatcher.BeginInvoke(
                                DispatcherPriority.Loaded,
                                new Action(() =>
                            {
                                try
                                {
                                    function(buffer1);
                                }
                                catch (Exception e)
                                { }
                            }));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("CAPTURE FAILURE please report to dev!. Exception -->" + e);
                        Console.WriteLine("Exception Source: " + e.Source);
                        Console.WriteLine("Stack Trace:" + e.StackTrace);
                    }
                }
            }
        }