Windows平台RTMP播放器/RTSP播放器如何在播放窗口添加OSD文字叠加
生活随笔
收集整理的這篇文章主要介紹了
Windows平台RTMP播放器/RTSP播放器如何在播放窗口添加OSD文字叠加
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
好多開發者在做Windows平臺特別是單屏多畫面顯示時,希望像監控攝像機一樣,可以在播放畫面添加OSD臺標,以實現字符疊加效果,大多開發者可很輕松的實現以上效果,針對此,本文以大牛直播SDK (Github)的Windows平臺demo為例,簡單介紹下具體實現:
Windows平臺RTMP播放器、RTSP播放器C++ demo
Windows平臺C++的demo,以錄像過程為例,動態在左上角顯示個閃動的圖標+當前時間,具體效果如下:
核心代碼
std::shared_ptr<nt_argb_image_logo> CSmartPlayerDlg::MakeLogo() {std::shared_ptr<nt_argb_image_logo> logo_image;if (!is_init_gdi_plus_ok_)return logo_image;if (!recoder_image_){static bool is_load_image_failed = false;if (!is_load_image_failed){recoder_image_.reset(Gdiplus::Image::FromFile(_T("red_circle.png")));if (recoder_image_ && Gdiplus::Ok != recoder_image_->GetLastStatus()){is_load_image_failed = true;recoder_image_.reset();}}}is_has_recoder_image_ = !is_has_recoder_image_;if (!recoder_image_){is_has_recoder_image_ = false;}if (m_hWnd == nullptr || !::IsWindow(m_hWnd))return logo_image;if (cur_logo_font_name_.empty()){cur_logo_font_name_ = FindLogoFontName();}if (cur_logo_font_name_.empty()){return logo_image;}Gdiplus::FontFamily font_family(cur_logo_font_name_.c_str());if (!font_family.IsAvailable()){return logo_image;}Gdiplus::Font font(&font_family, 10, Gdiplus::FontStyleBold, Gdiplus::Unit::UnitPoint);if (!font.IsAvailable()){return logo_image;}// 白色Gdiplus::SolidBrush solid_brush(Gdiplus::Color(255, 255, 255));Gdiplus::Graphics graphics(m_hWnd);if (Gdiplus::Ok != graphics.GetLastStatus()){return logo_image;}int recoder_image_w = 18;int recoder_image_h = 18;if (recoder_image_){recoder_image_w = recoder_image_->GetWidth();recoder_image_h = recoder_image_->GetHeight();}auto image_w = recoder_image_w + 2 + 5;auto image_h = recoder_image_h + 5 + 5;graphics.SetTextRenderingHint(Gdiplus::TextRenderingHint::TextRenderingHintClearTypeGridFit);auto cur_time_str = MakeCurTimerStr();Gdiplus::RectF bounding_box(0, 0, 0, 0);graphics.MeasureString(cur_time_str.c_str(), -1, &font, Gdiplus::PointF(0, 0), &bounding_box);Gdiplus::SizeF text_size(0, 0);bounding_box.GetSize(&text_size);image_w += (int)text_size.Width;image_h = image_h > ((int)text_size.Height) ? image_h : ((int)text_size.Height);image_w += 2;image_h += 2;image_w = ByteAlign(image_w, 4);image_h = ByteAlign(image_h, 4);Gdiplus::Bitmap bitmap(image_w, image_h, PixelFormat32bppARGB);if (Gdiplus::Ok != bitmap.GetLastStatus()){return logo_image;}Gdiplus::Graphics g(&bitmap);if (Gdiplus::Ok != g.GetLastStatus()){return logo_image;}int r_left = 2;int r_top = (image_h / 2) - (recoder_image_h / 2);r_top -= 1;if (is_has_recoder_image_){g.DrawImage(recoder_image_.get(), r_left, r_top);}r_left += recoder_image_w;r_left += 5;r_top = (image_h / 2) - (text_size.Height / 2);g.DrawString(cur_time_str.c_str(), -1, &font, Gdiplus::PointF(r_left, r_top), &solid_brush);Gdiplus::BitmapData locked_bitmapData;if (Gdiplus::Ok == bitmap.LockBits(nullptr, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &locked_bitmapData)){auto buffer_size = locked_bitmapData.Stride * locked_bitmapData.Height;std::unique_ptr<NT_BYTE[]> buffer(new NT_BYTE[buffer_size]);if (buffer){logo_image = std::make_shared<nt_argb_image_logo>(locked_bitmapData.Width, locked_bitmapData.Height);logo_image->stride_ = locked_bitmapData.Stride;memcpy(buffer.get(), locked_bitmapData.Scan0, buffer_size);logo_image->data_.swap(buffer);}bitmap.UnlockBits(&locked_bitmapData);}return logo_image; }Windows平臺RTMP播放器、RTSP播放器C# demo
Windows平臺C#的demo,添加了“設置臺標”選擇框,在player窗口左上角顯示“疊加字符展示”,具體內容、坐標可自定義,具體效果如下:
核心代碼
//設置OSD文本private void DrawOSD(string draw_text){// gdi 繪制的話,文本請自己繪制if (is_gdi_render_)return;if (player_handle_ == IntPtr.Zero)return;if (draw_text == null || draw_text.Length < 1){NTSmartPlayerSDK.NT_SP_SetRenderARGBLogo(player_handle_, IntPtr.Zero, 0, 0, 0, 0, 0, 0, 0);return;}Graphics graphics = this.CreateGraphics();SolidBrush solid_brush = new SolidBrush(Color.FromArgb(255, 255, 255));graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;SizeF text_size = new SizeF();text_size = graphics.MeasureString(draw_text, this.Font);int image_w = (int)text_size.Width + 4;int image_h = (int)text_size.Height + 4;image_w = (int)ByteAlign((UInt32)image_w, 4);image_h = (int)ByteAlign((UInt32)image_h, 4);Bitmap bmp = new Bitmap(image_w, image_h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);Graphics g = Graphics.FromImage(bmp);g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;float left = image_w / 2 - text_size.Width / 2;float top = image_h / 2 - text_size.Height / 2;g.DrawString(draw_text, this.Font, solid_brush, left, top);Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);System.Drawing.Imaging.BitmapData bmp_data = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);IntPtr ptr = bmp_data.Scan0;int strdie = Math.Abs(bmp_data.Stride);NTSmartPlayerSDK.NT_SP_SetRenderARGBLogo(player_handle_, ptr, strdie, bmp_data.Width,bmp_data.Height, 6, 6, bmp_data.Width, bmp_data.Height);// Unlock the bits.bmp.UnlockBits(bmp_data);}}注意,如果GDI模式下,我們數據回調到上層繪制的,這樣加起來更簡單:
if (btn_check_add_osd.Checked){string draw_text = "疊加字符展示";Graphics graphics = this.CreateGraphics();SolidBrush solid_brush = new SolidBrush(Color.FromArgb(255, 255, 255));graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;float left = playWnd.Left + 4;float top = playWnd.Top + 4;g.DrawString(draw_text, this.Font, solid_brush, left, top);}感興趣的開發者可自行嘗試。
總結
以上是生活随笔為你收集整理的Windows平台RTMP播放器/RTSP播放器如何在播放窗口添加OSD文字叠加的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【机器学习】浅析机器学习各大算法的适用场
- 下一篇: win8无线网络受限怎么办 win8网络