How do you display a vcard XMPP (Jabber) photo in Delphi?

How can I read a photo from a VMP XMPP file (an avatar image that I think is in JPEG format) and display it in a Delphi TImage control?

The XMPP server sends this XML:

<presence id="e3T50-75" to="cvg@esx10-2022/spark" from="semra@esx10-2022" 
 type="unavailable">
  <x xmlns="vcard-temp:x:update">
    <photo>897ce4538a4568f2e3c4838c69a0d60870c4fa49</photo>
  </x>
  <x xmlns="jabber:x:avatar">
    <hash>897ce4538a4568f2e3c4838c69a0d60870c4fa49</hash>
  </x>
</presence>
+3
source share
1 answer

The XML you sent does not contain an image. It contains a SHA-1 hash of the image content. At first, you only get a hash if you have already downloaded this image before, so you can display the cached version instead of requesting it again.

, vcard. , PHOTO, . : BINVAL TYPE. BINVAL Base-64, TYPE MIME , , image/jpeg image/png.

, TFileStream TMemoryStream. , TGraphic . TPngImage, TBitmap. . :

function CreateGraphicFromVCardPhoto(const BinVal, MimeType: string): TGraphic;
var
  Stream: TStream;
  GraphicClass: TGraphicClass;
begin
  Stream := TMemoryStream.Create;
  try
    if not Base64Decode(BinVal, Stream) then
      raise EBase64Decode.Create;
    Stream.Position := 0;
    GraphicClass := ChooseGraphicClass(MimeType);
    Result := GraphicClass.Create;
    try
      Result.LoadFromStream(Stream);
    except
      Result.Free;
      raise;
    end;
  finally
    Stream.Free;
  end;
end;

Base64Decode OmniXML, Base64 Delphi 2007. TGraphic, TImage , TGraphic s.

ChooseGraphicClass :

function ChooseGraphicClass(const MimeType: string): TGraphicClass;
begin
  if MimeType = 'image/bmp' then
    Result := TBitmap
  else if MimeType = 'image/png' then
    Result := TPngImage
  else if MimeType = 'image/gif' then
    Result := TGifImage
  else if MimeType = 'image/jpeg' then
    Result := TJpegImage
  else
    raise EUnknownGraphicFormat.Create(MimeType);
end;
+6

Source: https://habr.com/ru/post/1716658/


All Articles