Search code examples
c#.netip-camera

Grabbing frames from a Hikvision IP-camera


I'm having problems with grabbing frames from a remote IP-camera. My employer wants it done in C# .NET (for Windows) and if possible to use a light-weight solution, i.e. not using any huge frameworks.

The device model is DS-2CD2632F-I, it us currently connected to my LAN and the camera's Web-interface works perfectly fine.

I already tried out several popular frameworks, e.g. AForge, EmguCV, OzekiSDK and Directshow.NET, but none of them seem to be working. Particularly the OzekiSDK (apparently, recommended by Hikvision?) isn't able to get a video stream from the camera, even if I simply use of the sample projects provided, which simply shows a black screen and throws an "Empty camera object" exception if I try to grab a frame.

The camera's Web-interface works correctly and even the VLC Player manages to successfully play the stream from the camera via a rtsp:// link (rtsp://my_ip:554//Streaming/Channels/1), without asking for the login and the password.

I thought about using libvlcnet, but I'm not sure it's a viable solution.

Do you have any recommendations?


Solution

  • Ok, so I think I figured it out. I'll post the solution here, in case someone needs it in future. I found out, that for this particular type of camera there is a url, that simply returns the current frame from the camera as a JPEG picture, which looks like this: http://IP_ADDRESS:PORT/Streaming/channels/1/picture?snapShotImageType=JPEG

    Since my employer only needs to be able to grab one frame from the camera whenever he wants to and doesn't need to stream the video itself, I just wrote an application that copies this JPEG from that url by using an Async Web-Request:

    private Bitmap loadedBitmap;
    ...
    private void requestFrame()
    {    
     string cameraUrl = @"http://192.168.0.1XX:80/Streaming/channels/1/picture?snapShotImageType=JPEG";
     var request = System.Net.HttpWebRequest.Create(cameraUrl);   
     request.Credentials = new NetworkCredential(cameraLogin, cameraPassword);
     request.Proxy = null;
     request.BeginGetResponse(new AsyncCallback(finishRequestFrame), request);
    }
    
    void finishRequestFrame(IAsyncResult result)
    {
     HttpWebResponse response = (result.AsyncState as HttpWebRequest).EndGetResponse(result) as HttpWebResponse;
     Stream responseStream = response.GetResponseStream();
    
     using (Bitmap frame = new Bitmap(responseStream)) {
      if (frame != null) {
       loadedBitmap = (Bitmap) frame.Clone();                        
      }
     }
    }
    ...
    requestFrame(); // call the function