Delphi indy Http streaming server

The camera captures the image on the image. Wants this photo to be available at http. Can I use it somehow HTTPServer1CommandGet? I just want to display the image image1 on live If so how?

+4


source to share


2 answers


If you just need to display the last image when the 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 have to do live feeds of camera images in real time, it gets a little more complicated. You can do this in several different ways. For example, using a pull click:

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;

      

Use a server 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;

      

+5


source


Remy, WriteHeader seems to be writing "Content-Length: 0" It works as intended - but if you manually write the header and exclude Content-Length then it works



0


source







All Articles