Unity接入海康网络摄像头SDK
写这篇文章是因为采用https://blog.csdn.net/qq_33214503/article/details/80091247 的方法,导致unity无法退出,导出的exe无法关闭
采用的方案是使用32位的海康SDK
我的Demo项目已经上传的码云
地址: https://gitee.com/mrlx0/MonitorCamera_FFmpeg
- ffmpeg_for_unity同样采用了32位的 ,而网络上搜的是64位,所有自己编译了一个32位的,地址: https://gitee.com/mrlx0/ffmpeg_for_unity
- demo中包含了64位ffmpeg_for_unity和64位的海康SDK,可以选择64位测试(建议不要,能看到内容,但是unity退出会卡死);
- 注意发布时要选择x86;选择x86_64打包会读64位的sdk,依然是无法关闭
- 我同时根据 http://blog.51cto.com/weiyuqingcheng/2323546 提供的方案(YV12转换为RGB32),制作了第二个场景,但会卡顿和画质不对,可能算法有问题,没仔细研究
效果: ![Alt]
没有采用栈
存储
private void DecCallbackFUN(int nPort, IntPtr pBuf, int nSize, ref PlayCtrl32.FRAME_INFO pFrameInfo,
int nReserved1, int nReserved2)
{
if (m_realHanle > -1)
{
// 将pBuf解码后视频输入写入文件中(解码后YUV数据量极大,尤其是高清码流,不建议在回调函数中处理)
if (pFrameInfo.nType == 3)
{
if (playerResolution == null)
{
var resolution = new Resolution();
resolution.width = pFrameInfo.nWidth;
resolution.height = pFrameInfo.nHeight;
resolution.refreshRate = pFrameInfo.nFrameRate;
playerResolution = resolution;
transforDataframe = new byte[(long)pFrameInfo.nWidth * pFrameInfo.nHeight * 4];
}
if(sourceDataFrame == null)
{
sourceDataFrame = new byte[nSize];
}
Marshal.Copy(pBuf, sourceDataFrame, 0, nSize);
}
}
}
private void StartTransformData()
{
while (m_IsFFmpeg && m_userId > -1)
{
if (sourceDataFrame != null)
{
if (FFMPEG32.YV12toRgb_Updated(transforDataframe, sourceDataFrame, playerResolution.Value.width, playerResolution.Value.height))
{
//dataBackupQueue.Enqueue(transforDataframe);
}
else
Debug.Log("转换失败");
}
float waitTime = Mathf.Max(16, _farmeTime * 1000);
Thread.Sleep((int)waitTime);
}
}
private IEnumerator AnalizeData()
{
var endFrame = new WaitForEndOfFrame();
while ((m_realHanle > -1 || m_PlayHandle > -1) && m_userId > -1)
{
if (playerResolution != null && m_Texture == null)
{
m_Texture = new Texture2D(playerResolution.Value.width, playerResolution.Value.height
, TextureFormat.RGB24, false);
}
if (m_Texture != null && transforDataframe!=null)//dataBackupQueue.Count > 0)
{
View.texture = m_Texture;
m_Texture.LoadRawTextureData(transforDataframe);//(dataBackupQueue.Dequeue());
m_Texture.Apply();
View.texture = m_Texture;
}
_farmeTime = Time.deltaTime;
yield return endFrame;
}
}
技术有限,有问题欢迎留言。