简体   繁体   English

Delphi indy流式传输Http Server

[英]Delphi indy streaming Http Server

The camera captures the image to the image. 相机将图像捕获到图像。 Wants this picture was available at http. 想要这张照片可以在http。 Can I use it somehow HTTPServer1CommandGet I display a? 我可以以某种方式使用它HTTPServer1CommandGet我显示一个? I just want to display the image of image1 on live If so, how? 我只想在现场显示image1的图像如果是这样,怎么样?

If you just need to display the latest image when a client asks for it, you can do something like this: 如果您只需要在客户端要求时显示最新图像,您可以执行以下操作:

type
  TGetImageStream = class(TIdSync)
  protected
    FStream: TStream;
    procedure DoSynchronize; override;
  public
    class procedure GetImage(Stream: TStream);
  end;

procedure TGetImageStream.DoSynchronize;
begin
  Form1.Image1.Bitmap.SaveToStream(FStream);
end;

class procedure TGetImageStream.GetImage(Stream: TStream);
begin
  with Create do
  try
    FStream := Stream;
    Synchronize;
  finally
    Free;
  end;
end;

procedure TForm1.HTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  Strm: TMemoryStream;
begin
  Strm := TMemoryStream.Create;
  try
    TGetImageStream.GetImage(Strm);
    Strm.Position := 0;
  except
    Strm.Free;
    raise;
  end;
  AResponseInfo.ResponseNo := 200;
  AResponseInfo.ContentType := 'image/bmp';
  AResponseInfo.ContentStream := Strm;
end;

But if you need to do live streaming of the camera images in real time, that gets a bit trickier. 但是如果你需要实时对相机图像进行实时流式传输,那就会变得有点棘手。 You can do it a few different ways. 你可以用几种不同的方式做到这一点。 For instance, using a client pull: 例如,使用客户端拉:

procedure TForm1.HTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  Strm: TMemoryStream;
begin
  if ARequestInfo.Document = '' then
  begin
    AResponseInfo.Redirect('/');
  end
  else if ARequestInfo.Document = '/' then
  begin
    AResponseInfo.ResponseNo := 200;
    AResponseInfo.ContentType := 'text/html';
    AResponseIno.ContentText := '<html>'+EOL+
                                '<head>'+EOL+
                                '<title>Camera Image</title>'+EOL+
                                '<meta http-equiv="Refresh" content=5>'+EOL+
                                '</head>'+EOL+
                                '<body>'+EOL+
                                '<img src="/image">'+EOL+
                                '</body>'+EOL+
                                '</html>'+EOL;
  end
  else if ARequestInfo.Document = '/image' then
  begin
    Strm := TMemoryStream.Create;
    try
      TGetImageStream.GetImage(Strm);
      Strm.Position := 0;
    except
      Strm.Free;
      raise;
    end;
    AResponseInfo.ResponseNo := 200;
    AResponseInfo.ContentType := 'image/bmp';
    AResponseInfo.ContentStream := Strm;
  end else begin
    AResponseInfo.ResponseNo := 404;
  end;
end;

Using a server push instead: 改为使用服务器推送:

procedure TForm1.HTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  Strm: TMemoryStream;
begin
  Strm := TMemoryStream.Create;
  try
    AResponseInfo.ResponseNo := 200;
    AResponseInfo.ContentType := 'multipart/x-mixed-replace; boundary=imgboundary';
    AResponseInfo.CloseConnection := False;
    AResponseInfo.WriteHeader;

    AContext.Connection.IOHandler.WriteLn('--imgboundary');
    repeat
      Strm.Clear;
      TGetImageStream.GetImage(Strm);

      AContext.Connection.IOHandler.WriteLn('Content-type: image/bmp');
      AContext.Connection.IOHandler.WriteLn;
      AContext.Connection.IOHandler.Write(Strm);
      AContext.Connection.IOHandler.WriteLn;
      AContext.Connection.IOHandler.WriteLn('--imgboundary');

      Sleep(5000);
   until False;
  finally
    Strm.Free;
  end;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM