function TForm1.CalPercent(const BitmapSouce: TBitmap; valve: integer): double; var
p, p1: PByteArray;
x, y: Integer;
Bmp: TBitmap;
Gray: byte;
WhiteNum, BlackNum: integer;
begin
WhiteNum := 0;
BlackNum := 0;
Bmp := TBitmap.Create;
Bmp.Assign(BitmapSouce);
Bmp.PixelFormat := pf24Bit;
for y := 0 to Bmp.Height - 1 do
begin
p := Bmp.scanline[y];
p1 := BitmapSouce.scanline[y];
for x := 0 to Bmp.Width - 1 do
begin
Gray := (p1[x * 3 + 2] * 299 + p1[x * 3 + 1] * 587 + p1[x * 3] * 114) div 1000;
if gray > valve then
begin
gray := 255;
inc(WhiteNum)
end
else
begin
gray := 0;
inc(BlackNum);
end ;