如何裁剪FMX TBitmap [英] How to crop an FMX TBitmap

查看:263
本文介绍了如何裁剪FMX TBitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I receive a bitmap via TCameraComponent.SampleBufferReady event. 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; 

我通过@RRUZ找到了答案 delphi-how-do-i-crop-a-bitmap-in-place ,但是它需要VCL API句柄,并且使用Windows GDI函数:

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:

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平台.因此,如果使用手柄,我期望会遇到问题.我怎么解决这个问题?

My project is using FMX, and I plan to port it to Android platform in the future. So I am expecting to get problems if I use handles. How can I solve this problem?

推荐答案

假设您可以保证InBitmap和OutBitMap存在(如果不存在,则可以自己进行错误检查)

Assuming you can guarantee that InBitmap and OutBitMap exist (if not, you can handle error checking yourself)

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.

It is the same as the original but uses Firemonkey CopyFromBitmap which is similar to the Windows rather cryptically named BitBlt.

这篇关于如何裁剪FMX TBitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆