Skip to content

Commit 5b4b923

Browse files
committed
Add RecognizeNumber
Rename a recognize overload to RecognizeTPA
1 parent 2a48b99 commit 5b4b923

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

SimpleOCR.lpr

+40-30
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ procedure TSimpleOCR_Recognize(const Params: PParamArray; const Result: Pointer)
2525

2626
initialization
2727
addGlobalType('packed record' + LineEnding +
28-
' Character: AnsiChar;' + LineEnding +
28+
' Character: Char;' + LineEnding +
2929
' Width, Height: Int32;' + LineEnding +
3030
' Loaded, HasShadow: Boolean;' + LineEnding +
3131
' CharacterPoints: TPointArray;' + LineEnding +
@@ -46,7 +46,7 @@ initialization
4646
addGlobalType('packed record' + LineEnding +
4747
' Color, Tolerance: Int32;' + LineEnding +
4848
' UseShadow: Boolean;' + LineEnding +
49-
' ShadowMaxValue: Int32;' + LineEnding +
49+
' ShadowMaxValue: Int32;' + LineEnding +
5050
' Threshold: Boolean;' + LineEnding +
5151
' ThresholdAmount: Int32;' + LineEnding +
5252
' ThresholdInvert: Boolean;' + LineEnding +
@@ -63,35 +63,45 @@ initialization
6363
'TSimpleOCR');
6464

6565
addGlobalFunc('procedure TFontSet.Load(Font: String; Space: Int32 = 4); native;', @TFontSet_Load);
66-
addGlobalFunc('function TSimpleOCR.Recognize(constref Client: T2DIntegerArray; constref Filter: TCompareRules; constref Font: TFontSet; IsStatic: Boolean = False; MaxWalk: Int32 = 40): String; overload; native;', @TSimpleOCR_Recognize);
66+
addGlobalFunc('function TSimpleOCR.Recognize(Image: T2DIntegerArray; Filter: TCompareRules; constref Font: TFontSet; IsStatic: Boolean = False; MaxWalk: Int32 = 40): String; overload; native;', @TSimpleOCR_Recognize);
6767

68-
addCode('function TSimpleOCR.Recognize(B: TBox; Filter: TCompareRules; constref Font: TFontSet; IsStatic: Boolean = False; MaxWalk: Int32 = 40): AnsiString; overload;' + LineEnding +
69-
'begin' + LineEnding +
70-
' Result := Self.Recognize(GetColorsMatrix(B.X1, B.Y1, B.X2, B.Y2), Filter, Font, IsStatic, MaxWalk);' + LineEnding +
71-
'end;' + LineEnding +
72-
'' + LineEnding +
73-
'function TSimpleOCR.Recognize(constref TPA: TPointArray; constref Font: TFontSet; MaxWalk: Int32 = 40): String; overload;' + LineEnding +
74-
'var' + LineEnding +
75-
' Matrix: T2DIntegerArray;' + LineEnding +
76-
' B: TBox;' + LineEnding +
77-
' I: Int32;' + LineEnding +
78-
'begin' + LineEnding +
79-
' B := GetTPABounds(TPA);' + LineEnding +
80-
'' + LineEnding +
81-
' SetLength(Matrix, B.Y2 - B.Y1 + 1, B.X2 - B.X1 + 1);' + LineEnding +
82-
' for I := 0 to High(TPA) do' + LineEnding +
83-
' Matrix[TPA[I].Y - B.Y1][TPA[I].X - B.X1] := 255;' + LineEnding +
84-
'' + LineEnding +
85-
' Result := Self.Recognize(Matrix, [255], Font, False, MaxWalk);' + LineEnding +
86-
'end;' + LineEnding +
87-
'' + LineEnding +
88-
'procedure TFontSet.Load(Font: String; Space: Int32 = 4); override;' + LineEnding +
89-
'begin' + LineEnding +
90-
' if not DirectoryExists(Font) then' + LineEnding +
91-
' raise "Font directory does not exist: " + Font;' + LineEnding +
92-
' inherited();' + LineEnding +
93-
' if Length(Self.Data) = 0 then' + LineEnding +
94-
' raise "Failed to load font: " + Font;' + LineEnding +
68+
addCode('function TSimpleOCR.Recognize(B: TBox; Filter: TCompareRules; constref Font: TFontSet; IsStatic: Boolean = False; MaxWalk: Int32 = 40): String; overload;' + LineEnding +
69+
'begin' + LineEnding +
70+
' Result := Self.Recognize(GetColorsMatrix(B.X1, B.Y1, B.X2, B.Y2), Filter, Font, IsStatic, MaxWalk);' + LineEnding +
71+
'end;' + LineEnding +
72+
'' + LineEnding +
73+
'function TSimpleOCR.RecognizeNumber(B: TBox; Filter: TCompareRules; constref Font: TFontSet; IsStatic: Boolean = False; MaxWalk: Int32 = 40): Int64;' + LineEnding +
74+
'var' + LineEnding +
75+
' Text: String;' + LineEnding +
76+
'begin' + LineEnding +
77+
' Text := Self.Recognize(B, Filter, Font, IsStatic, MaxWalk);' + LineEnding +
78+
' Text := StringReplace(Text, "O", "0", [rfReplaceAll]);' + LineEnding +
79+
'' + LineEnding +
80+
' Result := StrToIntDef(ExtractFromStr(Text, Numbers), -1);' + LineEnding +
81+
'end;' + LineEnding +
82+
'' + LineEnding +
83+
'function TSimpleOCR.RecognizeTPA(constref TPA: TPointArray; constref Font: TFontSet; MaxWalk: Int32 = 40): String;' + LineEnding +
84+
'var' + LineEnding +
85+
' Matrix: T2DIntegerArray;' + LineEnding +
86+
' B: TBox;' + LineEnding +
87+
' I: Int32;' + LineEnding +
88+
'begin' + LineEnding +
89+
' B := GetTPABounds(TPA);' + LineEnding +
90+
'' + LineEnding +
91+
' SetLength(Matrix, B.Y2 - B.Y1 + 1, B.X2 - B.X1 + 1);' + LineEnding +
92+
' for I := 0 to High(TPA) do' + LineEnding +
93+
' Matrix[TPA[I].Y - B.Y1][TPA[I].X - B.X1] := 255;' + LineEnding +
94+
'' + LineEnding +
95+
' Result := Self.Recognize(Matrix, [255], Font, False, MaxWalk);' + LineEnding +
96+
'end;' + LineEnding +
97+
'' + LineEnding +
98+
'procedure TFontSet.Load(Font: String; Space: Int32 = 4); override;' + LineEnding +
99+
'begin' + LineEnding +
100+
' if not DirectoryExists(Font) then' + LineEnding +
101+
' raise "Font directory does not exist: " + Font;' + LineEnding +
102+
' inherited();' + LineEnding +
103+
' if Length(Self.Data) = 0 then' + LineEnding +
104+
' raise "Failed to load font: " + Font;' + LineEnding +
95105
'end;'
96106
);
97107

simpleocr.engine.pas

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface
1717
type
1818
PFontChar = ^TFontChar;
1919
TFontChar = packed record
20-
Character: AnsiChar;
20+
Character: Char;
2121
Width, Height: Int32;
2222
Loaded, HasShadow: Boolean;
2323
CharacterPoints: TPointArray;

0 commit comments

Comments
 (0)