簡體   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