2014-09-03 115 views
3

相机将图像捕捉到图像。 想要这张照片在http上可用。 我可以用它以某种方式HTTPServer1CommandGet我显示一个? 我只想在生活上显示image1的图像 如果是这样,怎么样?Delphi indy streaming Http服务器

+0

道歉,但我绝对不知道你要问什么。首先,你的意思是“将图像捕捉到图像上”? – 2014-09-03 21:51:29

+0

我想在互联网上显示image1的图像帮助http服务器 – 2014-09-03 21:53:44

+0

那么这是一个带有'TImage'控件的VCL表单应用程序吗?我只是在猜测。尽管如此,这只能澄清10%的问题。 – 2014-09-03 21:55:49

回答

4

如果你只需要在客户端需要它来显示最新的图像,你可以做这样的事情:

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; 
+0

谢谢! ,你帮了我,我只是编程的初学者,想学习最好的;) – 2014-09-04 09:27:43

+0

作为一个单独的应用程序,以接收此图像到Timage? – 2014-09-05 08:59:50

+0

webbrowser.navigate('localhost');?想要与另一个应用程序,你可以窥视图片 – 2014-09-05 09:00:50