相机将图像捕捉到图像。
想要这张图片可以在 http 上找到。
我可以以某种方式使用它 HTTPServer1CommandGet 我显示一个?
我只想在直播中显示 image1 的图像
如果是这样,如何?

最佳答案

如果您只需要在客户要求时显示最新图像,您可以执行以下操作:

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;

但是,如果您需要实时流式传输相机图像,那就有点棘手了。您可以通过几种不同的方式来做到这一点。例如,使用客户端拉取:
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;

改用服务器推送:
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;

关于Delphi indy 流式 Http 服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25653820/

10-10 13:27