delphifiremonkeydelphi-10-seattle

Working with JPG into Firemonkey


i have a compressed image i am trying to receive it and show it on android plat-from , in vcl i have been doing something like following

procedure TForm1.CreateJpg(Data: string);
var
  JpegStream: TMemoryStream;
  JpegImage: TJPEGImage;
  Bitmap: TBitmap;
  tmpPos, tmpLen: integer;
  pp: string;
begin

  try
    tmpPos := Pos('B]>', Data);
    pp := Copy(Data, 5, tmpPos - 5);
    tmpLen := StrToInt(pp);
    Data := Copy(Data, tmpPos + 3, tmpLen);
    Bitmap := TBitmap.Create;

    try
      JpegImage := TJPEGImage.Create;

      try
        JpegStream := TMemoryStream.Create;

        try
          TIdDecoderMIME.DecodeStream(Data, JpegStream);
          JpegStream.Position := 0;
          JpegImage.LoadFromStream(JpegStream);
        finally
          JpegStream.Free;
        end;

        with Bitmap do
        begin
          Canvas.Lock;

          try
            Width := JpegImage.Width;
            Height := JpegImage.Height;
            Canvas.StretchDraw(rect(0, 0, 200, 160), JpegImage);
          finally
            Canvas.Unlock;
          end;
        end;
      finally
        JpegImage.Free;
      end;

      img.Assign(Bitmap);
    finally
      Bitmap.Free;
    end;
  except
    on E: Exception do
      //
  end;
end;

but i cannot do the same into android because there is no declaration of TJPEGIMAGE library i am not sure if i can do something as alternative of JPEG on Fire-monkey i am confused about what i have to do


Solution

  • The VCL uses TGraphic-derived classes to handle individual image types. However, there is no equivalent of TGraphic in FireMonkey. It has only a single TBitmap class that supports multiple image types. Different FMX platforms support a different subset of image types (see Supported Image Formats for the complete list). Fortunately, JPG is one of only two image types that are supported on all FMX platforms (PNG is the other).

    The FMX equivalent of your VCL code would look something like this:

    procedure TForm1.CreateJpg(Data: string);
    var
      JpegStream: TMemoryStream;
      Jpeg, Bitmap: TBitmap;
      tmpPos, tmpLen: integer;
      pp: string;
    begin
      try
        tmpPos := Pos('B]>', Data);
        pp := Copy(Data, 5, tmpPos - 5);
        tmpLen := StrToInt(pp);
        Data := Copy(Data, tmpPos + 3, tmpLen);
    
        Bitmap := TBitmap.Create;
        try
          Jpeg := TBitmap.Create;
          try
            JpegStream := TMemoryStream.Create;
            try
              TIdDecoderMIME.DecodeStream(Data, JpegStream);
              JpegStream.Position := 0;
              Jpeg.LoadFromStream(JpegStream);
            finally
              JpegStream.Free;
            end;
    
            with Bitmap do
            begin
              SetSize(Jpeg.Width, Jpeg.Height);
    
              if Canvas.BeginScene then
              try
                Canvas.DrawBitmap(Jpeg,
                    {$IF RTLVersion >= 31} // 10.1 Berlin or higher
                    Jpeg.BoundsF,
                    {$ELSE}
                    TRectF.Create(0, 0, Jpeg.Width, Jpeg.Height),
                    {$IFEND}
                    TRectF.Create(0, 0, 200, 160), 1.0);
              finally
                Canvas.EndScene;
              end;
            end;
          finally
            Jpeg.Free;
          end;
    
          img.Bitmap.Assign(Bitmap);
        finally
          Bitmap.Free;
        end;
      except
        on E: Exception do
          //
      end;
    end;