繁体   English   中英

如何在c#中从图像流传递视频流

[英]how to deliver video-streaming from image stream in c#

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

我有一台服务器抓取来自IP摄像机视频流的帧。 我能够处理和编辑这些图像,但现在我必须再次将这些图像作为视频流(mjpeg)传送。 我不需要将其作为流媒体传递给每个人,如果我可以通过TCP将流传输到另一个客户端,那么现在就应该这样做。
我不知道最好的方法是什么,因为我不认为一个接一个地发送图像是一个很好的做法......

有人能以某种方式帮助我吗? 我从来没有处理过这种情况,我也无法在Google上找到任何解决方案(可能我没有做正确的查询...)

你会怎么处理?

我找到了一个简单的CodeProject ,它执行一个非常类似的任务,流式传输您的桌面。 我希望它也可以帮到你!

这是使用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 OpenPreviewWindow() 
    {
        int iHeight = 259;
        int iWidth = 369;
        // 
        //  Open Preview window in picturebox
        // 
        hHwnd = capCreateCaptureWindowA(iDevice.ToString (), (WS_VISIBLE | WS_CHILD), 0, 0, 640, 480, picCapture.Handle.ToInt32(), 0);
        // 
        //  Connect to device
        // 
        if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) == 1) 
        {
            // 
            // Set the preview scale
            // 
            SendMessage(hHwnd, WM_CAP_SET_SCALE, 1, 0);
            // 
            // Set the preview rate in milliseconds
            // 
            SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
            // 
            // Start previewing the image from the camera
            // 
            SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, 0);
            // 
            //  Resize window to fit in picturebox
            // 
            SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, iWidth,iHeight, (SWP_NOMOVE | SWP_NOZORDER));
        }
        else 
        {
            // 
            //  Error connecting to device close window
            //  
            DestroyWindow(hHwnd);
        }
    }

要关闭预览:

private void ClosePreviewWindow() 
    {
        // 
        //  Disconnect from device
        // 
        SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0);
        // 
        //  close window
        // 
        DestroyWindow(hHwnd);
    }

WebCam 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; 

请询问是否有任何疑问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM