Windows平台RTMP推送|轻量级RTSP服务摄像头如何添加动态文字水印

本文涉及的产品
视觉智能开放平台,图像资源包5000点
视觉智能开放平台,分割抠图1万点
视觉智能开放平台,视频资源包5000点
简介: 本文介绍了在Windows平台上实现摄像头或屏幕流中动态文字水印的技术方法。通过大牛直播SDK示例,展示了如何从文本获取RGB数据,并将其叠加到视频流上。文中提供了代码片段来说明如何开启文字水印、生成包含实时信息的位图、以及如何更新和控制图层。最终实现了动态显示时间和位置信息的需求。对这一领域的开发者而言,本文提供了实用的参考与指导。

技术背景

我们在做Windows平台RTMP推送、轻量级RTSP服务的时候,遇到过这样的技术需求,除了常规的png图片水印外,开发者希望能在桌面或摄像头上,叠加上实时时间和位置信息,并保存到图像里。

技术实现

本文以大牛直播SDK的摄像头采集+动态文字水印为例,谈谈如何实现的,简单来说,这块分两步,第一步,如何从文字里面获取到rgb数据,第二步,如何吧rgb数据叠加到摄像头上?

废话不多说,先上图,选中采集摄像头和摄像头添加文字水印,如果需要默认打开动态文字水印,直接打开即可,如需关闭,随时可以关闭或二次打开:

image.gif

本文以启动个轻量级RTSP服务为例,效果如下,可以清楚的看到右侧播放端,显示实时更新的文字信息(更新间隔,可以自行设置):

image.gif

打开摄像头添加文字水印:

private void btn_text_osd_Click(object sender, EventArgs e)
        {
            if (btn_text_osd.Text.Equals("打开动态文字水印"))
            {
                if (!timer_clock_.Enabled)
                {
                    timer_clock_.Enabled = true;
                }
                enable_layer(get_text_layer_index(), true);
                btn_text_osd.Text = "关闭动态文字水印";
                is_enable_text_watermarker_ = true;
            }
            else
            {
                if (!is_enable_publish_random_userdata_)
                {
                    timer_clock_.Enabled = false;
                }
                enable_layer(get_text_layer_index(), false);
                btn_text_osd.Text = "打开动态文字水印";
                is_enable_text_watermarker_ = false;
            }
        }

image.gif

添加文字水印图层:

private async void AddWaterMarkerLayer()
        {
            Bitmap bitmap = null;
            try
            {
                string format = "yyyy-MM-dd HH:mm:ss.fff";
                StringBuilder sb = new StringBuilder();
                sb.Append("施工单位:上海视沃信息科技有限公司");
                sb.Append("\r\n");
                sb.Append("施工时间:");
                sb.Append(DateTime.Now.DayOfWeek.ToString());
                sb.Append(" ");
                sb.Append(DateTime.Now.ToString(format));
                sb.Append("\r\n");
                sb.Append("当前位置:上海市");
                string str = sb.ToString();
                bitmap = GenerateBitmap(str);
            }
            catch (Exception)
            {
                return;
            }
            if (null == bitmap)
                return;
            int x = 0;
            int y = 200;
            UpdateLayerRegion(get_text_layer_index(), x, y, bitmap);
            enable_layer(get_text_layer_index(), true);
            await Task.Delay(30);
            post_argb8888_layer_image(get_text_layer_index(), bitmap);
        }

image.gif

投递图层数据:

/*
         * SmartPublisherDemoDlg.cs
         * Author: daniusdk.com
         */
        public void post_argb8888_layer_image(int index, Bitmap bitmap)
        {
            if (index < 0 || null == bitmap)
                return;
            if (!is_running())
                return;
            Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            System.Drawing.Imaging.BitmapData bmp_data = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
            IntPtr ptr = bmp_data.Scan0;
            int stride = bmp_data.Stride;
            if (stride < 0)
                stride = -stride;
            NT_PB_Image pb_image = new NT_PB_Image();
            pb_image.format_ = (int)NT.NTSmartPublisherDefine.NT_PB_E_IMAGE_FORMAT.NT_PB_E_IMAGE_FORMAT_ARGB;
            pb_image.width_ = bmp_data.Width;
            pb_image.height_ = bmp_data.Height;
            pb_image.timestamp_ = 0;
            pb_image.cb_size_ = (UInt32)Marshal.SizeOf(pb_image);
            pb_image.stride_ = new Int32[NTSmartPublisherDefine.NT_PB_IMAGE_MAX_PLANE_NUM];
            pb_image.stride_[0] = stride;
            pb_image.plane_size_ = new Int32[NTSmartPublisherDefine.NT_PB_IMAGE_MAX_PLANE_NUM];
            pb_image.plane_size_[0] = pb_image.stride_[0] * bmp_data.Height;
            pb_image.plane_ = new IntPtr[NTSmartPublisherDefine.NT_PB_IMAGE_MAX_PLANE_NUM];
            pb_image.plane_[0] = ptr;
            for (int i = 1; i < NTSmartPublisherDefine.NT_PB_IMAGE_MAX_PLANE_NUM; ++i)
            {
                pb_image.stride_[i] = 0;
                pb_image.plane_size_[i] = 0;
                pb_image.plane_[i] = IntPtr.Zero;
            }
            IntPtr image_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(pb_image));
            Marshal.StructureToPtr(pb_image, image_ptr, false);
            if (is_running())
            {
                if (shared_lock_.TryEnterReadLock(enter_read_lock_timeout_ms_))
                {
                    try
                    {
                        if (is_running())
                            NTSmartPublisherSDK.NT_PB_PostLayerImage(publisher_handle_, 0, index, image_ptr, 0, IntPtr.Zero);
                    }
                    finally
                    {
                        shared_lock_.ExitReadLock();
                    }
                }
            }
            Marshal.FreeHGlobal(image_ptr);
            bitmap.UnlockBits(bmp_data);
        }

image.gif

再说如何实现两个图层:

if (btn_check_camera_input_.Checked && btn_add_osd_to_camera_.Checked)
{
                    if (-1 != cur_sel_camera_index_
                           && -1 != cur_sel_camera_resolutions_index_
                           && -1 != cur_sel_camera_frame_rate_index_)
                    {
                        NT_PB_CameraLayerConfigV2 camera_layer_c0 = new NT_PB_CameraLayerConfigV2();
                        CameraInfo camera = cameras_[cur_sel_camera_index_];
                        NT_PB_VideoCaptureCapability cap = camera.capabilities_[cur_sel_camera_resolutions_index_];
                        camera_layer_c0.device_unique_id_utf8_ = camera.id_;
                        type = (Int32)NTSmartPublisherDefine.NT_PB_E_LAYER_TYPE.NT_PB_E_LAYER_TYPE_CAMERA;
                        fill_layer_base(camera_layer_c0, out camera_layer_c0.base_, type, index, true, 0, 0, cap.width_, cap.height_);
                        camera_layer_index_ = camera_layer_c0.base_.index_;
                        if (btn_check_flip_horizontal_camera_.Checked)
                        {
                            camera_layer_c0.is_flip_horizontal_ = 1;
                        }
                        else
                        {
                            camera_layer_c0.is_flip_horizontal_ = 0;
                        }
                        if (btn_check_flip_vertical_camera_.Checked)
                        {
                            camera_layer_c0.is_flip_vertical_ = 1;
                        }
                        else
                        {
                            camera_layer_c0.is_flip_vertical_ = 0;
                        }
                        // 这种叠加模式下不要旋转,否则变形厉害, 要么就定好一个角度,调整宽高,但不要动态旋转
                        camera_layer_c0.rotate_degress_ = 0;
                        if (add_layer_config(camera_layer_c0, type))
                            index++;
                        //第二层:文字水印
                        //叠加的文本层
                        NT_PB_ExternalVideoFrameLayerConfig text_layer = new NT_PB_ExternalVideoFrameLayerConfig();
                        type = (Int32)NTSmartPublisherDefine.NT_PB_E_LAYER_TYPE.NT_PB_E_LAYER_TYPE_EXTERNAL_VIDEO_FRAME;
                        fill_layer_base(text_layer, out text_layer.base_, type, index, true, 0, 0, 64, 64);
                        if (add_layer_config(text_layer, type))
                            text_layer_index_ = index++;
                    }
                    NTSmartPublisherSDK.NT_PB_SetFrameRate(publisher_handle_, (uint)(cur_sel_camera_frame_rate_index_ + 1));
}

image.gif

如果需要更新图层位置:

public bool update_layer_region(int index, int x, int y, int w, int h)
        {
            if (index < 1)
                return false;
            if (is_empty_handle())
                return false;
            NT_PB_RectRegion region = new NT_PB_RectRegion();
            region.x_ = x;
            region.y_ = y;
            region.width_ = w;
            region.height_ = h;
            return NTBaseCodeDefine.NT_ERC_OK == NTSmartPublisherSDK.NT_PB_UpdateLayerRegion(publisher_handle_, 0, index, ref region);
        }

image.gif

动态打开关闭图层:

public bool enable_layer(int index, bool enable)
        {
            if (index < 1)
                return false;
            if (is_empty_handle())
                return false;
            return NTBaseCodeDefine.NT_ERC_OK == NTSmartPublisherSDK.NT_PB_EnableLayer(publisher_handle_, 0, index, enable ? 1 : 0);
        }

image.gif

总结

Windows平台添加动态文字水印,首先确保从文字拿到rgb数据,然后,设置两个图层,摄像头或者屏幕数据,作为底层,上层添加文字图层,如果需要实时更新,有个定制器,刷新即可,感兴趣的开发者,可以单独和我交流。

相关文章
|
1月前
|
监控 Windows
Windows平台RTSP|RTMP播放器如何实时调节音量
我们在做Windows平台RTSP、RTMP播放器的时候,有这样的技术需求,特别是多路监控的时候,并不是每一路audio都需要播放出来的,所以,这时候,需要有针对音量调节的设计
|
7天前
|
NoSQL Shell MongoDB
Windows 平台安装 MongoDB
10月更文挑战第10天
15 0
Windows 平台安装 MongoDB
|
1月前
|
监控 C# 块存储
Windows平台RTSP|RTMP播放器如何叠加OSD文字
做Windows平台RTSP|RTMP播放器的时候,特别是多路播放场景下,开发者希望可以给每一路RTSP或RTMP流添加个额外的OSD台标,以区分不同的设备信息(比如添加摄像头所在位置),本文主要探讨,如何动态添加OSD台标。
Windows平台RTSP|RTMP播放器如何叠加OSD文字
|
1月前
|
Linux Android开发 iOS开发
Windows平台RTSP|RTMP播放器如何实现实时录像功能
Windows平台RTSP、RTMP播放器实时录像接口设计,实际上,除了Windows平台,我们Linux、Android、iOS平台也是一样的设计,单纯的录像模块,如果做的全面,也不是一两个接口可以搞定的
|
19天前
|
并行计算 开发工具 异构计算
在Windows平台使用源码编译和安装PyTorch3D指定版本
【10月更文挑战第6天】在 Windows 平台上,编译和安装指定版本的 PyTorch3D 需要先安装 Python、Visual Studio Build Tools 和 CUDA(如有需要),然后通过 Git 获取源码。建议创建虚拟环境以隔离依赖,并使用 `pip` 安装所需库。最后,在源码目录下运行 `python setup.py install` 进行编译和安装。完成后即可在 Python 中导入 PyTorch3D 使用。
|
消息中间件 Windows
在Windows系统上实现轻量级的线程间及进程间消息队列
Windows没有message queue累世的IPC内核对象,使得在在处理IPC时少了一种传递消息的手段。利用Windows的Naming Object可以实现一套简单的Inter-Thread消息队列。
1247 0
|
13天前
|
边缘计算 安全 网络安全
|
6天前
|
数据安全/隐私保护 Windows
安装 Windows Server 2019
安装 Windows Server 2019
|
9天前
|
网络协议 Windows
Windows Server 2019 DHCP服务器搭建
Windows Server 2019 DHCP服务器搭建
|
9天前
|
网络协议 定位技术 Windows
Windows Server 2019 DNS服务器搭建
Windows Server 2019 DNS服务器搭建