Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c语言中从图像流传送视频流#_C#_Video Streaming_Mjpeg - Fatal编程技术网

C# 如何在c语言中从图像流传送视频流#

C# 如何在c语言中从图像流传送视频流#,c#,video-streaming,mjpeg,C#,Video Streaming,Mjpeg,我将试图清楚地解释我的问题的背景 我有一个服务器从IP摄像机视频流中抓取帧。我能够处理和编辑这些图像,但现在我必须以视频流(mjpeg)的形式再次交付这些图像。我不需要强制要求将其作为流媒体发送给所有人,如果我可以通过TCP将流媒体发送给其他客户端,现在就足够了。 我不知道最好的方式是什么,因为我不认为一张一张地发送图片是一种好的做法 有人能帮我吗?我从未处理过这种情况,我无法在谷歌上找到任何解决方案(可能我没有做正确的查询…) 您将如何处理它?我发现了一个简单的方法,它可以执行非常类似的任务,

我将试图清楚地解释我的问题的背景

我有一个服务器从IP摄像机视频流中抓取帧。我能够处理和编辑这些图像,但现在我必须以视频流(mjpeg)的形式再次交付这些图像。我不需要强制要求将其作为流媒体发送给所有人,如果我可以通过TCP将流媒体发送给其他客户端,现在就足够了。
我不知道最好的方式是什么,因为我不认为一张一张地发送图片是一种好的做法

有人能帮我吗?我从未处理过这种情况,我无法在谷歌上找到任何解决方案(可能我没有做正确的查询…)

您将如何处理它?

我发现了一个简单的方法,它可以执行非常类似的任务,流式处理您的桌面。
我希望它也能帮助你

这里是使用Tcp通过IP将图像作为视频流发送的最简单方法 此方法用于发送和接收视频流。 这是基于Windows窗体的

发送流(服务器):

    private void Start_Sending_Video_Conference(string remote_IP, int port_number)
    {
        try
        {

            ms = new MemoryStream();// Store it in Binary Array as Stream


            IDataObject data;
            Image bmap;

            //  Copy image to clipboard
            SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);

            //  Get image from clipboard and convert it to a bitmap
            data = Clipboard.GetDataObject();

            if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
            {
                bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap))));
                bmap.Save(ms, ImageFormat.Bmp);
            }


            picCapture.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arrImage = ms.GetBuffer();
            myclient = new TcpClient(remote_IP, 5000);//Connecting with server
            myns = myclient.GetStream();
            mysw = new BinaryWriter(myns);
            mysw.Write(arrImage);//send the stream to above address
            ms.Flush();
            mysw.Flush();
            myns.Flush();
            ms.Close();
            mysw.Close();
            myns.Close();
            myclient.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Video Conference Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
  private void Start_Receiving_Video_Conference()
    {
        try
        {

            // Open The Port
            mytcpl = new TcpListener(5000);
            mytcpl.Start();                      // Start Listening on That Port
            mysocket = mytcpl.AcceptSocket();        // Accept Any Request From Client and Start a Session
            ns = new NetworkStream(mysocket);    // Receives The Binary Data From Port

            picture_comming.Image = Image.FromStream(ns);
            mytcpl.Stop();                           // Close TCP Session

            if (mysocket.Connected == true)          // Looping While Connected to Receive Another Message 
            {
                while (true)
                {
                    Start_Receiving_Video_Conference();              // Back to First Method
                }
            }
            myns.Flush();

        }
        catch (Exception) { button1.Enabled = true; myth.Abort(); }
    }
[System.Runtime.InteropServices.DllImport("user32", EntryPoint="SendMessageA")] 
    static extern int SendMessage(int hwnd, int wMsg, int wParam,  [MarshalAs(UnmanagedType.AsAny)] 
        object lParam); 
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint="SetWindowPos")] 
    static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); 
    [System.Runtime.InteropServices.DllImport("user32")] 
    static extern bool DestroyWindow(int hndw); 
    [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
    static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); 
    [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
    static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer);
接收流(客户端):

    private void Start_Sending_Video_Conference(string remote_IP, int port_number)
    {
        try
        {

            ms = new MemoryStream();// Store it in Binary Array as Stream


            IDataObject data;
            Image bmap;

            //  Copy image to clipboard
            SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);

            //  Get image from clipboard and convert it to a bitmap
            data = Clipboard.GetDataObject();

            if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
            {
                bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap))));
                bmap.Save(ms, ImageFormat.Bmp);
            }


            picCapture.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arrImage = ms.GetBuffer();
            myclient = new TcpClient(remote_IP, 5000);//Connecting with server
            myns = myclient.GetStream();
            mysw = new BinaryWriter(myns);
            mysw.Write(arrImage);//send the stream to above address
            ms.Flush();
            mysw.Flush();
            myns.Flush();
            ms.Close();
            mysw.Close();
            myns.Close();
            myclient.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Video Conference Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
  private void Start_Receiving_Video_Conference()
    {
        try
        {

            // Open The Port
            mytcpl = new TcpListener(5000);
            mytcpl.Start();                      // Start Listening on That Port
            mysocket = mytcpl.AcceptSocket();        // Accept Any Request From Client and Start a Session
            ns = new NetworkStream(mysocket);    // Receives The Binary Data From Port

            picture_comming.Image = Image.FromStream(ns);
            mytcpl.Stop();                           // Close TCP Session

            if (mysocket.Connected == true)          // Looping While Connected to Receive Another Message 
            {
                while (true)
                {
                    Start_Receiving_Video_Conference();              // Back to First Method
                }
            }
            myns.Flush();

        }
        catch (Exception) { button1.Enabled = true; myth.Abort(); }
    }
[System.Runtime.InteropServices.DllImport("user32", EntryPoint="SendMessageA")] 
    static extern int SendMessage(int hwnd, int wMsg, int wParam,  [MarshalAs(UnmanagedType.AsAny)] 
        object lParam); 
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint="SetWindowPos")] 
    static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); 
    [System.Runtime.InteropServices.DllImport("user32")] 
    static extern bool DestroyWindow(int hndw); 
    [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
    static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); 
    [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
    static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer);
非托管导入:

    private void Start_Sending_Video_Conference(string remote_IP, int port_number)
    {
        try
        {

            ms = new MemoryStream();// Store it in Binary Array as Stream


            IDataObject data;
            Image bmap;

            //  Copy image to clipboard
            SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);

            //  Get image from clipboard and convert it to a bitmap
            data = Clipboard.GetDataObject();

            if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
            {
                bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap))));
                bmap.Save(ms, ImageFormat.Bmp);
            }


            picCapture.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arrImage = ms.GetBuffer();
            myclient = new TcpClient(remote_IP, 5000);//Connecting with server
            myns = myclient.GetStream();
            mysw = new BinaryWriter(myns);
            mysw.Write(arrImage);//send the stream to above address
            ms.Flush();
            mysw.Flush();
            myns.Flush();
            ms.Close();
            mysw.Close();
            myns.Close();
            myclient.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Video Conference Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
  private void Start_Receiving_Video_Conference()
    {
        try
        {

            // Open The Port
            mytcpl = new TcpListener(5000);
            mytcpl.Start();                      // Start Listening on That Port
            mysocket = mytcpl.AcceptSocket();        // Accept Any Request From Client and Start a Session
            ns = new NetworkStream(mysocket);    // Receives The Binary Data From Port

            picture_comming.Image = Image.FromStream(ns);
            mytcpl.Stop();                           // Close TCP Session

            if (mysocket.Connected == true)          // Looping While Connected to Receive Another Message 
            {
                while (true)
                {
                    Start_Receiving_Video_Conference();              // Back to First Method
                }
            }
            myns.Flush();

        }
        catch (Exception) { button1.Enabled = true; myth.Abort(); }
    }
[System.Runtime.InteropServices.DllImport("user32", EntryPoint="SendMessageA")] 
    static extern int SendMessage(int hwnd, int wMsg, int wParam,  [MarshalAs(UnmanagedType.AsAny)] 
        object lParam); 
    [System.Runtime.InteropServices.DllImport("user32", EntryPoint="SetWindowPos")] 
    static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); 
    [System.Runtime.InteropServices.DllImport("user32")] 
    static extern bool DestroyWindow(int hndw); 
    [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
    static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID); 
    [System.Runtime.InteropServices.DllImport("avicap32.dll")] 
    static extern bool capGetDriverDescriptionA(short wDriver, string lpszName, int cbName, string lpszVer, int cbVer);
在服务器上预览网络摄像头图像的方法

此方法应首先启动以初始化网络摄像头预览(在服务器端)

要关闭预览,请执行以下操作:

private void ClosePreviewWindow() 
    {
        // 
        //  Disconnect from device
        // 
        SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0);
        // 
        //  close window
        // 
        DestroyWindow(hHwnd);
    }
网络摄像头Api

    const short WM_CAP = 1024; 
    const int WM_CAP_DRIVER_CONNECT = WM_CAP + 10; 
    const int WM_CAP_DRIVER_DISCONNECT = WM_CAP + 11; 
    const int WM_CAP_EDIT_COPY = WM_CAP + 30; 
    const int WM_CAP_SET_PREVIEW = WM_CAP + 50; 
    const int WM_CAP_SET_PREVIEWRATE = WM_CAP + 52; 
    const int WM_CAP_SET_SCALE = WM_CAP + 53; 
    const int WS_CHILD = 1073741824; 
    const int WS_VISIBLE = 268435456; 
    const short SWP_NOMOVE = 2; 
    const short SWP_NOSIZE = 1; 
    const short SWP_NOZORDER = 4; 
    const short HWND_BOTTOM = 1; 

请询问是否有任何疑问。

这取决于您的体系结构。这是PC对PC(从C到C)还是相机端更低级?(C,C++,ASM,…)相机正在流媒体,我可以通过RTSP或HTTP获取视频,所以相机架构不是问题。我无法解决的是如何再次从服务器流式传输到客户端(从C到C)。