簡體   English   中英

如何裁剪 FMX TBitmap

[英]How to crop an FMX TBitmap

我通過 TCameraComponent.SampleBufferReady 事件收到一個位圖。 然后我需要裁剪接收到的圖像,以便我得到一個,例如,矩形圖像。

我用以下方法計算必要的參數:

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; 

我找到了 @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;

我的項目使用的是FMX,未來打算移植到Android平台。 因此,如果我使用句柄,我預計會出現問題。 我怎么解決這個問題?

假設你可以保證 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;

它與原始版本相同,但使用 Firemonkey CopyFromBitmap,它類似於 Windows 中的 BitBlt。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM