Exemplo n.º 1
0
        public FormConfiguration(CameraSummary summary)
        {
            this.summary = summary;

            InitializeComponent();
            tbAlias.AutoSize = false;
            tbAlias.Height   = 20;

            tbAlias.Text            = summary.Alias;
            lblSystemName.Text      = summary.Name;
            btnIcon.BackgroundImage = summary.Icon;

            SpecificInfo specific = summary.Specific as SpecificInfo;

            if (specific == null || specific.Handle == null || !specific.Handle.IsValid)
            {
                return;
            }

            deviceHandle     = specific.Handle;
            cameraProperties = CameraPropertyManager.Read(specific.Handle, summary.Identifier);

            if (cameraProperties.Count != specific.CameraProperties.Count)
            {
                specificChanged = true;
            }

            Populate();
        }
Exemplo n.º 2
0
        public override string GetSummaryAsText(CameraSummary summary)
        {
            string result = "";
            string alias  = summary.Alias;

            SpecificInfo info = summary.Specific as SpecificInfo;

            try
            {
                if (info != null &&
                    info.StreamFormat != null &&
                    info.CameraProperties.ContainsKey("width") &&
                    info.CameraProperties.ContainsKey("height") &&
                    info.CameraProperties.ContainsKey("framerate"))
                {
                    string format    = info.StreamFormat;
                    int    width     = int.Parse(info.CameraProperties["width"].CurrentValue, CultureInfo.InvariantCulture);
                    int    height    = int.Parse(info.CameraProperties["height"].CurrentValue, CultureInfo.InvariantCulture);
                    double framerate = double.Parse(info.CameraProperties["framerate"].CurrentValue, CultureInfo.InvariantCulture);

                    result = string.Format("{0} - {1}×{2} @ {3:0.##} fps ({4}).", alias, width, height, framerate, format);
                }
                else
                {
                    result = string.Format("{0}", alias);
                }
            }
            catch
            {
                result = string.Format("{0}", alias);
            }

            return(result);
        }
Exemplo n.º 3
0
        private void Open()
        {
            if (grabbing)
            {
                Stop();
            }

            try
            {
                deviceHandle = Pylon.CreateDeviceByIndex(deviceIndex);
                imageProvider.Open(deviceHandle);
            }
            catch (Exception e)
            {
                log.Error("Could not open Basler device.");
                LogError(e, imageProvider.GetLastErrorMessage());
                return;
            }

            if (!deviceHandle.IsValid)
            {
                return;
            }

            SpecificInfo specific = summary.Specific as SpecificInfo;

            if (specific == null)
            {
                return;
            }

            // Store the handle into the specific info so that we can retrieve device informations from the configuration dialog.
            specific.Handle = deviceHandle;
            GenApiEnum currentStreamFormat = PylonHelper.ReadEnumCurrentValue(deviceHandle, "PixelFormat");

            if (!string.IsNullOrEmpty(specific.StreamFormat) && specific.StreamFormat != currentStreamFormat.Symbol)
            {
                PylonHelper.WriteEnum(deviceHandle, "PixelFormat", specific.StreamFormat);
            }

            // The bayer conversion mode will be set during Prepare().

            if (firstOpen)
            {
                // Restore camera parameters from the XML blurb.
                // Regular properties, including image size.
                // First we read the current properties from the API to get fully formed properties.
                // We merge the values saved in the XML into the properties.
                // (The restoration from the XML doesn't create fully formed properties, it just contains the values).
                // Then commit the properties to the camera.
                Dictionary <string, CameraProperty> cameraProperties = CameraPropertyManager.Read(deviceHandle, summary.Identifier);
                CameraPropertyManager.MergeProperties(cameraProperties, specific.CameraProperties);
                specific.CameraProperties = cameraProperties;
                CameraPropertyManager.WriteCriticalProperties(deviceHandle, specific.CameraProperties);
            }
            else
            {
                CameraPropertyManager.WriteCriticalProperties(deviceHandle, specific.CameraProperties);
            }
        }
Exemplo n.º 4
0
        public override bool Configure(CameraSummary summary)
        {
            bool         needsReconnection = false;
            SpecificInfo info = summary.Specific as SpecificInfo;

            if (info == null)
            {
                return(false);
            }

            FormConfiguration form = new FormConfiguration(summary);

            if (form.ShowDialog() == DialogResult.OK)
            {
                if (form.AliasChanged)
                {
                    summary.UpdateAlias(form.Alias, form.PickedIcon);
                }

                if (form.SpecificChanged)
                {
                    info.StreamFormat     = form.SelectedStreamFormat.Symbol;
                    info.Bayer8Conversion = form.Bayer8Conversion;
                    info.CameraProperties = form.CameraProperties;

                    summary.UpdateDisplayRectangle(Rectangle.Empty);
                    needsReconnection = true;
                }

                CameraTypeManager.UpdatedCameraSummary(summary);
            }

            form.Dispose();
            return(needsReconnection);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Configure device and report frame format that will be used during streaming.
        /// This method must return a proper ImageDescriptor so we can pre-allocate buffers.
        /// </summary>
        public ImageDescriptor Prepare()
        {
            Open();

            if (deviceHandle == null || !deviceHandle.IsValid)
            {
                return(ImageDescriptor.Invalid);
            }

            firstOpen = false;

            // Get the configured framerate for recording support.
            if (Pylon.DeviceFeatureIsReadable(deviceHandle, "ResultingFrameRateAbs"))
            {
                resultingFramerate = (float)Pylon.DeviceGetFloatFeature(deviceHandle, "ResultingFrameRateAbs");
            }
            else if (Pylon.DeviceFeatureIsReadable(deviceHandle, "ResultingFrameRate"))
            {
                resultingFramerate = (float)Pylon.DeviceGetFloatFeature(deviceHandle, "ResultingFrameRate");
            }

            SpecificInfo specific           = summary.Specific as SpecificInfo;
            string       streamFormatSymbol = specific.StreamFormat;

            bool hasWidth                  = Pylon.DeviceFeatureIsReadable(deviceHandle, "Width");
            bool hasHeight                 = Pylon.DeviceFeatureIsReadable(deviceHandle, "Height");
            bool hasPixelFormat            = Pylon.DeviceFeatureIsReadable(deviceHandle, "PixelFormat");
            bool canComputeImageDescriptor = hasWidth && hasHeight && hasPixelFormat;

            if (!canComputeImageDescriptor)
            {
                return(ImageDescriptor.Invalid);
            }

            int    width       = (int)Pylon.DeviceGetIntegerFeature(deviceHandle, "Width");
            int    height      = (int)Pylon.DeviceGetIntegerFeature(deviceHandle, "Height");
            string pixelFormat = Pylon.DeviceFeatureToString(deviceHandle, "PixelFormat");

            // Note: the image provider will perform the Bayer conversion itself and only output two formats.
            // - Y800 for anything monochrome.
            // - RGB32 for anything color.
            EPylonPixelType pixelType = Pylon.PixelTypeFromString(pixelFormat);

            if (pixelType == EPylonPixelType.PixelType_Undefined)
            {
                return(ImageDescriptor.Invalid);
            }

            bool        monochrome = Pylon.IsMono(pixelType) && !Pylon.IsBayer(pixelType);
            ImageFormat format     = monochrome ? format = ImageFormat.Y800 : ImageFormat.RGB32;

            int  bufferSize = ImageFormatHelper.ComputeBufferSize(width, height, format);
            bool topDown    = true;

            return(new ImageDescriptor(format, width, height, topDown, bufferSize));
        }
Exemplo n.º 6
0
 private void Populate(SpecificInfo specific)
 {
     try
     {
         PopulateStreamFormat();
         PopulateBayerConversion();
         PopulateCameraControls();
     }
     catch
     {
         log.ErrorFormat(PylonHelper.GetLastError());
     }
 }
Exemplo n.º 7
0
        private string SpecificInfoSerialize(CameraSummary summary)
        {
            SpecificInfo info = summary.Specific as SpecificInfo;

            if (info == null)
            {
                return(null);
            }

            XmlDocument doc     = new XmlDocument();
            XmlElement  xmlRoot = doc.CreateElement("Basler");

            XmlElement xmlStreamFormat = doc.CreateElement("StreamFormat");

            xmlStreamFormat.InnerText = info.StreamFormat;
            xmlRoot.AppendChild(xmlStreamFormat);

            XmlElement xmlBayer8Conversion = doc.CreateElement("Bayer8Conversion");

            xmlBayer8Conversion.InnerText = info.Bayer8Conversion.ToString().ToLower();
            xmlRoot.AppendChild(xmlBayer8Conversion);

            XmlElement xmlCameraProperties = doc.CreateElement("CameraProperties");

            foreach (KeyValuePair <string, CameraProperty> pair in info.CameraProperties)
            {
                XmlElement   xmlCameraProperty = doc.CreateElement("CameraProperty");
                XmlAttribute attr = doc.CreateAttribute("key");
                attr.Value = pair.Key;
                xmlCameraProperty.Attributes.Append(attr);

                XmlElement xmlCameraPropertyValue = doc.CreateElement("Value");
                xmlCameraPropertyValue.InnerText = pair.Value.CurrentValue;
                xmlCameraProperty.AppendChild(xmlCameraPropertyValue);

                XmlElement xmlCameraPropertyAuto = doc.CreateElement("Auto");
                xmlCameraPropertyAuto.InnerText = pair.Value.Automatic.ToString().ToLower();
                xmlCameraProperty.AppendChild(xmlCameraPropertyAuto);

                xmlCameraProperties.AppendChild(xmlCameraProperty);
            }

            xmlRoot.AppendChild(xmlCameraProperties);

            doc.AppendChild(xmlRoot);

            return(doc.OuterXml);
        }
Exemplo n.º 8
0
        public override string GetSummaryAsText(CameraSummary summary)
        {
            string result = "";
            string alias  = summary.Alias;

            SpecificInfo info = summary.Specific as SpecificInfo;

            try
            {
                if (info != null &&
                    info.StreamFormat != null &&
                    info.CameraProperties.ContainsKey("width") &&
                    info.CameraProperties.ContainsKey("height") &&
                    info.CameraProperties.ContainsKey("framerate"))
                {
                    string format    = info.StreamFormat;
                    int    width     = int.Parse(info.CameraProperties["width"].CurrentValue, CultureInfo.InvariantCulture);
                    int    height    = int.Parse(info.CameraProperties["height"].CurrentValue, CultureInfo.InvariantCulture);
                    double framerate = 0;

                    // The configured framerate is always between 0 and 100 000, but the actual resulting framerate can be obtained.
                    if (info.Handle != null && info.Handle.IsValid)
                    {
                        framerate = PylonHelper.GetResultingFramerate(info.Handle);
                    }
                    else
                    {
                        framerate = double.Parse(info.CameraProperties["framerate"].CurrentValue, CultureInfo.InvariantCulture);
                    }

                    result = string.Format("{0} - {1}×{2} @ {3:0.##} fps ({4}).", alias, width, height, framerate, format);
                }
                else
                {
                    result = string.Format("{0}", alias);
                }
            }
            catch
            {
                result = string.Format("{0}", alias);
            }

            return(result);
        }
Exemplo n.º 9
0
        private void BtnReconnect_Click(object sender, EventArgs e)
        {
            if (SelectedStreamFormat == null)
            {
                // This happens when we load the config window and the camera isn't connected.
                return;
            }

            SpecificInfo info = summary.Specific as SpecificInfo;

            if (info == null)
            {
                return;
            }

            info.StreamFormat     = this.SelectedStreamFormat.Symbol;
            info.Bayer8Conversion = this.Bayer8Conversion;
            info.CameraProperties = this.CameraProperties;
            summary.UpdateDisplayRectangle(Rectangle.Empty);
            CameraTypeManager.UpdatedCameraSummary(summary);

            disconnect();
            connect();

            SpecificInfo specific = summary.Specific as SpecificInfo;

            if (specific == null || specific.Handle == null || !specific.Handle.IsValid)
            {
                return;
            }

            deviceHandle     = specific.Handle;
            cameraProperties = CameraPropertyManager.Read(specific.Handle, summary.Identifier);

            RemoveCameraControls();
            PopulateCameraControls();
            UpdateResultingFramerate();
        }
Exemplo n.º 10
0
        public FormConfiguration(CameraSummary summary, Action disconnect, Action connect)
        {
            this.summary    = summary;
            this.disconnect = disconnect;
            this.connect    = connect;

            InitializeComponent();
            tbAlias.AutoSize = false;
            tbAlias.Height   = 20;

            tbAlias.Text            = summary.Alias;
            lblSystemName.Text      = summary.Name;
            btnIcon.BackgroundImage = summary.Icon;
            btnReconnect.Text       = CameraLang.FormConfiguration_Reconnect;

            SpecificInfo specific = summary.Specific as SpecificInfo;

            if (specific == null || specific.Handle == null || !specific.Handle.IsValid)
            {
                return;
            }

            deviceHandle     = specific.Handle;
            cameraProperties = CameraPropertyManager.Read(specific.Handle, summary.Identifier);

            if (cameraProperties.Count != specific.CameraProperties.Count)
            {
                specificChanged = true;
            }

            bayer8Conversion = specific.Bayer8Conversion;

            Populate();
            this.Text     = CameraLang.FormConfiguration_Title;
            btnApply.Text = CameraLang.Generic_Apply;
            UpdateResultingFramerate();
        }
Exemplo n.º 11
0
        public override List <CameraSummary> DiscoverCameras(IEnumerable <CameraBlurb> blurbs)
        {
            List <CameraSummary> summaries = new List <CameraSummary>();

            // We don't do the discover step every time to avoid UI freeze since Enumerate takes some time.
            if (discoveryStep > 0)
            {
                discoveryStep = (discoveryStep + 1) % discoverySkip;
                foreach (CameraSummary summary in cache.Values)
                {
                    summaries.Add(summary);
                }

                return(summaries);
            }

            discoveryStep = 1;
            List <CameraSummary> found = new List <CameraSummary>();

            List <DeviceEnumerator.Device> devices = DeviceEnumerator.EnumerateDevices();

            foreach (DeviceEnumerator.Device device in devices)
            {
                string identifier = device.FullName;

                bool cached = cache.ContainsKey(identifier);
                if (cached)
                {
                    deviceIndices[identifier] = device.Index;
                    summaries.Add(cache[identifier]);
                    found.Add(cache[identifier]);
                    continue;
                }

                string             alias            = device.Name;
                Bitmap             icon             = null;
                SpecificInfo       specific         = new SpecificInfo();
                Rectangle          displayRectangle = Rectangle.Empty;
                CaptureAspectRatio aspectRatio      = CaptureAspectRatio.Auto;
                deviceIndices[identifier] = device.Index;

                if (blurbs != null)
                {
                    foreach (CameraBlurb blurb in blurbs)
                    {
                        if (blurb.CameraType != this.CameraType || blurb.Identifier != identifier)
                        {
                            continue;
                        }

                        alias            = blurb.Alias;
                        icon             = blurb.Icon ?? defaultIcon;
                        displayRectangle = blurb.DisplayRectangle;
                        if (!string.IsNullOrEmpty(blurb.AspectRatio))
                        {
                            aspectRatio = (CaptureAspectRatio)Enum.Parse(typeof(CaptureAspectRatio), blurb.AspectRatio);
                        }

                        // Restore saved parameters.
                        specific = SpecificInfoDeserialize(blurb.Specific);
                        break;
                    }
                }

                icon = icon ?? defaultIcon;

                CameraSummary summary = new CameraSummary(alias, device.Name, identifier, icon, displayRectangle, aspectRatio, specific, this);

                summaries.Add(summary);
                found.Add(summary);
                cache.Add(identifier, summary);
            }

            List <CameraSummary> lost = new List <CameraSummary>();

            foreach (CameraSummary summary in cache.Values)
            {
                if (!found.Contains(summary))
                {
                    lost.Add(summary);
                }
            }

            foreach (CameraSummary summary in lost)
            {
                cache.Remove(summary.Identifier);
            }

            return(summaries);
        }
Exemplo n.º 12
0
        private SpecificInfo SpecificInfoDeserialize(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                return(null);
            }

            SpecificInfo info = null;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(new StringReader(xml));

                info = new SpecificInfo();

                string  streamFormat    = "";
                XmlNode xmlStreamFormat = doc.SelectSingleNode("/Basler/StreamFormat");
                if (xmlStreamFormat != null)
                {
                    streamFormat = xmlStreamFormat.InnerText;
                }

                Bayer8Conversion bayer8Conversion    = Bayer8Conversion.Color;
                XmlNode          xmlBayer8Conversion = doc.SelectSingleNode("/Basler/Bayer8Conversion");
                if (xmlBayer8Conversion != null)
                {
                    bayer8Conversion = (Bayer8Conversion)Enum.Parse(typeof(Bayer8Conversion), xmlBayer8Conversion.InnerText);
                }

                Dictionary <string, CameraProperty> cameraProperties = new Dictionary <string, CameraProperty>();

                XmlNodeList props = doc.SelectNodes("/Basler/CameraProperties/CameraProperty");
                foreach (XmlNode node in props)
                {
                    XmlAttribute keyAttribute = node.Attributes["key"];
                    if (keyAttribute == null)
                    {
                        continue;
                    }

                    string         key      = keyAttribute.Value;
                    CameraProperty property = new CameraProperty();

                    string  xpath            = string.Format("/Basler/CameraProperties/CameraProperty[@key='{0}']", key);
                    XmlNode xmlPropertyValue = doc.SelectSingleNode(xpath + "/Value");
                    if (xmlPropertyValue != null)
                    {
                        property.CurrentValue = xmlPropertyValue.InnerText;
                    }
                    else
                    {
                        property.Supported = false;
                    }

                    XmlNode xmlPropertyAuto = doc.SelectSingleNode(xpath + "/Auto");
                    if (xmlPropertyAuto != null)
                    {
                        property.Automatic = XmlHelper.ParseBoolean(xmlPropertyAuto.InnerText);
                    }
                    else
                    {
                        property.Supported = false;
                    }

                    cameraProperties.Add(key, property);
                }

                info.StreamFormat     = streamFormat;
                info.Bayer8Conversion = bayer8Conversion;
                info.CameraProperties = cameraProperties;
            }
            catch (Exception e)
            {
                log.ErrorFormat(e.Message);
            }

            return(info);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Configure device and report frame format that will be used during streaming.
        /// This method must return a proper ImageDescriptor so we can pre-allocate buffers.
        /// </summary>
        public ImageDescriptor Prepare()
        {
            Open();

            if (deviceHandle == null || !deviceHandle.IsValid)
            {
                return(ImageDescriptor.Invalid);
            }

            firstOpen = false;

            // Get the configured framerate for recording support.
            resultingFramerate = PylonHelper.GetResultingFramerate(deviceHandle);

            SpecificInfo specific           = summary.Specific as SpecificInfo;
            string       streamFormatSymbol = specific.StreamFormat;

            bool hasWidth                  = Pylon.DeviceFeatureIsReadable(deviceHandle, "Width");
            bool hasHeight                 = Pylon.DeviceFeatureIsReadable(deviceHandle, "Height");
            bool hasPixelFormat            = Pylon.DeviceFeatureIsReadable(deviceHandle, "PixelFormat");
            bool canComputeImageDescriptor = hasWidth && hasHeight && hasPixelFormat;

            if (!canComputeImageDescriptor)
            {
                return(ImageDescriptor.Invalid);
            }

            int    width       = (int)Pylon.DeviceGetIntegerFeature(deviceHandle, "Width");
            int    height      = (int)Pylon.DeviceGetIntegerFeature(deviceHandle, "Height");
            string pixelFormat = Pylon.DeviceFeatureToString(deviceHandle, "PixelFormat");

            EPylonPixelType pixelType = Pylon.PixelTypeFromString(pixelFormat);

            if (pixelType == EPylonPixelType.PixelType_Undefined)
            {
                return(ImageDescriptor.Invalid);
            }

            // Note: the image provider will perform the Bayer conversion itself and only output two formats.
            // - Y800 for anything monochrome.
            // - RGB32 for anything color.
            imageProvider.SetDebayering(specific.Bayer8Conversion);

            bool        isBayer    = Pylon.IsBayer(pixelType);
            bool        isBayer8   = PylonHelper.IsBayer8(pixelType);
            bool        bayerColor = (isBayer && !isBayer8) || (isBayer8 && specific.Bayer8Conversion == Bayer8Conversion.Color);
            bool        color      = !Pylon.IsMono(pixelType) || bayerColor;
            ImageFormat format     = color ? ImageFormat.RGB32 : ImageFormat.Y800;

            finishline.Prepare(width, height, format, resultingFramerate);
            if (finishline.Enabled)
            {
                height             = finishline.Height;
                resultingFramerate = finishline.ResultingFramerate;
            }

            int  bufferSize = ImageFormatHelper.ComputeBufferSize(width, height, format);
            bool topDown    = true;

            return(new ImageDescriptor(format, width, height, topDown, bufferSize));
        }
Exemplo n.º 14
0
        private void Open()
        {
            // Unlike in the DirectShow module, we do not backup and restore camera configuration.
            // If the user configured the camera outside of Kinovea we respect the new settings.
            // Two reasons:
            // 1. In DirectShow we must do the backup/restore to work around drivers that inadvertently reset the camera properties.
            // 2. Industrial cameras have many properties that won't be configurable in Kinovea
            // so the user is more likely to configure the camera from the outside.

            if (grabbing)
            {
                Stop();
            }

            try
            {
                deviceHandle = Pylon.CreateDeviceByIndex(deviceIndex);
                imageProvider.Open(deviceHandle);
            }
            catch (Exception e)
            {
                log.Error("Could not open Basler device.");
                LogError(e, imageProvider.GetLastErrorMessage());
                return;
            }

            if (!deviceHandle.IsValid)
            {
                return;
            }

            SpecificInfo specific = summary.Specific as SpecificInfo;

            if (specific == null)
            {
                return;
            }

            // Store the handle into the specific info so that we can retrieve device informations from the configuration dialog.
            specific.Handle = deviceHandle;

            GenApiEnum currentStreamFormat = PylonHelper.ReadEnumCurrentValue(deviceHandle, "PixelFormat");

            // Some properties can only be changed when the camera is opened but not streaming.
            // We store them in the summary when coming back from FormConfiguration, and we write them to the camera here.
            // Only do this if it's not the first time we open the camera, to respect any change that could have been done outside Kinovea.
            if (!firstOpen)
            {
                if (specific.StreamFormat != currentStreamFormat.Symbol)
                {
                    PylonHelper.WriteEnum(deviceHandle, "PixelFormat", specific.StreamFormat);
                }

                if (specific.CameraProperties != null && specific.CameraProperties.ContainsKey("framerate"))
                {
                    if (specific.CameraProperties.ContainsKey("enableFramerate") && specific.CameraProperties["enableFramerate"].Supported)
                    {
                        bool enabled = bool.Parse(specific.CameraProperties["enableFramerate"].CurrentValue);
                        if (!enabled && !specific.CameraProperties["enableFramerate"].ReadOnly)
                        {
                            specific.CameraProperties["enableFramerate"].CurrentValue = "true";
                            CameraPropertyManager.Write(deviceHandle, specific.CameraProperties["enableFramerate"]);
                        }
                    }

                    CameraPropertyManager.Write(deviceHandle, specific.CameraProperties["framerate"]);
                }

                if (specific.CameraProperties != null && specific.CameraProperties.ContainsKey("width") && specific.CameraProperties.ContainsKey("height"))
                {
                    CameraPropertyManager.Write(deviceHandle, specific.CameraProperties["width"]);
                    CameraPropertyManager.Write(deviceHandle, specific.CameraProperties["height"]);
                }
            }
            else
            {
                specific.StreamFormat = currentStreamFormat.Symbol;
            }
        }