我通过 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 平台。因此,如果我使用句柄,我预计会遇到问题。我怎么解决这个问题?