delphi - 如何裁剪 FMX TBitmap

标签 delphi bitmap crop firemonkey 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,但名称很神秘。

关于delphi - 如何裁剪 FMX TBitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31159795/

相关文章:

javascript - Cropper 在使用 dropzone js 上传之前裁剪多个图像

delphi - Delphi选项页面中设置浏览路径的问题

delphi - 具体是什么导致 EPrivilege 提高?

c# - 在 WriteableBitmap 上绘制文本

c# - 如何修复 System.Drawing.dll 中的控制台应用程序异常 "' System.ArgumentException'

swift - 裁剪后的图像不清晰快速

delphi - 将 Team Coherence 版本标签合并到构建中

windows - 当用户不使用 "Safely Remove Hardware"时,如何确保文件永久保存在 USB 上?

c++ - c++ 中的位图 - unsigned int 会完成这项工作吗?

ffmpeg:缩放输出裁剪的宽度/高度不起作用