Пример #1
0
        internal static void SetColourFx(this MmalCameraComponent camera, ColorEffects colorFx)
        {
            MmalLog.Logger.LogDebug("Setting colour effects");

            var(_, u, v) = MmalColor.RgbToYuvBytes(colorFx.Color);

            var colFx = new MmalParameterColorFxType(
                new MmalParameterHeaderType(MmalParameterColorEffect, Marshal.SizeOf <MmalParameterColorFxType>()),
                colorFx.Enable ? 1 : 0,
                u,
                v);

            MmalCheck(MmalPort.SetParameter(camera.Control.Ptr, &colFx.Hdr), "Unable to set colour fx");
        }
Пример #2
0
        internal static void SetAnnotateSettings(this MmalCameraComponent camera)
        {
            if (CameraConfig.Annotate == null)
            {
                return;
            }

            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 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               = CameraConfig.Annotate.Justify;
            var xOffset               = CameraConfig.Annotate.XOffset;
            var yOffset               = CameraConfig.Annotate.YOffset;

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

            if (CameraConfig.Annotate.ShowDateText)
            {
                sb.Append(DateTime.Now.ToString(CameraConfig.Annotate.DateFormat) + " ");
            }

            if (CameraConfig.Annotate.ShowTimeText)
            {
                sb.Append(DateTime.Now.ToString(CameraConfig.Annotate.TimeFormat) + " ");
            }

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

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

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

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

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

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

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

            var textSize = Convert.ToByte(CameraConfig.Annotate.TextSize);

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

                var(y, u, v) = MmalColor.RgbToYuvBytes(CameraConfig.Annotate.TextColour);
                customTextY  = y;
                customTextU  = u;
                customTextV  = v;
            }

            if (CameraConfig.Annotate.BgColour != Color.Empty)
            {
                customBackgroundColor = 1;
                var(y, u, v)          = MmalColor.RgbToYuvBytes(CameraConfig.Annotate.BgColour);
                customBackgroundY     = y;
                customBackgroundU     = u;
                customBackgroundV     = v;
            }

            // .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[MmalCameraAnnotateMaxTextLenV3];
            var bytes = Encoding.ASCII.GetBytes(text);

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

            var strV4 = new MmalParameterCameraAnnotateV4Type(
                new MmalParameterHeaderType(MmalParameterAnnotate, Marshal.SizeOf <MmalParameterCameraAnnotateV4Type>() + (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 <MmalParameterCameraAnnotateV4Type>() + (arr.Length - 1));

            Marshal.StructureToPtr(strV4, ptrV4, false);

            try
            {
                MmalCheck(MmalPort.SetParameter(camera.Control.Ptr, (MmalParameterHeaderType *)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 MmalParameterCameraAnnotateV3Type(
                    new MmalParameterHeaderType(MmalParameterAnnotate, Marshal.SizeOf <MmalParameterCameraAnnotateV3Type>() + (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 <MmalParameterCameraAnnotateV3Type>() + (arr.Length - 1));
                Marshal.StructureToPtr(str, ptr, false);

                try
                {
                    MmalCheck(MmalPort.SetParameter(camera.Control.Ptr, (MmalParameterHeaderType *)ptr), "Unable to set annotate V3.");
                }
                finally
                {
                    Marshal.FreeHGlobal(ptr);
                }
            }
            finally
            {
                if (ptrV4 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptrV4);
                }
            }
        }