【发布时间】:2011-06-01 21:36:03
【问题描述】:
我正在尝试从网络摄像头获取图片。有一个 php-python 网络服务可以从网络摄像头中获取图片并提供服务:它提供图片的服务类似于 http://ip/jpeg/camera=1。
private HttpWebRequest request;
private HttpWebResponse response;
private CookieContainer container;
private Uri uri;
private string _user;
private string _pass;
private string _ip;
//Login code as seen in the previous section should be here
//GetImages is meant to run as a separate thread
private void GetImages(string camNo)
{
//create the GET request for the JPEG snapshot (found at /jpeg on the IP Camera)
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + deviceIP + "/jpeg/camera"+camNo);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = container;
//attempt to get a response from the IP110, event if error
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
ConnectionError(new ConnectionErrorEventArgs(e.Message));
}
//Get the stream containing the JPEG image.
Stream stream = response.GetResponseStream();
//Read the stream to memory.
byte[] buffer = new byte[100000];
int read, total = 0;
while ((read = stream.Read(buffer, total, 1000)) != 0)
{
total += read;
}
//create a BMP image from the stream
Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0, total));
//send the bmp image to where you would like to display it.
}
...然后我将该位图作为 jpeg 存储到文件夹中。我的问题从这里开始;我想尽快完成这个过程。我想使用该代码从 50 个网络摄像头拍摄照片并以 jpeg 格式存储,而且它必须很快 - 我的意思是每 10 秒我必须从 50 个摄像头获取新照片。
我使用了 timercontrol,每 500ms 给它 500ms 程序使用上面的代码到相机 编号并保存jpeg,但它一个接一个地工作,所以性能变低了!
50ms x 500cams = 25000 ms (25sec),如果我安排定时器控制的间隔 100 ms 程序冻结。 当我使用上面的代码时,我会在 200 毫秒内得到位图 bmp,但是当它尝试在磁盘上写入 jpeg 时需要很长时间。
我可以做些什么来更快地写入磁盘?我寻找内存映射 - 有帮助吗?我想将 jpeg 存储在光盘上,因为我将在网站上提供这些图片并与人们分享。我可以使用内存映射并通过网站公开提供它吗?
更新: 我还使用 WebClient 类进行异步工作, http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=571
结果是:得到一张 300 毫秒左右的图片并将其写入磁盘大约 700 毫秒 所以我必须找到一个尽可能多地写入磁盘的解决方案。
哪个更好?将图片写入磁盘或将图像发送到数据库? 我试图存储图片以使其准备好服务,因为在网站上人们必须看到更新的图片。哪一个更适合成千上万的客户查询?将它们存储在磁盘上还是以二进制形式存储在数据库中?
【问题讨论】:
-
这是网络吗?桌面?手机?
-
它是桌面应用程序,python-php 服务为它们提供服务,在同一台机器上我尝试使用上面的代码获取图片。
标签: c# performance get httprequest image