C# 海康威視網絡半球攝像頭回調YV12取畫面


  海康網絡攝像頭回調取畫面,網口最好用千兆的網卡來做,開始用筆記本的百兆網口,不管怎么優化都是卡頓的,

后來用千兆網卡台式機的,基本就沒有卡頓了,取圖再加上運動檢測處理,基本上十幾毫秒每幀。

      用回調方式處理數據流方式,參見官方的Demo,本文只介紹相關的回調YV12取圖,回調中的圖像數據是龐大的,

在回調中只適合做簡單的處理,如果處理過於復雜,會出現卡頓的現象。因為我只需要對圖像進行運動檢測處理,所以只是

簡單取了圖像的灰度圖,具體如下:

//回調函數
private
void DecCallbackFUN(int nPort, IntPtr pBuf, int nSize, ref PlayCtrl.FRAME_INFO pFrameInfo, int nReserved1, int nReserved2) { // 將pBuf解碼后視頻輸入寫入文件中(解碼后YUV數據量極大,尤其是高清碼流,不建議在回調函數中處理) if (pFrameInfo.nType == 3) //#define T_YV12 3 { int m_Width = pFrameInfo.nWidth; int m_Height = pFrameInfo.nHeight; //this.MeasureTime(() => { var img = GetBitmapFromYV12(pBuf, m_Width, m_Height, nSize); NewFrame?.Invoke((Bitmap)img.Clone()); img.Dispose(); // }); } }

///把YV12數據轉為BGR24數據,我只關心灰度圖,因此只取了Y數據,如果想獲取彩色圖像,考慮計算UV數據
private unsafe bool YV12ToBGR24(byte* pYUV, byte* pBGR24, int width, int height) { if (width < 1 || height < 1 || pYUV == null || pBGR24 == null) return false; byte* yData = pYUV; int yIdx; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { yIdx = i * width + j; pBGR24[yIdx * 3] = pBGR24[yIdx * 3 + 1] = pBGR24[yIdx * 3 + 2] = yData[yIdx]; } } return true; }

///獲取回調的圖片
private unsafe Bitmap GetBitmapFromYV12(IntPtr pBuf, int width, int height, int nSize) { Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat); byte* desPtr = (byte*)bmpData.Scan0; byte* srcPtr = (byte*)pBuf; YV12ToBGR24(srcPtr, desPtr, width, height); bitmap.UnlockBits(bmpData); return bitmap; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM