agsh / onvif

ONVIF node.js implementation
http://agsh.github.io/onvif/
MIT License
679 stars 230 forks source link

Ability to set font size and color in setOSD and createOSD methods. #326

Closed RotemDoar closed 3 weeks ago

RotemDoar commented 3 weeks ago

I implemented the capability to adjust font size and color within both the 'setOSD' and 'createOSD' methods.

While working, I recognized the necessity to customize font attributes like size and color in the screen display.

Upon reviewing the Onvif MediaBinding spec (both media and media2 versions) - https://www.onvif.org/ver10/media/wsdl/media.wsdl https://www.onvif.org/ver20/media/wsdl/media.wsdl I found that in the setOSD and createOSD function it is possible to set the font color and size.

To adding the ability, I introduced new parameters to the method options for setting font size and color in the 'setOSD' and 'createOSD' functions defined in media.js file:

* @param {number} [options.fontSize] The text font size.
* @param {object} [options.fontColor] The color of the text font (OSDColor), should be object with properties - X, Y, Z.

And in the request body as part of the TextString I added :

${
    options.fontSize ?
        `<sch:FontSize>${options.fontSize}</sch:FontSize>` 
         : ''
}               
${
    options.fontColor ? 
        `
        <sch:FontColor>
        ${'<sch:Color Z="' + options.fontColor.Z + '" Y="' + options.fontColor.Y + '" X="' + options.fontColor.X + '"/>'}
                </sch:FontColor>`
        : ''
}

Example of request options while I run the changes for one of my cameras -

{
          videoSourceConfigurationToken: "VideoSourceConfigToken-01-0",
          OSDToken: "OSDDateTimeToken-0",
          position: "LowerLeft",
          timeFormat: "HH:mm:ss",
          dateFormat: "YYYY-MM-DD",
          fontSize: 1,
          fontColor: {
            X: 82,
            Y: 90,
            Z: 240,
          }
} 

As a result the font color configuration (X,Y,Z) changed the OSD color to red with minimum size of the font .

Moreover, I enhanced the 'create OSD' method to include the functionality of setting custom position, date, and time, aligning with the capabilities offered in the 'set OSD' method.

RotemDoar commented 3 weeks ago

@agsh

I have added an optional parameter - 'colorspace', allowing support for both YCbCr and RGB. The default setting is RGB. (see line 1259 in createOSD and line 1334 in setOSD).

* @param {string} [options.colorspace] Colorspace - RGB or YCbCr. Default RGB.

and fix the request for the support (with extra validation to the parameters X,Y,Z) - (line 1304 and 1369)

${ options.fontColor && options.fontColor.X && options.fontColor.Y && options.fontColor.Z ? 
        `
        <sch:FontColor>
        ${ '<sch:Color Z="' + options.fontColor.Z + '" Y="' + options.fontColor.Y + '" X="' + options.fontColor.X + '" Colorspace="' + `${ options.colorspace === "YCbCr" ? "http://www.onvif.org/ver10/colorspace/YCbCr" : "http://www.onvif.org/ver10/colorspace/RGB" }` + '"/>' }
        </sch:FontColor>` : "" }

I added extra documentation to the font color parameter.

* @param {object} [options.fontColor] The color of the text font (OSDColor), should be object with properties - X, Y, Z.
* @param {float} [options.fontColor.X] For RGB means R value, For YCbCr means Y value.
* @param {float} [options.fontColor.Y] For RGB means G value, For YCbCr means Cb value.
* @param {float} [options.fontColor.Z] For RGB means B value, For YCbCr means Cr value.

I added example to the parameters for createOSD method.

* await cam.createOSD({
   *           position: "LowerLeft",
   *           timeFormat: "HH:mm:ss",
   *           dateFormat: "YYYY-MM-DD",
   *           fontSize: 1,
   *           colorspace: "RGB",
   *           fontColor: {
   *             X: 1,
   *             Y: 0.7,
   *             Z: 0.9,
   *           }
   * });

I added the link to create osd method in the setOSD method.

* @see {Cam~createOSD}

agsh commented 3 weeks ago

@RotemDoar Great, thanks! Nice and detailed comments :smile_cat: