使用的HttpRequest摆脱给定的URL图片 [英] Using Httprequest to get pictures from given URL

查看:222
本文介绍了使用的HttpRequest摆脱给定的URL图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从摄像头的图片。有一个php-python的Web服务来获得网络摄像头的图片和为他们服务:它提供像 HTTP图片:// IP / JPEG /摄像头= 1

I'm trying to get pictures from webcams. There is a php-python web service to get the pictures from webcams and serve them: it serves the picture like 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到一个文件夹。我的问题就在这里开始;我想这样做的过程尽可能快。我想借此使用该代码并存储为JPEG 50摄像头的图片,它有要快 - 我的意思是每10秒我不得不从50凸轮获得新的图片

... then I store that bitmap as a jpeg to a folder. My problem starts here; I want to do that process as fast as possible. I want to take pictures from 50 webcams using that code and store as jpeg, and it has to be fast - I mean every 10 seconds I have to get new pictures from 50 cams.

我用timercontrol,并给它500毫秒每500毫秒程序使用上面的代码到相机
号和保存JPEG,却接连因此性能变低的工作之一!

I used timercontrol, and give it 500 ms every 500ms program use that code above up to camera number and save the jpeg, but it work one after another so performance gets low!

50ms的点¯x500cams = 25000毫秒(25秒),如果我安排时间控制的间隔100毫秒程序冻结。
当我用上面的代码我得到了200毫秒的BMP位图,但是当它试图写为JPEG光盘上它需要很长的时间。

50ms x 500cams = 25000 ms (25sec), if I arrange timer control's interval 100 ms program freeze. When I use the code above I get bitmap bmp in 200 ms but when it tries to write as jpeg on disc it takes a long time.

能有什么我写盘速度更快?我找了内存映射 - 这将帮助?我想存储在光盘上的JPEG becouse我将成为一个网站上这些照片,并与人分享。我可以使用内存映射,并通过网站公开为它服务。

What can I do to write to disk faster? I looked for memory mapped - would it help? I want to store jpegs on disc becouse I will serve these pictures on a web site and share them with people. Can I use memory mapped and serve it publicly via a web site?

更新:
我也用WebClient类的异步工作,
http://www.informit.com/ ?指南/ content.aspx G = DOTNET&安培; SEQNUM = 571

结果是:要得到像300 ms的图片,并将其写入磁盘约700毫秒
所以我必须找到一个解决办法写入到磁盘尽可能地。

The result was: To get a picture like 300 ms and writing it to disk about 700ms So I have to find a solution to write to disk as much as possible.

哪一个更好?要写入图片到磁盘或将图像发送到数据库?
我试图存储的图片,让他们准备好发球,因为网站上的人,才能看到更新的照片。哪一个是为成千上万的客户端查询的更好吗?将它们存储在磁盘上或他们的数据库存储二进制?

Which one is better? To write the pictures to disk or to send images to database? I tried to store the pictures to make them ready for serve, because on the website people have to see updated pictures. Which one is better for thousands of client query? To store them on disk or store them on database as binary?

推荐答案

获取图像的代码是同步的。我建议修改代码中使用 HttpWebRequest.BeginGetResponse()中的这个答案

The code to fetch the images is synchronous. I would suggest modifying your code to use HttpWebRequest.BeginGetResponse() to fetch your images as detailed in this answer.

因此,当你(单)计时器火灾,开始异步请求你的摄像头,然后(因为它们完成)回调将触发每完成请求,并可以节省掉返回的图像。

So when your (single) timer fires, begin the async requests to your cams, then (as they complete) the callback will fire for each completed request, and you can save off the returned image.

如果您需要优化进一步,那么你可以委派图像到磁盘保存到后台 线程池工作项

If you need to optimize further, then you could delegate saving the image to disk to a background ThreadPool work item.

这篇关于使用的HttpRequest摆脱给定的URL图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆