C#:使用海康SDK解码回调函数DecCallbackFUN()
2021-06-28 13:09
标签:length 数据转换 方式 def 颜色 http 分享 参考 ast 使用海康摄像头采集图像时,需要在图像上添加图标、文字等额外数据。可选择使用海康SDK绘图回调函数叠加字符、图像等(请参考上一篇文章);也可使用海康SDK的解码回调函数,对视频流数据进行解码后处理。该方法流程为:调用视频预览函数NET_DVR_RealPlay_V40()时将第三个参数设置为实时数据回调函数RealDataCallBack()的函数指针,然后在RealDataCallBack()回调函数中注册视频流数据解码函数DecCallbackFUN(),最后在DecCallbackFUN()函数中对数据解析解码、叠加字符等处理。 注:得到的m_img用于PictureBox进行显示。 C#:使用海康SDK解码回调函数DecCallbackFUN() 标签:length 数据转换 方式 def 颜色 http 分享 参考 ast 原文地址:http://blog.51cto.com/weiyuqingcheng/2323546
本文只调用解码回调函数将YV12格式视频流数据转换为RGB32格式数据,然后将RGB32格式数据转换为Image,并使用PictrueBox显示该Image数据,暂不做叠加字符、图像等处理。代码
1、摄像头操作代码
struct CameraInfo
{
public string strIP;
public short nPort;
public string strUserName;
public string strPassword;
}
class IDeviceCamera
{
public Image m_img = null;
public virtual bool InitCamera( CameraInfo stInfo )
{
return true;
}
}
class DeviceCamera : IDeviceCamera
{
private CameraInfo m_stCameraInfo;
private bool m_bInitSDK = false;
private Int32 m_lUserID = -1;
private Int32 m_lRealHandle = -1;
private Int32 m_lPort = -1;
CHCNetSDK.REALDATACALLBACK RealData = null; //必须得定义为成员变量
public override bool InitCamera( CameraInfo stInfo )
{
m_stCameraInfo = stInfo;
m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if ( !m_bInitSDK )
{
uint nError = CHCNetSDK.NET_DVR_GetLastError();
}
CHCNetSDK.NET_DVR_SetConnectTime( 5000, 1 );
CHCNetSDK.NET_DVR_SetReconnect( 10000, 1 );
if ( m_bInitSDK == false )
{
MessageBox.Show( "NET_DVR_Init error!" );
return false;
}
else
{
//保存SDK日志 To save the SDK log
CHCNetSDK.NET_DVR_SetLogToFile( 3, "C:\\SdkLog\\", true );
}
string DVRIPAddress = stInfo.strIP; //设备IP地址或者域名 Device IP
Int16 DVRPortNumber = stInfo.nPort; //设备服务端口号 Device Port
string DVRUserName = stInfo.strUserName;//设备登录用户名 User name to login
string DVRPassword = stInfo.strPassword;//设备登录密码 Password to login
CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
m_lUserID = CHCNetSDK.NET_DVR_Login_V30( DVRIPAddress, DVRPortNumber, DVRUserName, DVRPassword, ref DeviceInfo );
if ( m_lUserID 0 )
{
if ( m_lPort >= 0 )
return; //同一路码流不需要多次调用开流接口
//获取播放句柄 Get the port to play
if ( !PlayCtrl.PlayM4_GetPort( ref m_lPort ) )
{
nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
break;
}
//设置流播放模式 Set the stream mode: real-time stream mode
if ( !PlayCtrl.PlayM4_SetStreamOpenMode( m_lPort, PlayCtrl.STREAME_REALTIME ) )
{
nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
//str = "Set STREAME_REALTIME mode failed, error code= " + nLastErr;
//this.BeginInvoke( AlarmInfo, str );
}
//打开码流,送入头数据 Open stream
if ( !PlayCtrl.PlayM4_OpenStream( m_lPort, pBuffer, dwBufSize, 2 * 1024 * 1024 ) )
{
nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
//str = "PlayM4_OpenStream failed, error code= " + nLastErr;
//this.BeginInvoke( AlarmInfo, str );
break;
}
//设置显示缓冲区个数 Set the display buffer number
if ( !PlayCtrl.PlayM4_SetDisplayBuf( m_lPort, 15 ) )
{
nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
//str = "PlayM4_SetDisplayBuf failed, error code= " + nLastErr;
//this.BeginInvoke( AlarmInfo, str );
}
//设置显示模式 Set the display mode
//if ( !PlayCtrl.PlayM4_SetOverlayMode( m_lPort, 0, 0) ) //play off screen
//{
// nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
// //str = "PlayM4_SetOverlayMode failed, error code= " + nLastErr;
// //this.BeginInvoke( AlarmInfo, str );
//}
//设置解码回调函数,获取解码后音视频原始数据 Set callback function of decoded data
m_fDisplayFun = new PlayCtrl.DECCBFUN( DecCallbackFUN );
if ( !PlayCtrl.PlayM4_SetDecCallBackEx( m_lPort, m_fDisplayFun, IntPtr.Zero, 0 ) )
{
//this.BeginInvoke( AlarmInfo, "PlayM4_SetDisplayCallBack fail" );
}
//开始解码 Start to play
if ( !PlayCtrl.PlayM4_Play( m_lPort, m_ptrRealHandle ) )
{
nLastErr = PlayCtrl.PlayM4_GetLastError( m_lPort );
//str = "PlayM4_Play failed, error code= " + nLastErr;
//this.BeginInvoke( AlarmInfo, str );
break;
}
}
break;
case CHCNetSDK.NET_DVR_STREAMDATA: // video stream data
default: //the other data
if ( dwBufSize > 0 && m_lPort != -1 )
{
for ( int i = 0; i
2、YV12转换为RGB32、RGB32转换为Image代码
class CommonFun
{
public static bool YV12_to_RGB32( byte[] buffYV12, byte[] bufferRGB32, int nWidth, int nHeight )
{
if( buffYV12.Length > 1 );
if ( nYLen > 8);
int G = Y - (((V - 128) * 88) >> 8) - (((U - 128) * 183) >> 8);
int B = Y + (V - 128) + (((V - 128) * 198) >> 8);
// r分量值
R = R 255 ? 255 : R;
// g分量值
G = G 255 ? 255 : G;
// b分量值
B = B 255 ? 255 : B;
//A分量值
nRgb[ 3 ] = 255;
//Out RGB Buffer
bufferRGB32[4 * (nRow*nWidth + nCol) + 0] = (byte)nRgb[0];
bufferRGB32[4 * (nRow*nWidth + nCol) + 1] = (byte)nRgb[1];
bufferRGB32[4 * (nRow*nWidth + nCol) + 2] = (byte)nRgb[2];
bufferRGB32[4 * (nRow*nWidth + nCol) + 3] = (byte)nRgb[3];
}
}
return true;
}
public static Bitmap RGB32_to_Image( byte[] byteBuff, int nWidth, int nHeight )
{
if ( byteBuff.Length
效果
文章标题:C#:使用海康SDK解码回调函数DecCallbackFUN()
文章链接:http://soscw.com/index.php/essay/98914.html