Exemple #1
0
        internal static void SetAnnotateSettings(this MMALCameraComponent camera)
        {
            if (MMALCameraConfig.Annotate != null)
            {
                MMALLog.Logger.LogDebug("Setting annotate");

                var sb = new StringBuilder();

                var showShutter           = 0;
                var showAnalogGain        = 0;
                var showLens              = 0;
                var showCaf               = 0;
                var showMotion            = 0;
                var showFrame             = 0;
                var enableTextBackground  = 0;
                var textSize              = (byte)0;
                var customTextColor       = 0;
                var customTextY           = (byte)0;
                var customTextU           = (byte)0;
                var customTextV           = (byte)0;
                var customBackgroundColor = 0;
                var customBackgroundY     = (byte)0;
                var customBackgroundU     = (byte)0;
                var customBackgroundV     = (byte)0;
                var justify               = MMALCameraConfig.Annotate.Justify;
                var xOffset               = MMALCameraConfig.Annotate.XOffset;
                var yOffset               = MMALCameraConfig.Annotate.YOffset;

                if (!string.IsNullOrEmpty(MMALCameraConfig.Annotate.CustomText))
                {
                    sb.Append(MMALCameraConfig.Annotate.CustomText + " ");
                }

                if (MMALCameraConfig.Annotate.ShowTimeText)
                {
                    sb.Append(DateTime.Now.ToString("HH:mm") + " ");
                }

                if (MMALCameraConfig.Annotate.ShowDateText)
                {
                    sb.Append(DateTime.Now.ToString("dd/MM/yyyy") + " ");
                }

                if (MMALCameraConfig.Annotate.ShowShutterSettings)
                {
                    showShutter = 1;
                }

                if (MMALCameraConfig.Annotate.ShowGainSettings)
                {
                    showAnalogGain = 1;
                }

                if (MMALCameraConfig.Annotate.ShowLensSettings)
                {
                    showLens = 1;
                }

                if (MMALCameraConfig.Annotate.ShowCafSettings)
                {
                    showCaf = 1;
                }

                if (MMALCameraConfig.Annotate.ShowMotionSettings)
                {
                    showMotion = 1;
                }

                if (MMALCameraConfig.Annotate.ShowFrameNumber)
                {
                    showFrame = 1;
                }

                if (MMALCameraConfig.Annotate.AllowCustomBackgroundColour)
                {
                    enableTextBackground = 1;
                }

                textSize = Convert.ToByte(MMALCameraConfig.Annotate.TextSize);

                if (MMALCameraConfig.Annotate.TextColour != Color.Empty)
                {
                    customTextColor = 1;

                    var yuv = MMALColor.RGBToYUVBytes(MMALCameraConfig.Annotate.TextColour);
                    customTextY = yuv.Item1;
                    customTextU = yuv.Item2;
                    customTextV = yuv.Item3;
                }

                if (MMALCameraConfig.Annotate.BgColour != Color.Empty)
                {
                    customBackgroundColor = 1;
                    var yuv = MMALColor.RGBToYUVBytes(MMALCameraConfig.Annotate.BgColour);
                    customBackgroundY = yuv.Item1;
                    customBackgroundU = yuv.Item2;
                    customBackgroundV = yuv.Item3;
                }

                // .NET Core has an issue with marshalling arrays "ByValArray". The array being passed MUST equal the size
                // specified in the "SizeConst" field or you will receive an exception. Mono does not have this restriction
                // and is quite happy to pass an array of a lower size if asked. In order to get around this, I am creating
                // an array equaling "SizeConst" and copying the contents of the annotation text into it, minus the EOL character.
                var text  = sb.ToString() + char.MinValue;
                var arr   = new byte[MMALParametersCamera.MMAL_CAMERA_ANNOTATE_MAX_TEXT_LEN_V3];
                var bytes = Encoding.ASCII.GetBytes(text);

                Array.Copy(bytes, arr, bytes.Length);

                var strV4 = new MMAL_PARAMETER_CAMERA_ANNOTATE_V4_T(
                    new MMAL_PARAMETER_HEADER_T(MMAL_PARAMETER_ANNOTATE, Marshal.SizeOf <MMAL_PARAMETER_CAMERA_ANNOTATE_V4_T>() + (arr.Length - 1)),
                    1, showShutter, showAnalogGain, showLens,
                    showCaf, showMotion, showFrame, enableTextBackground,
                    customBackgroundColor, customBackgroundY, customBackgroundU, customBackgroundV, 0, customTextColor,
                    customTextY, customTextU, customTextV, textSize, arr, (int)justify, xOffset, yOffset);

                var ptrV4 = Marshal.AllocHGlobal(Marshal.SizeOf <MMAL_PARAMETER_CAMERA_ANNOTATE_V4_T>() + (arr.Length - 1));
                Marshal.StructureToPtr(strV4, ptrV4, false);

                try
                {
                    MMALCheck(MMALPort.mmal_port_parameter_set(camera.Control.Ptr, (MMAL_PARAMETER_HEADER_T *)ptrV4),
                              "Unable to set annotate");
                }
                catch
                {
                    Marshal.FreeHGlobal(ptrV4);
                    ptrV4 = IntPtr.Zero;

                    MMALLog.Logger.LogWarning("Unable to set V4 annotation structure. Trying V3. Please update firmware to latest version.");

                    var str = new MMAL_PARAMETER_CAMERA_ANNOTATE_V3_T(
                        new MMAL_PARAMETER_HEADER_T(MMAL_PARAMETER_ANNOTATE, Marshal.SizeOf <MMAL_PARAMETER_CAMERA_ANNOTATE_V3_T>() + (arr.Length - 1)),
                        1, showShutter, showAnalogGain, showLens,
                        showCaf, showMotion, showFrame, enableTextBackground,
                        customBackgroundColor, customBackgroundY, customBackgroundU, customBackgroundV, 0, customTextColor,
                        customTextY, customTextU, customTextV, textSize, arr);

                    var ptr = Marshal.AllocHGlobal(Marshal.SizeOf <MMAL_PARAMETER_CAMERA_ANNOTATE_V3_T>() + (arr.Length - 1));
                    Marshal.StructureToPtr(str, ptr, false);

                    try
                    {
                        MMALCheck(MMALPort.mmal_port_parameter_set(camera.Control.Ptr, (MMAL_PARAMETER_HEADER_T *)ptr), "Unable to set annotate V3.");
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(ptr);
                    }
                }
                finally
                {
                    if (ptrV4 != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(ptrV4);
                    }
                }
            }
        }
Exemple #2
0
        internal static void SetAnnotateSettings(this MMALCameraComponent camera)
        {
            if (MMALCameraConfig.Annotate != null)
            {
                MMALLog.Logger.Debug("Setting annotate");

                var sb = new StringBuilder();

                var showShutter           = 0;
                var showAnalogGain        = 0;
                var showLens              = 0;
                var showCaf               = 0;
                var showMotion            = 0;
                var showFrame             = 0;
                var enableTextBackground  = 0;
                var textSize              = (byte)0;
                var customTextColor       = 0;
                var customTextY           = (byte)0;
                var customTextU           = (byte)0;
                var customTextV           = (byte)0;
                var customBackgroundColor = 0;
                var customBackgroundY     = (byte)0;
                var customBackgroundU     = (byte)0;
                var customBackgroundV     = (byte)0;
                var justify               = MMALCameraConfig.Annotate.Justify;
                var xOffset               = MMALCameraConfig.Annotate.XOffset;
                var yOffset               = MMALCameraConfig.Annotate.YOffset;

                if (!string.IsNullOrEmpty(MMALCameraConfig.Annotate.CustomText))
                {
                    sb.Append(MMALCameraConfig.Annotate.CustomText + " ");
                }

                if (MMALCameraConfig.Annotate.ShowTimeText)
                {
                    sb.Append(DateTime.Now.ToString("HH:mm") + " ");
                }

                if (MMALCameraConfig.Annotate.ShowDateText)
                {
                    sb.Append(DateTime.Now.ToString("dd/MM/yyyy") + " ");
                }

                if (MMALCameraConfig.Annotate.ShowShutterSettings)
                {
                    showShutter = 1;
                }

                if (MMALCameraConfig.Annotate.ShowGainSettings)
                {
                    showAnalogGain = 1;
                }

                if (MMALCameraConfig.Annotate.ShowLensSettings)
                {
                    showLens = 1;
                }

                if (MMALCameraConfig.Annotate.ShowCafSettings)
                {
                    showCaf = 1;
                }

                if (MMALCameraConfig.Annotate.ShowMotionSettings)
                {
                    showMotion = 1;
                }

                if (MMALCameraConfig.Annotate.ShowFrameNumber)
                {
                    showFrame = 1;
                }

                if (MMALCameraConfig.Annotate.AllowCustomBackgroundColour)
                {
                    enableTextBackground = 1;
                }

                textSize = Convert.ToByte(MMALCameraConfig.Annotate.TextSize);

                if (MMALCameraConfig.Annotate.TextColour != Color.Empty)
                {
                    customTextColor = 1;

                    var yuv = MMALColor.RGBToYUVBytes(MMALCameraConfig.Annotate.TextColour);
                    customTextY = yuv.Item1;
                    customTextU = yuv.Item2;
                    customTextV = yuv.Item3;
                }

                if (MMALCameraConfig.Annotate.BgColour != Color.Empty)
                {
                    customBackgroundColor = 1;
                    var yuv = MMALColor.RGBToYUVBytes(MMALCameraConfig.Annotate.BgColour);
                    customBackgroundY = yuv.Item1;
                    customBackgroundU = yuv.Item2;
                    customBackgroundV = yuv.Item3;
                }

                string t = sb.ToString() + char.MinValue;

                var text = Encoding.ASCII.GetBytes(t);

                var strV4 = new MMAL_PARAMETER_CAMERA_ANNOTATE_V4_T(
                    new MMAL_PARAMETER_HEADER_T(MMAL_PARAMETER_ANNOTATE, Marshal.SizeOf <MMAL_PARAMETER_CAMERA_ANNOTATE_V4_T>() + t.Length),
                    1, showShutter, showAnalogGain, showLens,
                    showCaf, showMotion, showFrame, enableTextBackground,
                    customBackgroundColor, customBackgroundY, customBackgroundU, customBackgroundV, 0, customTextColor,
                    customTextY, customTextU, customTextV, textSize, text, (int)justify, xOffset, yOffset);

                var ptrV4 = Marshal.AllocHGlobal(Marshal.SizeOf <MMAL_PARAMETER_CAMERA_ANNOTATE_V4_T>());
                Marshal.StructureToPtr(strV4, ptrV4, false);

                try
                {
                    MMALCheck(MMALPort.mmal_port_parameter_set(camera.Control.Ptr, (MMAL_PARAMETER_HEADER_T *)ptrV4),
                              "Unable to set annotate");
                }
                catch
                {
                    Marshal.FreeHGlobal(ptrV4);
                    ptrV4 = IntPtr.Zero;

                    MMALLog.Logger.Warn("Unable to set V4 annotation structure. Trying V3. Please update firmware to latest version.");

                    var str = new MMAL_PARAMETER_CAMERA_ANNOTATE_V3_T(
                        new MMAL_PARAMETER_HEADER_T(MMAL_PARAMETER_ANNOTATE, Marshal.SizeOf <MMAL_PARAMETER_CAMERA_ANNOTATE_V3_T>() + t.Length),
                        1, showShutter, showAnalogGain, showLens,
                        showCaf, showMotion, showFrame, enableTextBackground,
                        customBackgroundColor, customBackgroundY, customBackgroundU, customBackgroundV, 0, customTextColor,
                        customTextY, customTextU, customTextV, textSize, text);

                    var ptr = Marshal.AllocHGlobal(Marshal.SizeOf <MMAL_PARAMETER_CAMERA_ANNOTATE_V3_T>());
                    Marshal.StructureToPtr(str, ptr, false);

                    try
                    {
                        MMALCheck(MMALPort.mmal_port_parameter_set(camera.Control.Ptr, (MMAL_PARAMETER_HEADER_T *)ptr), "Unable to set annotate V3.");
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(ptr);
                    }
                }
                finally
                {
                    if (ptrV4 != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(ptrV4);
                    }
                }
            }
        }