无法通过Tidtrivialftp发送JPG。收到的文件是0KB

问题描述 投票:1回答:1

我正在设置一个软件(SOFT1),它采用JPG格式的视频帧抓取,并使用Tidtrivialftp发送到另一个软件(SOFT2)。

SOFT2用于接收Jpeg并在TImage中显示以供查看。

我不确定我做错了什么。代码是来自这里的另一篇帖子的剪切和粘贴。它似乎工作,但我的TImage没有得到任何东西,如果我尝试保存到磁盘,我得到一个0KB文件。

我尝试实现此链接上的解决方案:

Udp image streaming, delphi indy10

试图发送一个小的test.jpg,只有2.48KB用于测试目的。

客户端:

procedure TForm1.BtnClick(Sender: TObject);
var
  Myjpg: TJPEGImage;
  Strmkoko : TMemoryStream;
begin

try
    //Tried a lot of different ways to load the jpg into a stream. This is the latest one with same results.

    Strmkoko := TMemoryStream.Create;
    Myjpg := TJPEGImage.Create;
    Myjpg.LoadFromFile('C:\Users\Etienne\Desktop\MyVideo\a\test.jpg');
    Myjpg.SaveToStream(Strmkoko);
    Strmkoko.Position := 0;
    Image1.Picture.assign(Myjpg); //Confirming MyJpg is showing the picture by placing it in a TImage component before sending - All ok

    //Also tried to just put the filename instead of stream. - no difference
    IdtrivialFTPClientFrameGrab.Put(Strmkoko, 'test.jpg');

finally
    Strmkoko.Free;
    Myjpg.Free;
end;
end;

服务器端:

procedure TForm2.IdFTPServerFrameGrabTransferComplete(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
      jpg.LoadFromStream(AStream);
      jpg.SaveToFile('C:\Users\Etienne\Desktop\Pic\test.jpg'); //trying to save the jpg to check what I get and its 0KB
      img1.Picture.Assign(jpg); //This is the final place I want to send the stream
    finally
      jpg.Free;
    end;
  end;
end;

procedure TForm2.IdFTPServerFrameGrabWriteFile(Sender: TObject;
  var FileName: string; const PeerInfo: TPeerInfo; var GrantAccess: Boolean;
  var AStream: TStream; var FreeStreamOnComplete: Boolean);
begin
  if Filename = 'test.jpg' then
  begin
    //Code does get in here when I debug
    GrantAccess := True;
    AStream := TMemoryStream.Create;
    FreeStreamOnComplete := True;
  end else
    GrantAccess := False;
end;

我期待我发送的文件(test.jpg)出现在img1中,并保存在'C:\ Users \ Etienne \ Desktop \ Pic \ test.jpg'中

代码确实以保存文件并将其分配给img1的方式工作,但它是空的。

这一切都在当地完成。

它像“IdtrivialFTPClientFrameGrab.Put(Strmkoko,'test.jpg');”正在发送一个空文件。但我已经尝试了几种方法来加载流并始终保持相同的结果。

我知道TCP / IP会更好,但我想让它发挥作用。任何帮助,将不胜感激。

干杯,E。

file delphi udp indy transfer
1个回答
1
投票

经过大量的试验,我发现了问题所在。我不知道为什么从其他帖子中显示的示例中省略了它,但是在收到Stream后你必须重置它的位置才能加载它...

AStream.Position:= 0;

procedure TForm2.IdFTPServerFrameGrabTransferComplete(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; // <----- insert here
      jpg.LoadFromStream(AStream);
      jpg.SaveToFile('C:\Users\Etienne\Desktop\Pic\test.jpg'); //trying to save the jpg to check what I get and its 0KB
      img1.Picture.Assign(jpg); //This is the final place I want to send the stream
    finally
      jpg.Free;
    end;
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.