我正在设置一个软件(SOFT1),该软件将JPG格式的视频帧抓取并使用Tidtrivialftp发送到另一个软件(SOFT2)。
SOFT2将接收Jpeg并显示在TImage中以供查看。
我不确定自己在做什么错。该代码是从此处另一篇文章中剪切并粘贴的。它似乎可以正常工作,但是我的TImage中什么也没有,如果尝试将其保存到磁盘,则会得到一个0KB的文件。
我尝试实施此链接上找到的解决方案:
Udp image streaming, delphi indy10
尝试发送一个仅2.48KB的小test.jpg以供测试。
客户端:
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。
答案 0 :(得分: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;