首页 > 解决方案 > Firemonkey中TBitmap的校验和

问题描述

如何在 Firemonkey 中为 TBitmap 创建校验和?(不是 TPicture)。最好直接,而不是保存到 Streams。

我已经对在 Firemonkey 中替换 Scanline 的 Map 进行了一些研究...... 如何直接在 FMX2 中访问 TBitmap 像素(TBitmap.ScanLine 替换)?

但仍然完全迷失了。有谁知道如何使用 FMX 扫描线替换来做到这一点?

我看到下面的 VCL 代码:

在 Delphi 中对图像进行校验和

Function Checksum_CountryFlag(Img : TPicture):Integer;
var j, k, Checksum : Integer;
  Line: PIntegerArray;
  Pixel : integer;
begin
  Checksum := 0;
  Img.Bitmap.PixelFormat:= pf32bit; // just for case
  For k := 0 to Img.Height - 1 do begin      //  !! Height first, -1
    Line:= Img.Bitmap.ScanLine[k];
    For j := 0 to Img.Width - 1 do begin  // !! -1
      Pixel := Line^[j];
      If ((Pixel <> 15577344) And (Pixel <> 15311104) And (Pixel <> 3816255)
        And (Pixel <> 10526623) And (Pixel <> 12303034) And (Pixel <> 9013641)) Then
        begin
          Checksum := Checksum + Pixel;
        end;
      Inc(Line);
    end;
  end;
  Result := Abs(Checksum);
end;

基本上我只需要检查图像(在内存中)是否已经改变(宽度/高度不够,因为大多数时候宽度/高度不会改变,即使图像已经改变),如果我可以对 FMX TBitmap 进行直接校验和。

Qn:如何使用下面的代码对扫描线等效项进行校验和?

procedure TForm1.btnCompareClick(Sender: TObject);
var
  bd1, bd2 : TBitmapData;
  w,h : Integer;
begin
  Image1.Bitmap.LoadFromFile('c:\1.jpg');
  Image2.Bitmap.LoadFromFile('c:\2.jpg');
  w := Image1.Bitmap.Width;   
  h := Image1.Bitmap.Height;
  try
    Image1.Bitmap.Map(TMapAccess.Read, bd1);
    Image2.Bitmap.Map(TMapAccess.Write, bd2);
    AlphaColorToScanline(@PAlphaColorArray(bd1.Data)[0], bd2.Data,
    Round(w * h), Image1.Bitmap.PixelFormat);
  finally
    Image1.Bitmap.Unmap(bd1);
    Image2.Bitmap.Unmap(bd2);
  end;
end;

标签: delphibitmapfiremonkeychecksum

解决方案


推荐阅读