简体   繁体   English

如何裁剪 FMX TBitmap

[英]How to crop an FMX TBitmap

I receive a bitmap via TCameraComponent.SampleBufferReady event.我通过 TCameraComponent.SampleBufferReady 事件收到一个位图。 Then I need to crop the received image so that I get a, for instance, recangular image.然后我需要裁剪接收到的图像,以便我得到一个,例如,矩形图像。

I calculate the necessary parameters in the following method:我用以下方法计算必要的参数:

procedure TPersonalF.SampleBufferReady(Sender: TObject;
  const ATime: TMediaTime);
var
  BMP: TBitmap;
  X, Y, W, H: Word;
begin
  Try
    BMP := TBitmap.Create;
    CameraComponent.SampleBufferToBitmap(BMP, true);
    if BMP.Width >= BMP.Height then //landscape
    begin
      W:=BMP.Height;
      H:=W;
      Y:=0;
      X:=trunc((BMP.Width-BMP.Height)/2);
    end
    else //portrait
    begin
      W:=BMP.Width;
      H:=W;
      X:=0;
      Y:=trunc((BMP.Height-BMP.Width)/2);
    end;
    CropBitmap(BMP, Image1.Bitmap, X,Y,W,H);
  Finally
    BMP.Free;
  End;
end; 

I found an answer by @RRUZ delphi-how-do-i-crop-a-bitmap-in-place , but it requires a VCL API handle and is uses a Windows GDI function:我找到了 @RRUZ delphi-how-do-i-crop-a-bitmap-in-place的答案,但它需要一个 VCL API 句柄并且使用 Windows GDI 函数:

procedure CropBitmap(InBitmap, OutBitMap: TBitmap; X, Y, W, H: Word);
  begin
    OutBitMap.PixelFormat := InBitmap.PixelFormat;
    OutBitMap.Width := W;
    OutBitMap.Height := H;
    BitBlt(OutBitMap.Canvas.Handle, 0, 0, W, H, InBitmap.Canvas.Handle, X,
      Y, SRCCOPY);
  end;

My project is using FMX, and I plan to port it to Android platform in the future.我的项目使用的是FMX,未来打算移植到Android平台。 So I am expecting to get problems if I use handles.因此,如果我使用句柄,我预计会出现问题。 How can I solve this problem?我怎么解决这个问题?

Assuming you can guarantee that InBitmap and OutBitMap exist (if not, you can handle error checking yourself)假设你可以保证 InBitmap 和 OutBitMap 存在(如果没有,你可以自己处理错误检查)

procedure CropBitmap(InBitmap, OutBitMap: TBitmap; X, Y, W, H: Word);
var
  iRect : TRect;
begin
    OutBitMap.PixelFormat := InBitmap.PixelFormat;
    OutBitMap.Width := W;
    OutBitMap.Height := H;
    iRec.Left := 0;
    iRect.Top := 0;
    iRect.Width := W;
    iRect.Height := H;
    OutBitMap.CopyFromBitmap( InBitMap, iRect, 0, 0 );
end;

It is the same as the original but uses Firemonkey CopyFromBitmap which is similar to the Windows rather cryptically named BitBlt.它与原始版本相同,但使用 Firemonkey CopyFromBitmap,它类似于 Windows 中的 BitBlt。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM