话不多说,直接上关键代码!
关键代码一:在初始化相机的时候对相机的像素格式进行判断
private string InitCallBack() //匹配相机像素格式
{
ImageInfo ii = new ImageInfo();
CameraErrorMsg = GetImageInfo(ref ii);
if (CameraErrorMsg != null)
return CameraErrorMsg;
if (IsMonoData((MyCamera.MvGvspPixelType)ii.PixelFormat)) //灰度图
{
CameraErrorMsg = SetPixelFormat(ii.PixelFormat);
if (CameraErrorMsg != null)
return CameraErrorMsg;
ImageCallback = new MyCamera.cbOutputExdelegate(CallbackGray);
nRet = m_pMyCamera.MV_CC_RegisterImageCallBackEx_NET(ImageCallback, IntPtr.Zero);
if (MyCamera.MV_OK != nRet)
return HikCamErrorInfo.ReturnErrorMsg("注册回调函数失败", nRet);
ii.IsColor = false;
}
else if (IsColorData((MyCamera.MvGvspPixelType)ii.PixelFormat))
{
CameraErrorMsg = SetPixelFormat(ii.PixelFormat);
if (CameraErrorMsg != null)
return CameraErrorMsg;
ImageCallback = new MyCamera.cbOutputExdelegate(CallbackBGR);
nRet = m_pMyCamera.MV_CC_RegisterImageCallBackForBGR_NET(ImageCallback, IntPtr.Zero);
if (MyCamera.MV_OK != nRet)
return HikCamErrorInfo.ReturnErrorMsg("注册回调函数失败", nRet);
ii.IsColor = true;
}
else
{
return HikCamErrorInfo.ReturnErrorMsg("未知的像素格式" + Enum.GetName(typeof(MyCamera.MvGvspPixelType), ii.PixelFormat), -1);
}
if (OpenCameraOverEvent != null)
OpenCameraOverEvent(ii);
return null;
}
关键代码二:灰度图取流回调
//灰度图
private void CallbackGray(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)
{
try
{
Int32 nWidth = pFrameInfo.nWidth;
Int32 nHeight = pFrameInfo.nHeight;
Int32 lenData = nWidth * nHeight * 1; //数据长度,8位图像数据的长度=宽度*高度*1
byte[] buffer = new byte[lenData]; //创建指定长度byte数据
MyCamera.MvGvspPixelType enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
// MyCamera.MvGvspPixelType enDstPixelType;
//if (IsMonoData(pFrameInfo.enPixelType))
// {
// enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
//}
//else if (IsColorData(pFrameInfo.enPixelType))
//{
// enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed;
//}
//else
//{
// return;
//}
IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
stConverPixelParam.nWidth = pFrameInfo.nWidth;
stConverPixelParam.nHeight = pFrameInfo.nHeight;
stConverPixelParam.pSrcData = pData;
stConverPixelParam.nSrcDataLen = pFrameInfo.nFrameLen;
stConverPixelParam.enSrcPixelType = pFrameInfo.enPixelType;
stConverPixelParam.enDstPixelType = enDstPixelType;
stConverPixelParam.pDstBuffer = pImage;
stConverPixelParam.nDstBufferSize = (uint)lenData;
nRet = m_pMyCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
if (MyCamera.MV_OK != nRet)
{
return;
}
GetImageEventArgs e = new GetImageEventArgs()
{
buffer = buffer,
pImage = pImage,
Height = nHeight,
Width = nWidth,
IsColor = IsColorData(pFrameInfo.enPixelType)
};
if (GetImageEvent != null)
{
GetImageEvent(this, e);
}
// GC.Collect();
}
catch (Exception ex)
{
WritelogStatic.WriteSocketLog(ex.ToString());
// throw new Exception(ex.ToString());
}
}
关键代码三:RGB图取图回调
//彩色图
private void CallbackBGR(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser) //gray CallbackBGR
{
try
{
Int32 nWidth = pFrameInfo.nWidth;
Int32 nHeight = pFrameInfo.nHeight;
Int32 lenData = nWidth * 3 * nHeight;//数据长度,24位图像数据的长度=宽度*高度*3
byte[] buffer = new byte[lenData];//创建指定长度byte数据
MyCamera.MvGvspPixelType enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed;
//MyCamera.MvGvspPixelType enDstPixelType;
//if (IsMonoData(pFrameInfo.enPixelType))
//{
// enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
//}
//else if (IsColorData(pFrameInfo.enPixelType))
//{
// enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed;
//}
//else
//{
// return;
//}
IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
stConverPixelParam.nWidth = pFrameInfo.nWidth;//图像宽度
stConverPixelParam.nHeight = pFrameInfo.nHeight;//图像高度
stConverPixelParam.pSrcData = pData;//源数据
stConverPixelParam.nSrcDataLen = pFrameInfo.nFrameLen;//
stConverPixelParam.enSrcPixelType = pFrameInfo.enPixelType;//源数据格式
stConverPixelParam.enDstPixelType = enDstPixelType;
stConverPixelParam.pDstBuffer = pImage;//转换后的数据指针
stConverPixelParam.nDstBufferSize = (uint)lenData;//数据大小
nRet = m_pMyCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);//格式转换
if (MyCamera.MV_OK != nRet)
{
return;
}
GetImageEventArgs e = new GetImageEventArgs()
{
buffer = buffer,
pImage = pImage,
Height = nHeight,
Width = nWidth,
IsColor = IsColorData(pFrameInfo.enPixelType)
};
if (GetImageEvent != null)
{
GetImageEvent(this, e);
}
// GC.Collect();
}
catch (Exception ex)
{
WritelogStatic.WriteSocketLog(ex.ToString());
// throw new Exception(ex.ToString());
}
}