在 Firemonkey 中使用 JPG

Working with JPG into Firemonkey

我有一个压缩图像,我正在尝试接收它并在 android 平台上显示它,在 vcl 中我一直在做类似

的事情
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;

但我不能对 android 做同样的事情,因为没有 TJPEGIMAGE 库的声明 我不确定我是否可以在 [=13] 上做一些替代 JPEG 的事情=] 我对我必须做什么感到困惑

VCL 使用 TGraphic 派生的 类 来处理单个图像类型。但是,FireMonkey 中没有 TGraphic 的等价物。完整列表只有一个 TBitmap class that supports multiple image types. Different FMX platforms support a different subset of image types (see Supported Image Formats)。幸运的是,JPG 是所有 FMX 平台都支持的仅有的两种图像类型之一(另一种是 PNG)。

您的 VCL 代码的 FMX 等效项如下所示:

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;