0

我已经极大地改变了这个问题,但它就在这里。我正在从 IP cam 读取 mjpeg 流,一切正常。但是现在在我收到的每一帧(参见 stream_NewFrame)上,我想将此图像推送到客户端。但我似乎无法弄清楚如何访问 HttpContext.Current 因为它在该函数中始终为空。有谁知道如何访问 HttpContext 上下文,就像我可以在 ProcessRequest 函数中一样?我想我在这里遗漏了一些明显的东西,但我不知道是什么!感谢您的时间。

  public class ImageHandler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {

        //Get parameter
        string Url = context.Request.QueryString["url"];
        string Username = context.Request.QueryString["username"];
        string Password = context.Request.QueryString["password"];

        //Set cache 
        HttpResponse Response = HttpContext.Current.Response;
        Response.Expires = 0;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "multipart/x-mixed-replace";

        // create MJPEG video source
        MJPEGStream stream = new MJPEGStream(string.Format("{0}/video.cgi?user={1}&pwd={2}", Url, Username, Password));
        stream.NewFrame += new NewFrameEventHandler(stream_NewFrame);
        stream.Start();

    }


    private void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Image img = eventArgs.Frame;
        byte[] b = GetImageBytes(eventArgs.Frame);
        HttpContext.Current.Response.OutputStream.Write(b, 0, b.Length);

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
4

2 回答 2

1

你为什么不把它保存在HttpContext某个地方以便可以从stream_NewFrame方法中访问它呢?我建议在你的类中使用一个成员变量。

如果您想封装更多内容,请创建一个单独的类HttpContext,然后将stream_NewFrame方法放入该类中。就像是:

class Processor

      private HttpContext _context;

      public Processor(HttpContext context) {
           _context = context;
      }

      public void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
      {
          Image img = eventArgs.Frame;
          byte[] b = GetImageBytes(eventArgs.Frame);
          HttpContext.Current.Response.OutputStream.Write(b, 0, b.Length);
      }
}

然后在你的ProcessRequest,你这样做:

公共类 ImageHandler : IHttpHandler, IRequiresSessionState {

public void ProcessRequest(HttpContext context)
{

    //Get parameter
    string Url = context.Request.QueryString["url"];
    string Username = context.Request.QueryString["username"];
    string Password = context.Request.QueryString["password"];

    //Set cache 
    HttpResponse Response = HttpContext.Current.Response;
    Response.Expires = 0;
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "multipart/x-mixed-replace";

    // create processor
    Processor p = new Processor(context);    

    // create MJPEG video source
    MJPEGStream stream = new MJPEGStream(string.Format("{0}/video.cgi?user={1}&pwd={2}", Url, Username, Password));
    stream.NewFrame += new NewFrameEventHandler(p.stream_NewFrame);
    stream.Start();

}
于 2011-08-14T19:34:01.663 回答
0

MJPEGStream 创建一个它操作的后台线程。HTTPContext.Current 是一个线程局部变量,这意味着后台线程(即调用stream_newFrame回调函数的线程)在不同的线程上,所以它不一样HTTPContext(实际上它没有)。您需要以其他方式提供它。像 Erik 建议的那样创建一个处理器对象来保存引用的想法应该很有效。

于 2011-08-14T23:07:24.203 回答