1

我正在尝试编写一个代码,它将连接到 IP 摄像机并从中获取 jpeg 图像。它是这样的:

            HttpWebRequest request;                                
            CookieContainer cookies= new CookieContainer();

            //First, I log to IP camera on adress http://192.168.0.2
            request = (HttpWebRequest)WebRequest.Create(new Uri(sanyoUrls[0]));
            request.Credentials = new NetworkCredential(username, password);
            request.KeepAlive = false;
            request.AllowAutoRedirect = false;
            request.CookieContainer = cookies;

            // then I try to get the IP camera jpeg image from adress http://192.168.0.2/liveimg.cgi        
            cookies = request.CookieContainer;
            request = (HttpWebRequest)WebRequest.Create(new Uri(sanyoUrls[1]));
            request.KeepAlive = false;
            request.Credentials = new NetworkCredential(username, password);
            request.AllowAutoRedirect = false;
            request.CookieContainer = cookies;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {

                using (Stream strImg = response.GetResponseStream())
                {
                    MemoryStream memStr = new MemoryStream();
                    strImg.CopyTo(memStr);

                    if (response.ContentType == "application/xml")
                    {
                        // write message to GUI
                    }
                    else
                    {
                        try
                        {
                            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(memStr);
                            PBcameraImage.Image = bmp;                      
                            // write OK to file
                        }
                        catch (ArgumentException ex)
                        {
                            // write NULL to file
                        }

                    }
                }
                response.Close();
            }

此代码以指定的周期执行(我尝试了 40 ms 到 1 秒)。文件中的日志通常是这样的:首先是 NULL,然后是 20 次 OK,之后在文件中只有 NULL,但长时间后只有一条 OK 行。所以我从 IP 摄像机只得到 20 张照片。有什么建议吗?谢谢你。

干杯, Ondrej

4

0 回答 0