我通过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句柄,并且使用Windows函数:
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平台。所以,如果我使用句柄,我就会遇到问题。我该如何解决这个问题?
发布于 2015-07-01 15:26:06
假设您可以保证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,非常神秘地命名为BitBlt。
https://stackoverflow.com/questions/31159795
复制相似问题