在WPF中获取海康威视摄像头视图

问题描述 投票:0回答:1

我有一台海康威视摄像头。我需要在我的应用程序中从该相机获取图像。所有SDK示例都是用winform制作的,我正在尝试将其集成到wpf中。在给出的示例SDK中,使用了picturebox,并且pictureBox的句柄参数连接到图像。

lpPreviewInfo.hPlayWnd = mtbDate.Handle;

我在WPF中使用WindowsFormsHost添加了PictureBox,但没有出现图像。当我按下 Live 按钮时,RAM 在后台增加。我认为这证明图像正在被接收,但我看不到图像。

登录顺利。

private void Login(string ipAddress, int port, string username, string password)
{
    CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
    m_lUserID = CHCNetSDK.NET_DVR_Login_V30(ipAddress, port, username, password, ref DeviceInfo);

    if (m_lUserID < 0)
    {
        iLastErr = CHCNetSDK.NET_DVR_GetLastError();
        str = "NET_DVR_Login_V30 failed, error code= " + iLastErr;
        MessageBox.Show(str);
    }
    else
    {
        MessageBox.Show("Login Success!");
    }
}

Live 中没有出现任何错误,但我也看不到视频。

private Int32 m_lRealHandle = -1;
CHCNetSDK.REALDATACALLBACK RealData = null;
private void LiveView()
{
    if (m_lUserID < 0)
    {
        MessageBox.Show("Please login the device firstly");
        return;
    }

    if (m_lRealHandle < 0)
    {
        CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
        lpPreviewInfo.hPlayWnd = myImage.Handle;
        lpPreviewInfo.lChannel = 1;
        lpPreviewInfo.dwStreamType = 0;
        lpPreviewInfo.dwLinkMode = 0;
        lpPreviewInfo.bBlocked = true;
        lpPreviewInfo.dwDisplayBufNum = 1;
        lpPreviewInfo.byProtoType = 0;
        lpPreviewInfo.byPreviewMode = 0;


        if (RealData == null)
        {
            RealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);
        }

        IntPtr pUser = new IntPtr();

        m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, null/*RealData*/, pUser);
        if (m_lRealHandle < 0)
        {
            iLastErr = CHCNetSDK.NET_DVR_GetLastError();
            str = "NET_DVR_RealPlay_V40 failed, error code= " + iLastErr;
            MessageBox.Show(str);
            return;
        }
        else
        {
        }
    }
    return;
}

public void RealDataCallBack(Int32 lRealHandle, UInt32 dwDataType, IntPtr pBuffer, UInt32 dwBufSize, IntPtr pUser)
{
    if (dwBufSize > 0)
    {
        byte[] sData = new byte[dwBufSize];
        Marshal.Copy(pBuffer, sData, 0, (Int32)dwBufSize);

        string str = "ʵʱÁ÷Êý¾Ý.ps";
        FileStream fs = new FileStream(str, FileMode.Create);
        int iLen = (int)dwBufSize;
        fs.Write(sData, 0, iLen);
        fs.Close();
    }
}

设计代码如下。

   <StackPanel>
  
       <WindowsFormsHost>
           <wf:PictureBox x:Name="myImage"
                          Width="500"
                          Height="500"
                          />
       </WindowsFormsHost>
       <Button Content="Login"
               Click="Login_Click" />
       <Button Content="Live View"
               Click="Live_Click" />
   </StackPanel>

注意:它永远不会落入 RealDataCallBack 函数中。也可以在原来的winform代码中拍摄图像,而不触发RealDataCallBack。

我认为问题在于无法显示图像。 SDK中有录音功能。当我想要记录时,它成功获取记录并将其写入相关文件路径。代码如下。

        bool m_bRecord = false;
    private void btnRecord_Click(object sender, EventArgs e)
    {
        string sVideoFileName;
        sVideoFileName = "Record_test.mp4";

        if (m_bRecord == false)
        {
            int lChannel = 1;
            CHCNetSDK.NET_DVR_MakeKeyFrame(m_lUserID, lChannel);

            if (!CHCNetSDK.NET_DVR_SaveRealData(m_lRealHandle, sVideoFileName))
            {
                iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                str = "NET_DVR_SaveRealData failed, error code= " + iLastErr;
                System.Windows.MessageBox.Show(str);
                return;
            }
            else
            {
                m_bRecord = true;
            }
        }
        else
        {
            if (!CHCNetSDK.NET_DVR_StopSaveRealData(m_lRealHandle))
            {
                iLastErr = CHCNetSDK.NET_DVR_GetLastError();
                str = "NET_DVR_StopSaveRealData failed, error code= " + iLastErr;
                System.Windows.MessageBox.Show(str);
                return;
            }
            else
            {
                str = "Successful to stop recording and the saved file is " + sVideoFileName;
                System.Windows.MessageBox.Show(str);
                m_bRecord = false;
            }
        }

        return;
    }
c# wpf hikvision
1个回答
0
投票

我明白了问题的根源。项目是 .NET Framework 还是 .NET Core 并不重要。有趣的是,如果在应用程序中设置了

AllowsTransparency="True"
,WindowsFormsHost 将不起作用。您可以通过下面的链接访问详细信息。

必须是;

AllowsTransparency="False"

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/troubleshooting-hybrid-applications?view=netframeworkdesktop-4.8#opacity-and-layering

© www.soinside.com 2019 - 2024. All rights reserved.