Delphi XE2 之 FireMonkey 入门(16) - 滤镜: 实例测试


窗体上需要 TImage、TOpenDialog 和六个按钮.
unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Objects;

type
  TForm1 = class(TForm)
    Image1: TImage;
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses FMX.Filter;

var
  bit: TBitmap;

procedure TForm1.FormCreate(Sender: TObject);
begin
  bit := TBitmap.Create(0, 0);
  if not OpenDialog1.Execute then Exit;
  bit.LoadFromFile(OpenDialog1.FileName);
  Image1.Bitmap.Assign(bit);
  Image1.SetBounds(10, 10, bit.Width, bit.Height);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  bit.Free;
end;

{高斯模糊: 一个参数}
procedure TForm1.Button1Click(Sender: TObject);
var
  filter: TFilter;
begin
  filter := FilterByName('GaussianBlur');           //GaussianBlur 是高斯模糊滤镜的名称
  filter.Values['BlurAmount'] := 0.25;              //BlurAmount 是高斯模糊参数的名称, 默认值是 1(0.1..10)
  filter.ValuesAsBitmap['input'] := bit;            //input  是给输入图片约定的索引名称, 不区分大小写
  Image1.Bitmap := filter.ValuesAsBitmap['output']; //output 是给输出图片约定的索引名称
end;

{颜色翻转: 无参数}
procedure TForm1.Button2Click(Sender: TObject);
var
  filter: TFilter;
begin
  filter := FilterByName('Invert');       //颜色翻转
  filter.ValuesAsBitmap['input'] := bit;
  Image1.Bitmap := filter.ValuesAsBitmap['output'];
end;

{灰度: 无参数}
procedure TForm1.Button3Click(Sender: TObject);
var
  filter: TFilter;
begin
  filter := FilterByName('Monochrome');
  filter.ValuesAsBitmap['input'] := bit;
  Image1.Bitmap := filter.ValuesAsBitmap['output'];
end;

{马赛克: 一个参数}
procedure TForm1.Button4Click(Sender: TObject);
var
  filter: TFilter;
begin
  filter := FilterByName('Pixelate');
  filter.Values['BlockCount'] := 30; {25 : 1..1000}
  filter.ValuesAsBitmap['input'] := bit;
  Image1.Bitmap := filter.ValuesAsBitmap['output'];
end;

{对比度: 两个参数}
procedure TForm1.Button5Click(Sender: TObject);
var
  filter: TFilter;
begin
  filter := FilterByName('Contrast');
  filter.Values['Brightness'] := -0.5; {-1..1}
  filter.Values['Contrast']   := 1.5;  {0..2}
  filter.ValuesAsBitmap['input'] := bit;
  Image1.Bitmap := filter.ValuesAsBitmap['output'];
end;

{透视: 参数是四个点}
procedure TForm1.Button6Click(Sender: TObject);
var
  filter: TFilter;
begin
  filter := FilterByName('PerspectiveTransform');
  filter.Values['TopLeft'] := VarFromPointXY(0, 0);
  filter.Values['TopRight'] := VarFromPointXY(bit.Width, 0);
  filter.Values['BottomRight'] := VarFromPointXY(bit.Width * 2, bit.Height * 2);
  filter.Values['BottomLeft'] := VarFromPointXY(0, bit.Height);
  filter.ValuesAsBitmap['input'] := bit;
  Image1.Bitmap := filter.ValuesAsBitmap['output'];
  Image1.SetBounds(10, 10, bit.Width * 2, bit.Height * 2);
end;

end.


Transition 分类的滤镜需要两张图片:

uses FMX.Filter;

var
  bit1,bit2: TBitmap;

{百叶窗效果}
procedure TForm1.FormCreate(Sender: TObject);
var
  filter: TFilter;
begin
  bit1 := TBitmap.Create(0, 0);
  bit2 := TBitmap.Create(0, 0);
  bit1.LoadFromFile('C:\Temp\test1.jpg');
  bit2.LoadFromFile('C:\Temp\test2.jpg');

  filter := FilterByName('BlindTransition');
  filter.Values['Progress'] := 50;       //变换进度: 0..100; 当以此进度做动画时, PropertyName := 'Value'
  filter.Values['NumberOfBlinds'] := 10; //分叶数: 2..15

  filter.ValuesAsBitmap['input'] := bit1;
  filter.ValuesAsBitmap['target'] := bit2; //target 也是约定的索引名称
  Image1.Bitmap := filter.ValuesAsBitmap['output'];
  Image1.SetBounds(10, 10, bit1.Width, bit1.Height);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  bit1.Free;
  bit2.Free;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值