static void DestroysObjects(SapAcquisition acq, SapAcqDevice camera, SapBuffer buf, SapTransfer xfer, SapView view)
        {
            if (xfer != null)
            {
                xfer.Destroy();
                xfer.Dispose();
            }

            if (camera != null)
            {
                camera.Destroy();
                camera.Dispose();
            }

            if (acq != null)
            {
                acq.Destroy();
                acq.Dispose();
            }

            if (buf != null)
            {
                buf.Destroy();
                buf.Dispose();
            }

            if (view != null)
            {
                view.Destroy();
                view.Dispose();
            }

            //Console.WriteLine("\nPress any key to terminate\n");
            //Console.ReadKey(true);
        }
Beispiel #2
0
        static void DestroysObjects(SapAcqDevice acq, SapBuffer buf, SapTransfer xfer, SapView view)
        {
            if (xfer != null && xfer.Initialized)
            {
                xfer.Destroy();
                xfer.Dispose();
            }

            if (acq != null && acq.Initialized)
            {
                acq.Destroy();
                acq.Dispose();
            }

            if (buf != null && buf.Initialized)
            {
                buf.Destroy();
                buf.Dispose();
            }

            if (view != null && view.Initialized)
            {
                view.Destroy();
                view.Dispose();
            }
        }
 public void CreateTranfer(SapTransfer xfer, SapAcqDevice acqDevice, SapBuffer buf)
 {
     if (acqDevice == null || buf == null)
     {
     }
     xfer = new SapAcqDeviceToBuf(acqDevice, buf);
 }
 public void CreateAcqDevice(SapAcqDevice camera, SapLocation location, string configFileName)
 {
     if (location == null || configFileName == null)
     {
     }
     camera = new SapAcqDevice(location, configFileName);
 }
        static void DestroysObjects(SapAcquisition acq, SapAcqDevice camera, SapBuffer buf, SapTransfer xfer, SapView view)
        {
            if (xfer != null)
            {
                xfer.Destroy();
                xfer.Dispose();
            }

            if (camera != null)
            {
                camera.Destroy();
                camera.Dispose();
            }

            if (acq != null)
            {
                acq.Destroy();
                acq.Dispose();
            }

            if (buf != null)
            {
                buf.Destroy();
                buf.Dispose();
            }

            if (view != null)
            {
                view.Destroy();
                view.Dispose();
            }

            //Console.WriteLine("\nPress any key to terminate\n");
            //Console.ReadKey(true);
        }
Beispiel #6
0
        public bool init()     //初始化相机
        {
            try
            {
                m_ServerLocation = new SapLocation(m_ServerName, 0);
                // m_Acquisition = new SapAcquisition(m_ServerLocation, filename);
                m_AcqDevice = new SapAcqDevice(m_ServerLocation, filename);
                m_Buffers   = new SapBufferWithTrash(2, m_AcqDevice, SapBuffer.MemoryType.ScatterGather);
                m_Xfer      = new SapAcqDeviceToBuf(m_AcqDevice, m_Buffers);

                // m_Xfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
                m_Xfer.XferNotify += m_Xfer_XferNotify;

                if (!CreateObjects())
                {
                    DisposeObjects();
                    return(false);
                }
            }
            catch (Exception ex)
            {
                // MessageBox.Show(ex.ToString());
            }
            return(true);
        }
        public bool CAM_connect()
        {
            try
            {
                //server名称
                int    ServerCount = SapManager.GetServerCount();
                int    ServerIndex = ServerCount - 1;
                string ServerName  = SapManager.GetServerName(ServerIndex);
                //设备Index
                int CameraCount   = SapManager.GetResourceCount(ServerName, SapManager.ResourceType.AcqDevice);
                int ResourceIndex = CameraCount - 1;
                //if (CameraCount == 0) MessageBox.Show("没有相机。");

                loc = new SapLocation(ServerName, ResourceIndex);

                //配置文件
                string   ConfigFileName;
                string   ConfigPath      = Environment.GetEnvironmentVariable("SAPERADIR") + "\\camFiles\\User\\";
                string[] ccffiles        = Directory.GetFiles(ConfigPath, "*.ccf");
                int      configFileCount = ccffiles.Length;
                ConfigFileName = ccffiles[0];
                //创建对象
                if (SapManager.GetResourceCount(ServerName, SapManager.ResourceType.AcqDevice) > 0)
                {
                    AcqDevice = new SapAcqDevice(loc, ConfigFileName);
                    Buffers   = new SapBuffer(1, AcqDevice, SapBuffer.MemoryType.ScatterGather);
                    Xfer      = new SapAcqDeviceToBuf(AcqDevice, Buffers);
                    if (!AcqDevice.Create())
                    {
                        return(false);
                    }
                }

                if (!Buffers.Create())
                {
                    return(false);
                }
                if (!Xfer.Create())
                {
                    return(false);
                }


                ////ManagedBusManager busMgr = new ManagedBusManager();
                ////uint numCameras = busMgr.GetNumOfCameras();
                ////ManagedPGRGuid guid = busMgr.GetCameraFromIndex(0);
                ////m_camera = new ManagedCamera();
                ////m_camera.Connect(guid);
                //m_camera.IsConnected()
                //CameraInfo camInfo = m_camera.GetCameraInfo();
            }
            catch (FC2Exception ex)//要修改
            {
                MessageBox.Show("相机连接失败。");
                MessageBox.Show("Failed to load form successfully: " + ex.Message);
                return(false);
            }
            return(true);
        }
Beispiel #8
0
        public AcquisitionDevice()
        {
            source = Observable.Create <IplImage>((observer, cancellationToken) =>
            {
                return(Task.Factory.StartNew(() =>
                {
                    lock (captureLock)
                    {
                        using (var location = new SapLocation(ServerName, DeviceIndex))
                            using (var device = new SapAcqDevice(location, ConfigFileName))
                                using (var buffer = new SapBufferWithTrash(2, device, SapBuffer.MemoryType.ScatterGather))
                                    using (var transfer = new SapAcqDeviceToBuf(device, buffer))
                                        using (var waitEvent = new AutoResetEvent(false))
                                        {
                                            if (!device.Create())
                                            {
                                                throw new InvalidOperationException("Error opening device.");
                                            }

                                            transfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
                                            transfer.XferNotify += (sender, e) =>
                                            {
                                                int channels;
                                                IplDepth depth;
                                                SapFormat outputFormat;
                                                GetImageDepth(buffer.Format, out depth, out channels, out outputFormat);
                                                var image = new IplImage(new Size(buffer.Width, buffer.Height), depth, channels);
                                                buffer.Write(0, buffer.Width * buffer.Height, image.ImageData);
                                                observer.OnNext(image);
                                                waitEvent.Set();
                                            };

                                            if (!buffer.Create())
                                            {
                                                throw new InvalidOperationException("Error creating buffer.");
                                            }

                                            if (!transfer.Create())
                                            {
                                                throw new InvalidOperationException("Error creating transfer.");
                                            }

                                            while (!cancellationToken.IsCancellationRequested)
                                            {
                                                waitEvent.WaitOne();
                                            }
                                        }
                    }
                },
                                             cancellationToken,
                                             TaskCreationOptions.LongRunning,
                                             TaskScheduler.Default));
            })
                     .PublishReconnectable()
                     .RefCount();
        }
        public void InitDevice(MyAcquisitionParams acqParams = null)
        {
            _acqParams = acqParams;

            if (acqParams == null)
            {
                string[] args = new string[0];
                if (!ExampleUtils.GetOptions(args, acqParams))
                {
                    Console.WriteLine("\nPress any key to terminate\n");
                    Console.ReadKey(true);
                    return;
                }
            }



            _loc = new SapLocation(acqParams.ServerName, acqParams.ResourceIndex);
            if (SapManager.GetResourceCount(acqParams.ServerName, SapManager.ResourceType.AcqDevice) > 0)
            {
                _acqDevice = new SapAcqDevice(_loc, acqParams.ConfigFileName);
                _buffers   = new SapBufferWithTrash(2, _acqDevice, SapBuffer.MemoryType.ScatterGather);
                _transfer  = new SapAcqDeviceToBuf(_acqDevice, _buffers);

                // Create acquisition object
                if (!_acqDevice.Create())
                {
                    Console.WriteLine("Error during SapAcqDevice creation!\n");
                    DestroysObjects(null, _acqDevice, _buffers, _transfer, _view);
                    return;
                }
            }

            _transfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
            _transfer.XferNotify        += new SapXferNotifyHandler(xfer_XferNotify);
            _transfer.XferNotifyContext  = _buffers;

            // Create buffer object
            if (!_buffers.Create())
            {
                Console.WriteLine("Error during SapBuffer creation!\n");
                DestroysObjects(null, _acqDevice, _buffers, _transfer, _view);
                return;
            }

            // Create buffer object
            if (!_transfer.Create())
            {
                Console.WriteLine("Error during SapTransfer creation!\n");
                DestroysObjects(null, _acqDevice, _buffers, _transfer, _view);
                return;
            }

            _transfer.Grab();
        }
Beispiel #10
0
 private void DisposeObjects()
 {
     if (m_Xfer != null)
     {
         m_Xfer.Dispose(); m_Xfer = null;
     }
     if (m_Buffers != null)
     {
         m_Buffers.Dispose(); m_Buffers = null;
     }
     if (m_AcqDevice != null)
     {
         m_AcqDevice.Dispose(); m_AcqDevice = null;
     }
 }
Beispiel #11
0
 /// <summary>
 /// 释放句柄
 /// </summary>
 private void DisposeObjects()
 {
     if (m_Xfer != null)
     {
         m_Xfer.Dispose(); m_Xfer = null;
     }
     if (m_View != null)
     {
         m_View.Dispose(); m_View = null; m_ImageBox.View = null;
     }
     if (m_Buffers != null)
     {
         m_Buffers.Dispose(); m_Buffers = null;
     }
     if (m_AcqDevice != null)
     {
         m_AcqDevice.Dispose(); m_AcqDevice = null;
     }
 }
Beispiel #12
0
        static bool ComputeGain(String Name, SapAcqDevice Camera, SapFeature featureInfo, double coefGain)
        {
            int    featureExponent, gainCameraValue, gainGreenMin, gainGreenMax;
            float  powValue;
            double NewGainValuePrecision;
            int    NewGainRounded;

            if (!Camera.GetFeatureInfo(Name, featureInfo))
            {
                return(false);
            }

            // Get Gain Green minimum
            featureInfo.GetValueMin(out gainGreenMin);
            // Get Gain Green maximum
            featureInfo.GetValueMax(out gainGreenMax);
            // Get Gain Value
            Camera.GetFeatureValue(Name, out gainCameraValue);
            // Get Gain exponent
            featureExponent = featureInfo.SiToNativeExp10;
            powValue        = (float)Math.Pow(10, featureExponent);

            if (gainCameraValue == 0)
            {
                NewGainValuePrecision = (coefGain * powValue);
            }
            else
            {
                NewGainValuePrecision = (coefGain * gainCameraValue);
            }

            NewGainRounded = (int)Math.Round(NewGainValuePrecision);

            if (!ValidateWhiteBalance(NewGainRounded, gainGreenMin, gainGreenMax))
            {
                return(false);
            }

            Camera.SetFeatureValue(Name, NewGainRounded);
            return(true);
        }
Beispiel #13
0
 public Boolean Create()
 {
     try
     {
         Params      = new MyAcquisitionParams();
         Location    = new SapLocation(Params.ServerName, Params.ResourceIndex);
         Device      = new SapAcqDevice(Location, Params.ConfigFileName);
         Buffers     = new SapBufferWithTrash(DefaultBuffer, Device, SapBuffer.MemoryType.ScatterGather);
         Acquisition = new SapAcquisition(Location, Params.ConfigFileName);
         Transfer    = new SapAcqToBuf(Acquisition, Buffers);
     }
     catch (SapException CreateException)
     {
         throw new Exception("CreateCamera failed!", CreateException);
     }
     finally
     {
         Console.WriteLine("Camera Created!");
     }
     return(true);
 }
Beispiel #14
0
        /// <summary>
        /// 初始化设备
        /// </summary>
        /// <param name="acConfigDlg"></param>
        /// <param name="Restore"></param>
        /// <returns></returns>
        public bool CreateNewObjects(AcqConfigDlg acConfigDlg, bool Restore)
        {
            if (!Restore)
            {
                m_ServerLocation = acConfigDlg.ServerLocation;
                m_ConfigFileName = acConfigDlg.ConfigFile;
            }
            m_AcqDevice = new SapAcqDevice(m_ServerLocation, m_ConfigFileName);
            if (SapBuffer.IsBufferTypeSupported(m_ServerLocation, SapBuffer.MemoryType.ScatterGather))
            {
                m_Buffers = new SapBufferWithTrash(2, m_AcqDevice, SapBuffer.MemoryType.ScatterGather);
            }
            else
            {
                m_Buffers = new SapBufferWithTrash(2, m_AcqDevice, SapBuffer.MemoryType.ScatterGatherPhysical);
            }
            m_Xfer          = new SapAcqDeviceToBuf(m_AcqDevice, m_Buffers);
            m_View          = new SapView(m_Buffers);
            m_ImageBox.View = m_View;

            m_Xfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
            m_Xfer.XferNotify        += new SapXferNotifyHandler(xfer_XferNotify);
            m_Xfer.XferNotifyContext  = this;
            StatusLabelInfo.Text      = "Online... Waiting grabbed images";

            if (!CreateObjects())
            {
                DisposeObjects();
                return(false);
            }

            // Resize ImagBox to take into account the size of created sapview
            m_ImageBox.OnSize();
            UpdateControls();
            return(true);
        }
Beispiel #15
0
        private void btn_findcamera_Click(object sender, EventArgs e)
        {
            Console.WriteLine(cb_cameraselect.SelectedIndex);
            switch (cb_cameraselect.SelectedIndex)
            {
            case 0:
                Console.WriteLine("\n\nCameras listed by User Defined Name:\n");

                for (int serverIndex = 0; serverIndex < serverCount; serverIndex++)
                {
                    if (SapManager.GetResourceCount(serverIndex, SapManager.ResourceType.AcqDevice) != 0)
                    {
                        SapLocation location = new SapLocation(SapManager.GetServerName(serverIndex), 0);
                        acqDevice = new SapAcqDevice(location);

                        // Create acquisition device object
                        bool status = acqDevice.Create();
                        if (status && acqDevice.FeatureCount > 0)
                        {
                            // Get User Defined Name Feature Value
                            status = acqDevice.GetFeatureValue("DeviceUserID", new SapLut(tx_commandparameter.Text.ToString()));
                            Console.WriteLine("{0}/ {1}", cameraIndex + 1, status ? userDefinedName : "N/A");
                            cameraIndex++;
                        }

                        // Destroy acquisition device object
                        acqDevice.Destroy();
                    }
                }
                if (cameraIndex == 0)
                {
                    Console.WriteLine("No camera found !");
                }
                if (acqDevice != null)
                {
                    acqDevice.Dispose();
                }
                break;

            case 1:
                Console.WriteLine("\n\nCameras listed by Serial Number:\n");
                string serialNumberName = "";

                for (int serverIndex = 0; serverIndex < serverCount; serverIndex++)
                {
                    if (SapManager.GetResourceCount(serverIndex, SapManager.ResourceType.AcqDevice) != 0)
                    {
                        SapLocation location = new SapLocation(SapManager.GetServerName(serverIndex), 0);
                        acqDevice = new SapAcqDevice(location);

                        // Create acquisition device object
                        bool status = acqDevice.Create();
                        if (status && acqDevice.FeatureCount > 0)
                        {
                            // Get Serial Number Feature Value
                            status = acqDevice.GetFeatureValue("DeviceID", out serialNumberName);
                            Console.WriteLine("{0}/ {1}", cameraIndex + 1, status ? serialNumberName : "N/A");
                            cameraIndex++;
                        }

                        // Destroy acquisition device object
                        acqDevice.Destroy();
                    }
                }
                if (cameraIndex == 0)
                {
                    Console.WriteLine("No camera found !");
                }
                if (acqDevice != null)
                {
                    acqDevice.Dispose();
                }
                break;

            case 2:
                Console.WriteLine("\n\nCameras listed by Server Name:\n");

                for (int serverIndex = 0; serverIndex < serverCount; serverIndex++)
                {
                    if (SapManager.GetResourceCount(serverIndex, SapManager.ResourceType.AcqDevice) != 0)
                    {
                        SapLocation location = new SapLocation(SapManager.GetServerName(serverIndex), 0);
                        acqDevice = new SapAcqDevice(location);

                        // Create acquisition device object
                        bool status = acqDevice.Create();
                        if (status && acqDevice.FeatureCount > 0)
                        {
                            // Get Server Name Value
                            Console.WriteLine("{0}/ {1}", cameraIndex + 1, SapManager.GetServerName(serverIndex));
                            cameraIndex++;
                        }

                        // Destroy acquisition device object
                        acqDevice.Destroy();
                    }
                }
                if (cameraIndex == 0)
                {
                    Console.WriteLine("No camera found !");
                }
                if (acqDevice != null)
                {
                    acqDevice.Dispose();
                }
                break;

            case 3:
                Console.WriteLine("\n\nCameras listed by Model Name:\n");
                string deviceModelName = "";

                for (int serverIndex = 0; serverIndex < serverCount; serverIndex++)
                {
                    if (SapManager.GetResourceCount(serverIndex, SapManager.ResourceType.AcqDevice) != 0)
                    {
                        SapLocation location = new SapLocation(SapManager.GetServerName(serverIndex), 0);
                        acqDevice = new SapAcqDevice(location);

                        // Create acquisition device object
                        bool status = acqDevice.Create();
                        if (status && acqDevice.FeatureCount > 0)
                        {
                            // Get Model Name Feature Value
                            status = acqDevice.GetFeatureValue("DeviceModelName", out deviceModelName);
                            Console.WriteLine("{0}/ {1}", cameraIndex + 1, status ? deviceModelName : "N/A");
                            cameraIndex++;
                        }

                        // Destroy acquisition device object
                        acqDevice.Destroy();
                    }
                }
                if (cameraIndex == 0)
                {
                    Console.WriteLine("No camera found !");
                }
                if (acqDevice != null)
                {
                    acqDevice.Dispose();
                }
                break;

            case 4:
                Console.WriteLine("Please type the user defined name:\n");
                userDefinedName = Console.ReadLine();
                serverName      = SapManager.GetServerName(userDefinedName);
                if (serverName.Length > 0)
                {
                    Console.WriteLine("\nServer name for {0} is {1}", userDefinedName, serverName);
                }
                else
                {
                    Console.WriteLine("\nNo server found for {0}", userDefinedName);
                }
                break;

            case 5:
                Console.Write("\nDetecting new CameraLink camera servers... ");
                if (SapManager.DetectAllServers(SapManager.DetectServerType.All))
                {
                    // let time for the detection to execute
                    Thread.Sleep(5000);

                    // get the new server count
                    serverCount = SapManager.GetServerCount();

                    Console.WriteLine("complete\n");
                }
                else
                {
                    Console.WriteLine("failed\n");
                }
                break;
            }
        }
Beispiel #16
0
        static void Main(string[] args)
        {
            SapAcquisition Acq       = null;
            SapAcqDevice   AcqDevice = null;
            SapBuffer      Buffers   = null;
            SapTransfer    Xfer      = null;
            SapView        View      = null;

            Console.WriteLine("Sapera Console Grab Example (C# version)\n");

            MyAcquisitionParams acqParams = new MyAcquisitionParams();

            // Call GetOptions to determine which acquisition device to use and which config
            // file (CCF) should be loaded to configure it.
            if (!GetOptions(args, acqParams))
            {
                Console.WriteLine("\nPress any key to terminate\n");
                Console.ReadKey(true);
                return;
            }

            SapLocation loc = new SapLocation(acqParams.ServerName, acqParams.ResourceIndex);

            if (SapManager.GetResourceCount(acqParams.ServerName, SapManager.ResourceType.Acq) > 0)
            {
                Acq     = new SapAcquisition(loc, acqParams.ConfigFileName);
                Buffers = new SapBufferWithTrash(2, Acq, SapBuffer.MemoryType.ScatterGather);
                Xfer    = new SapAcqToBuf(Acq, Buffers);

                // Create acquisition object
                if (!Acq.Create())
                {
                    Console.WriteLine("Error during SapAcquisition creation!\n");
                    DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                    return;
                }
                Acq.EnableEvent(SapAcquisition.AcqEventType.StartOfFrame);
            }

            else if (SapManager.GetResourceCount(acqParams.ServerName, SapManager.ResourceType.AcqDevice) > 0)
            {
                AcqDevice = new SapAcqDevice(loc, acqParams.ConfigFileName);
                Buffers   = new SapBufferWithTrash(2, AcqDevice, SapBuffer.MemoryType.ScatterGather);
                Xfer      = new SapAcqDeviceToBuf(AcqDevice, Buffers);

                // Create acquisition object
                if (!AcqDevice.Create())
                {
                    Console.WriteLine("Error during SapAcqDevice creation!\n");
                    DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                    return;
                }
            }

            View = new SapView(Buffers);

            // End of frame event
            Xfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
            Xfer.XferNotify        += new SapXferNotifyHandler(xfer_XferNotify);
            Xfer.XferNotifyContext  = View;

            // Create buffer object
            if (!Buffers.Create())
            {
                Console.WriteLine("Error during SapBuffer creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }

            // Create buffer object
            if (!Xfer.Create())
            {
                Console.WriteLine("Error during SapTransfer creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }

            // Create buffer object
            if (!View.Create())
            {
                Console.WriteLine("Error during SapView creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }


            Xfer.Grab();
            Console.WriteLine("\n\nGrab started, press a key to freeze");
            Console.ReadKey(true);
            Xfer.Freeze();
            Xfer.Wait(1000);

            DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
            loc.Dispose();
        }
        static void Main(string[] args)
        {
            SapAcquisition Acq       = null;
            SapAcqDevice   AcqDevice = null;
            SapBuffer      Buffers   = null;
            SapTransfer    Xfer      = null;
            SapView        View      = null;

            //Console.WriteLine("Sapera Console Grab Example (C# version)\n");

            MyAcquisitionParams acqParams = new MyAcquisitionParams();

            // Call GetOptions to determine which acquisition device to use and which config
            // file (CCF) should be loaded to configure it.
            if (!GetOptions(args, acqParams))
            {
                Console.WriteLine("\nPress any key to terminate\n");
                Console.ReadKey(true);
                return;
            }

            SapLocation loc = new SapLocation(acqParams.ServerName, acqParams.ResourceIndex);

            if (SapManager.GetResourceCount(acqParams.ServerName, SapManager.ResourceType.Acq) > 0)
            {
                Acq     = new SapAcquisition(loc, acqParams.ConfigFileName);
                Buffers = new SapBuffer(1, Acq, SapBuffer.MemoryType.ScatterGather);
                Xfer    = new SapAcqToBuf(Acq, Buffers);

                // Create acquisition object
                if (!Acq.Create())
                {
                    Console.WriteLine("Error during SapAcquisition creation!\n");
                    DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                    return;
                }
            }

            if (SapManager.GetResourceCount(acqParams.ServerName, SapManager.ResourceType.AcqDevice) > 0)
            {
                AcqDevice = new SapAcqDevice(loc, acqParams.ConfigFileName);
                Buffers   = new SapBuffer(1, AcqDevice, SapBuffer.MemoryType.ScatterGather);
                Xfer      = new SapAcqDeviceToBuf(AcqDevice, Buffers);

                // Create acquisition object
                if (!AcqDevice.Create())
                {
                    Console.WriteLine("Error during SapAcqDevice creation!\n");
                    DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                    return;
                }
            }

            View = new SapView(Buffers);
            // End of frame event
            Xfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
            Xfer.XferNotify        += new SapXferNotifyHandler(xfer_XferNotify);
            Xfer.XferNotifyContext  = View;

            //Console.WriteLine("gggggwhat is new line");
            // Create buffer object
            if (!Buffers.Create())
            {
                Console.WriteLine("Error during SapBuffer creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }

            // Create buffer object
            if (!Xfer.Create())
            {
                Console.WriteLine("Error during SapTransfer creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }

            // Create buffer object
            if (!View.Create())
            {
                Console.WriteLine("Error during SapView creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }

            // Grab as fast as possible, wait for a key to be pressed, if it's p,
            // write the file, otherwise if it's q exit the program. Designed to be run by and monitored
            // through a pipe

            Boolean stop_snap = false;
            int     curr_code = 0;
            string  new_cmd   = "";

            while (stop_snap == false)
            {
                Console.WriteLine("Press a key to trigger snap");
                new_cmd = Console.ReadLine();
                Xfer.Snap();

                Console.WriteLine("Press a key to trigger save");
                new_cmd = Console.ReadLine();
                View.Buffer.Save("test.raw", "-format raw");

                var dsb = new StringBuilder("frame: " + frame_count);
                Console.WriteLine(dsb);
                frame_count = frame_count + 1;


                Console.WriteLine("File saved, Press a key to repeat, q to quit:");
                new_cmd = Console.ReadLine();
                if (new_cmd == "q")
                {
                    stop_snap = true;
                }
                //if (curr_code == 113) { stop_snap = true; }
            }

            DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
            loc.Dispose();
        }
        static void Main(string[] args)
        {
            SapAcquisition Acq = null;
            SapAcqDevice AcqDevice = null;
            SapBuffer Buffers = null;
            SapTransfer Xfer = null;
            SapView View = null;

            //Console.WriteLine("Sapera Console Grab Example (C# version)\n");

            MyAcquisitionParams acqParams = new MyAcquisitionParams();

            // Call GetOptions to determine which acquisition device to use and which config
            // file (CCF) should be loaded to configure it.
            if (!GetOptions(args, acqParams))
            {
                Console.WriteLine("\nPress any key to terminate\n");
                Console.ReadKey(true);
                return;
            }

            SapLocation loc = new SapLocation(acqParams.ServerName, acqParams.ResourceIndex);

            if (SapManager.GetResourceCount(acqParams.ServerName, SapManager.ResourceType.Acq) > 0)
            {
                Acq         = new SapAcquisition(loc, acqParams.ConfigFileName);
                Buffers     = new SapBuffer(1, Acq, SapBuffer.MemoryType.ScatterGather);
                Xfer = new SapAcqToBuf(Acq, Buffers);

               // Create acquisition object
                if (!Acq.Create())
                {
                    Console.WriteLine("Error during SapAcquisition creation!\n");
                    DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                    return;
                }
            }

            if (SapManager.GetResourceCount(acqParams.ServerName, SapManager.ResourceType.AcqDevice) > 0)
            {
                AcqDevice   = new SapAcqDevice(loc, acqParams.ConfigFileName);
                Buffers = new SapBuffer(1, AcqDevice, SapBuffer.MemoryType.ScatterGather);
                Xfer = new SapAcqDeviceToBuf(AcqDevice, Buffers);

               // Create acquisition object
                if (!AcqDevice.Create())
                {
                    Console.WriteLine("Error during SapAcqDevice creation!\n");
                    DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                    return;
                }
            }

            View = new SapView(Buffers);
            // End of frame event
            Xfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
            Xfer.XferNotify += new SapXferNotifyHandler(xfer_XferNotify);
            Xfer.XferNotifyContext = View;

            //Console.WriteLine("gggggwhat is new line");
            // Create buffer object
            if (!Buffers.Create())
            {
                Console.WriteLine("Error during SapBuffer creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }

            // Create buffer object
            if (!Xfer.Create())
            {
                Console.WriteLine("Error during SapTransfer creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }

            // Create buffer object
            if (!View.Create())
            {
                Console.WriteLine("Error during SapView creation!\n");
                DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
                return;
            }

            // Grab as fast as possible, wait for a key to be pressed, if it's p,
            // write the file, otherwise if it's q exit the program. Designed to be run by and monitored
            // through a pipe

            Boolean stop_snap = false;
            int curr_code = 0;
            string new_cmd = "";
            while (stop_snap == false)
            {
                Console.WriteLine("Press a key to trigger snap");
                new_cmd = Console.ReadLine();
                Xfer.Snap();

                Console.WriteLine("Press a key to trigger save");
                new_cmd = Console.ReadLine();
                View.Buffer.Save("test.raw", "-format raw");

                var dsb = new StringBuilder("frame: " + frame_count);
                Console.WriteLine(dsb);
                frame_count = frame_count + 1;

                Console.WriteLine("File saved, Press a key to repeat, q to quit:");
                new_cmd = Console.ReadLine();
                if (new_cmd == "q") { stop_snap = true; }
                //if (curr_code == 113) { stop_snap = true; }

            }

            DestroysObjects(Acq, AcqDevice, Buffers, Xfer, View);
            loc.Dispose();
        }
Beispiel #19
0
        //static void Main(string[] args)
        public static void Done()
        {
            SapAcqDevice camera   = null;
            SapView      view     = null;
            SapTransfer  transfer = null;
            SapBuffer    buffer   = null;

            Console.WriteLine("Sapera Console GigE Cameras AutoWhiteBalance Example (C# version)\n");

            MyAcquisitionParams acqParams = new MyAcquisitionParams();

            /*if (!GetOptions(args, acqParams))
             * {
             *  Console.WriteLine("\nPress any key to terminate\n");
             *  Console.ReadKey();
             *  return;
             * }*/
            SapLocation location = new SapLocation(acqParams.ServerName, acqParams.ResourceIndex);


            camera   = new SapAcqDevice(location, acqParams.ConfigFileName);
            buffer   = new SapBufferWithTrash(2, camera, SapBuffer.MemoryType.ScatterGather);
            transfer = new SapAcqDeviceToBuf(camera, buffer);
            view     = new SapView(buffer);

            // End of frame event
            transfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
            transfer.XferNotify        += new SapXferNotifyHandler(Xfer_XferNotify);
            transfer.XferNotifyContext  = view;

            if (!camera.Create())
            {
                Console.WriteLine("Error during SapAcquisition creation!\n");
                DestroysObjects(camera, buffer, transfer, view);
                return;
            }

            // Monochrome models are not supported for White Balance Calibration.
            int  colorType   = 0;
            bool isAvailable = false;

            if (isAvailable = camera.IsFeatureAvailable("ColorType"))
            {
                if (camera.GetFeatureValue("ColorType", out colorType))
                {
                    if (colorType == 0)
                    {
                        Console.WriteLine("This camera model does not support Auto White Balance calibration.\n");
                        DestroysObjects(camera, buffer, transfer, view);
                        return;
                    }
                }
            }


            //activate feature only if camera has 3 separate gains

            Boolean bRedGain   = false;
            Boolean bGreenGain = false;
            Boolean bBlueGain  = false;

            bRedGain   = camera.IsFeatureAvailable("GainRed");
            bGreenGain = camera.IsFeatureAvailable("GainGreen");
            bBlueGain  = camera.IsFeatureAvailable("GainBlue");
            if (!bRedGain && !bGreenGain && !bBlueGain)
            {
                Console.WriteLine("This camera model does not support Auto White Balance calibration.\n");
                DestroysObjects(camera, buffer, transfer, view);
                return;
            }

            // Create buffer object
            if (!buffer.Create())
            {
                Console.WriteLine("Error during SapBuffer creation!\n");
                DestroysObjects(camera, buffer, transfer, view);
                return;
            }

            // Create transfer object
            if (!transfer.Create())
            {
                Console.WriteLine("Error during SapTransfer creation!\n");
                DestroysObjects(camera, buffer, transfer, view);
                return;
            }

            // Create view object
            if (!view.Create())
            {
                Console.WriteLine("Error during SapView creation!\n");
                DestroysObjects(camera, buffer, transfer, view);
                return;
            }

            Console.WriteLine("Press any key to start Grab. Press 'q' to quit.");
            ConsoleKeyInfo info = Console.ReadKey(true);
            char           key  = info.KeyChar;

            if (key != 0)
            {
                if (key == 'q')
                {
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }
            }

            // Start continous grab
            transfer.Grab();

            Console.WriteLine("Press any key to stop grab\n");
            Console.ReadKey();

            // Stop grab
            transfer.Freeze();
            transfer.Wait(5000);

            Console.WriteLine("Press any key to start Auto White Balance calibration. Press 'q' to quit.\n");
            info = Console.ReadKey(true);
            key  = info.KeyChar;
            if (key != 0)
            {
                if (key == 'q')
                {
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }
            }

            int oldPixelFormat = 0;

            isAvailable = false;
            if (isAvailable = camera.IsFeatureAvailable("PixelFormat"))
            {
                camera.GetFeatureValue("PixelFormat", out oldPixelFormat);
            }
            else
            {
                Console.WriteLine("This camera model does not support Auto White Balance calibration.\n");
                DestroysObjects(camera, buffer, transfer, view);
                return;
            }

            if (oldPixelFormat != GVSP_PIX_BAYRG8)
            {
                // Camera Pixel Format defined is not Raw Bayer.
                // We change the value

                DestroysObjects(null, buffer, transfer, view);

                camera.SetFeatureValue("PixelFormat", GVSP_PIX_BAYRG8);

                // Instantiation of new buffer object
                buffer = new SapBufferWithTrash(2, camera, SapBuffer.MemoryType.ScatterGather);
                // Instantiation of new view object
                view = new SapView(buffer);
                // Instantiation of new transfer object
                transfer = new SapAcqDeviceToBuf(camera, buffer);

                // End of frame event
                transfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
                transfer.XferNotify        += new SapXferNotifyHandler(Xfer_XferNotify);
                transfer.XferNotifyContext  = view;

                if (!camera.Create())
                {
                    Console.WriteLine("Error during SapAcquisition creation!\n");
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }

                // Create buffer object
                if (!buffer.Create())
                {
                    Console.WriteLine("Error during SapBuffer creation!\n");
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }

                // Create transfer object
                if (!transfer.Create())
                {
                    Console.WriteLine("Error during SapTransfer creation!\n");
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }

                // Create view object
                if (!view.Create())
                {
                    Console.WriteLine("Error during SapView creation!\n");
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }
            }


            if (!AutoWhiteBalanceOperations(camera, buffer, transfer))
            {
                Console.WriteLine("Auto WhiteBalamce has failed");
                DestroysObjects(camera, buffer, transfer, view);
                return;
            }

            if (oldPixelFormat != GVSP_PIX_BAYRG8)
            {
                // Camera Pixel Format was changed for Raw Bayer.
                // We set the original value.
                DestroysObjects(null, buffer, transfer, view);

                camera.SetFeatureValue("PixelFormat", oldPixelFormat);

                // Instantiation of new buffer object
                buffer = new SapBufferWithTrash(2, camera, SapBuffer.MemoryType.ScatterGather);
                // Instantiation of new view object
                view = new SapView(buffer);
                // Instantiation of new transfer object
                transfer = new SapAcqDeviceToBuf(camera, buffer);

                // End of frame event
                transfer.Pairs[0].EventType = SapXferPair.XferEventType.EndOfFrame;
                transfer.XferNotify        += new SapXferNotifyHandler(Xfer_XferNotify);
                transfer.XferNotifyContext  = view;

                // Create buffer object
                if (!buffer.Create())
                {
                    Console.WriteLine("Error during SapBuffer creation!\n");
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }

                // Create transfer object
                if (!transfer.Create())
                {
                    Console.WriteLine("Error during SapTransfer creation!\n");
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }

                // Create view object
                if (!view.Create())
                {
                    Console.WriteLine("Error during SapView creation!\n");
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }
            }

            Console.WriteLine("Press any key to start Grab. Press 'q' to quit.\n");
            info = Console.ReadKey(true);
            key  = info.KeyChar;
            if (key != 0)
            {
                if (key == 'q')
                {
                    DestroysObjects(camera, buffer, transfer, view);
                    return;
                }
            }

            // Start continous grab
            transfer.Grab();

            Console.WriteLine("Press any key to stop grab\n");
            Console.ReadKey();

            // Stop grab
            transfer.Freeze();
            transfer.Wait(5000);

            DestroysObjects(camera, buffer, transfer, view);
            location.Dispose();
        }
Beispiel #20
0
        static bool AutoWhiteBalanceOperations(SapAcqDevice Camera, SapBuffer Buffers, SapTransfer Transfer)
        {
            Console.WriteLine("\nCalibration in progress ...........\n\n");

            double coefBlueGain         = MAX_COEF + 1;
            double coefGreenGain        = MAX_COEF + 1;
            double coefRedGain          = MAX_COEF + 1;
            int    calibrationIteration = 0;

            // Create a new Bayer object
            SapBayer   Bayer       = new SapBayer(Buffers);
            SapFeature FeatureInfo = new SapFeature(Camera.Location);

            if (!FeatureInfo.Create())
            {
                DestroysFeaturesAndBayer(FeatureInfo, Bayer);
                return(false);
            }

            // Create Bayer object
            if (!Bayer.Create())
            {
                DestroysFeaturesAndBayer(FeatureInfo, Bayer);
                return(false);
            }

            // Initialize all Gain colors to 0
            Camera.SetFeatureValue("GainBlue", 0);
            Camera.SetFeatureValue("GainGreen", 0);
            Camera.SetFeatureValue("GainRed", 0);

            // Choose alignment used
            Bayer.Align = SapBayer.AlignMode.RGGB;

            // Definition of ROI used for calibration
            int fixSelectedRoiLeft = 0;
            int fixSelectedRoiTop  = 0;
            // Half buffer width
            int fixSelectedRoiWidth = Buffers.Width / 2;
            // Half buffer height
            int fixSelectedRoiHeight = Buffers.Height / 2;

            // Start loop for calibration until each coefficient is under 1.05
            while (coefBlueGain > MAX_COEF || coefGreenGain > MAX_COEF || coefRedGain > MAX_COEF)
            {
                if (!Transfer.Snap())
                {
                    Console.WriteLine("Unable to acquire an image");
                    return(false);
                }

                Thread.Sleep(500);
                // Call WhiteBalance function
                if (!Bayer.WhiteBalance(Buffers, fixSelectedRoiLeft, fixSelectedRoiTop, fixSelectedRoiWidth, fixSelectedRoiHeight))
                {
                    break;
                }

                // New coefficients values are reused.
                coefBlueGain  = Bayer.WBGain.Blue;
                coefGreenGain = Bayer.WBGain.Green;
                coefRedGain   = Bayer.WBGain.Red;

                if (coefRedGain > MAX_COEF)
                {
                    if (!ComputeGain("GainRed", Camera, FeatureInfo, coefRedGain))
                    {
                        break;
                    }
                }
                if (coefGreenGain > MAX_COEF)
                {
                    if (!ComputeGain("GainGreen", Camera, FeatureInfo, coefGreenGain))
                    {
                        break;
                    }
                }
                if (coefBlueGain > MAX_COEF)
                {
                    if (!ComputeGain("GainBlue", Camera, FeatureInfo, coefBlueGain))
                    {
                        break;
                    }
                }

                if (calibrationIteration >= MAX_CALIBRATION_ITERATION)
                {
                    Console.WriteLine("Iterations for calibration are at the maximum.\n");
                    break;
                }
                calibrationIteration++;
            }

            // Uncomment this part if you want to get new values after calibration.

            /*
             * int gainBlue=0, gainRed=0, gainGreen=0;
             * Camera.GetFeatureValue("GainBlue", out gainBlue);
             * Camera.GetFeatureValue("GainRed", out gainRed);
             * Camera.GetFeatureValue("GainGreen", out gainGreen);
             */

            DestroysFeaturesAndBayer(FeatureInfo, Bayer);
            Console.WriteLine("\nCalibration finished ...........\n\n");
            return(true);
        }