简体   繁体   English

Udp图像流,delphi indy10

[英]Udp image streaming, delphi indy10

i'm using delphi xe4 with indy10 component and i want to send an image from an Tidudpclient to Tidudpserver. 我正在使用带有indy10组件的delphi xe4,我想将一个图像从Tidudpclient发送到Tidudpserver。 I already done this operation with tcp component,but the same code didn't work with udp. 我已经使用tcp组件完成了此操作,但相同的代码不能与udp一起使用。 how i can do this? 我怎么能这样做? Thanks in advance! 提前致谢!

Timage(client)--->streamUDP-->Timage(server) TImage中(客户端)---> streamUDP - >的TImage(服务器)

CLIENT SIDE----------------------------------------------- SEND IMAGE 客户端-----------------------------------------------发送图片

var
    pic: tbitmap;
       Strm : TMemoryStream;
        img2:Timage;

    buffer:TIdBytes;
    begin

        try
          img2:=Timage.Create(nil);
          pic:=Tbitmap.Create;
          Takekpic(pic);     
          BMPtoJPG(pic,img2);
          Strm := TMemoryStream.Create;
          img2.Picture.bitmap.SaveToStream(strm);
          Strm.Position:=0;
          ReadTIdBytesFromStream(Strm,buffer,SizeOf(Strm),0);
          IdTrivialFTPServer1.SendBuffer('192.168.17.128',1234,buffer);
        finally
             strm.Free;
          end; 
    end;

SERVER SIDE---------------------------------------------------- READ IMAGE 服务器端 - - - - - - - - - - - - - - - - - - - - - - - - ----阅读图片

procedure TForm6.IdTrivialFTP1UDPRead(AThread: TIdUDPListenerThread;
  const AData: TIdBytes; ABinding: TIdSocketHandle);
    var
Strm : TMemoryStream;
Jpg: TJpegImage;

begin
      Strm := TMemoryStream.Create;
      try    

         WriteTIdBytesToStream(Strm,AData,SizeOf(AData),0);
         strm.Position:=0;
         Jpg := TJpegImage.Create;
         jpg.LoadFromStream(Strm);  <---- error while reading (JPEG Error #53)
         img1.Picture.assign(jpg);
      finally
         strm.Free;
         Jpg.Free;
      end;
end;

what can be wrong in this code? 这段代码有什么不对吗?

TIdUDPClient and TIdUDPServer do not support sending/receiving TStream data. TIdUDPClientTIdUDPServer不支持发送/接收TStream数据。 You can save your image data into a TStream , but you will have to send/receive it using TIdBytes chunks. 您可以将图像数据保存到TStream ,但必须使用TIdBytes块发送/接收它。

Alternatively, use TIdTrivialFTP and TIdTrivialFTPServer instead, which implement TFTP, a UDP-based file transfer protocol. 或者,使用TIdTrivialFTPTIdTrivialFTPServer来实现TFTP,这是一种基于UDP的文件传输协议。 They operate using TStream objects 它们使用TStream对象进行操作

Update : for example: 更新 :例如:

Client: 客户:

var
  bmp: TBitmap;
  jpg: TJPEGImage;
  Strm : TMemoryStream;
begin
  Strm := TMemoryStream.Create;
  try
    jpg := TJPEGImage.Create;
    try
      bmp := TBitmap.Create;
      try 
        Takekpic(bmp);     
        jpg.Assign(bmp);
      finally
        bmp.Free;
      end;
      jpg.SaveToStream(Strm);
    finally
      jpg.Free;
    end;
    Strm.Position := 0;
    {
    These can be assigned ahead of time...
    IdTrivialFTP1.Host := '192.168.17.128';
    IdTrivialFTP1.Port := 1234;
    }
    IdTrivialFTP1.Put(Strm, 'image.jpg');
  finally
    Strm.Free;
  end; 
end;

Server: 服务器:

procedure TForm6.IdTrivialFTPServer1WriteFile(Sender: TObject; var FileName: String; const PeerInfo: TPeerInfo; var GrantAccess: Boolean; var AStream: TStream; var FreeStreamOnComplete: Boolean) of object;
begin
  if FileName = 'image.jpg' then
  begin
    GrantAccess := True;
    AStream := TMemoryStream.Create;
    FreeStreamOnComplete := True;
  end else
    GrantAccess := False;
end;

{
If you set TIdTrivialFTPServer.ThreadedEvent to False, this event handler
runs in the context of the main thread, so the UI can be accessed safely.
If you set IdTrivialFTPServer.ThreadedEvent to True, this event handler
runs in the context of a worker thread, so you will have to manually
synchronize with the main thread when updating the UI...
}
procedure TForm6.IdTrivialFTPServer1TransferComplete(Sender: TObject; const Success: Boolean; const PeerInfo: TPeerInfo; var AStream: TStream; const WriteOperation: Boolean);
var
  jpg: TJPEGImage;
begin
  if WriteOperation and Success then
  begin
    jpg := TJPEGImage.Create;
    try
      AStream.Position := 0;
      jpg.LoadFromStream(AStream);
      img1.Picture.Assign(jpg);
    finally
      jpg.Free;
    end;
  end;
end;

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

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