Skip to content

Commit e015237

Browse files
committed
dev
1 parent a9c866e commit e015237

File tree

5 files changed

+178
-210
lines changed

5 files changed

+178
-210
lines changed

SimpleOCR.lpr

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
PSimpleOCR = ^TSimpleOCR;
2323
PFontSet = ^TFontSet;
2424
POCRFilter = ^TOCRFilter;
25+
POCRTarget = ^TOCRTarget;
2526

2627
procedure _LapeFontSet_Create(const Params: PParamArray; const Result: Pointer); cdecl;
2728
begin
@@ -38,24 +39,24 @@ procedure _LapeFontSet_TextToTPA(const Params: PParamArray; const Result: Pointe
3839
PPointArray(Result)^ := PFontSet(Params^[0])^.TextToTPA(PString(Params^[1])^);
3940
end;
4041

41-
procedure _LapeSimpleOCR_LocateText(const Params: PParamArray; const Result: Pointer); cdecl;
42+
procedure _LapeSimpleOCR_Locate(const Params: PParamArray; const Result: Pointer); cdecl;
4243
begin
43-
PSingle(Result)^ := PSimpleOCR(Params^[0])^.LocateText(PString(Params^[1])^, PFontSet(Params^[2])^, POCRFilter(Params^[3])^);
44+
PSingle(Result)^ := PSimpleOCR(Params^[0])^.Locate(POCRTarget(Params^[1])^, PString(Params^[2])^, PFontSet(Params^[3])^, POCRFilter(Params^[4])^);
4445
end;
4546

4647
procedure _LapeSimpleOCR_Recognize(const Params: PParamArray; const Result: Pointer); cdecl;
4748
begin
48-
PString(Result)^ := PSimpleOCR(Params^[0])^.Recognize(POCRFilter(Params^[1])^, PFontSet(Params^[2])^);
49+
PString(Result)^ := PSimpleOCR(Params^[0])^.Recognize(POCRTarget(Params^[1])^, POCRFilter(Params^[2])^, PFontSet(Params^[3])^);
4950
end;
5051

5152
procedure _LapeSimpleOCR_RecognizeStatic(const Params: PParamArray; const Result: Pointer); cdecl;
5253
begin
53-
PString(Result)^ := PSimpleOCR(Params^[0])^.RecognizeStatic(POCRFilter(Params^[1])^, PFontSet(Params^[2])^);
54+
PString(Result)^ := PSimpleOCR(Params^[0])^.RecognizeStatic(POCRTarget(Params^[1])^, POCRFilter(Params^[2])^, PFontSet(Params^[3])^);
5455
end;
5556

5657
procedure _LapeSimpleOCR_RecognizeLines(const Params: PParamArray; const Result: Pointer); cdecl;
5758
begin
58-
PStringArray(Result)^ := PSimpleOCR(Params^[0])^.RecognizeLines(POCRFilter(Params^[1])^, PFontSet(Params^[2])^);
59+
PStringArray(Result)^ := PSimpleOCR(Params^[0])^.RecognizeLines(POCRTarget(Params^[1])^, POCRFilter(Params^[2])^, PFontSet(Params^[3])^);
5960
end;
6061

6162
initialization
@@ -92,10 +93,8 @@ initialization
9293
' end; ' + LineEnding +
9394
' ' + LineEnding +
9495
' ColorFilter: record ' + LineEnding +
95-
' Colors: array of record ' + LineEnding +
96-
' Color: Integer; ' + LineEnding +
97-
' Tolerance: Single; ' + LineEnding +
98-
' end; ' + LineEnding +
96+
' Colors: TIntegerArray; ' + LineEnding +
97+
' Tolerances: TSingleArray; ' + LineEnding +
9998
' Invert: Boolean; ' + LineEnding +
10099
' end; ' + LineEnding +
101100
' ' + LineEnding +
@@ -121,6 +120,8 @@ initialization
121120
'end;',
122121
'TOCRMatch');
123122

123+
addGlobalType('TIntegerMatrix', 'TOCRTarget');
124+
124125
addGlobalType(
125126
'packed record ' + LineEnding +
126127
' Client: TIntegerMatrix; ' + LineEnding +
@@ -132,15 +133,15 @@ initialization
132133
'end;',
133134
'TSimpleOCR');
134135

135-
addGlobalFunc('function TFontSet.Create(FileName: String; SpaceWidth: Integer = 4): TFontSet; static; native;', @_LapeFontSet_Create);
136+
addGlobalFunc('function TFontSet.Create(Directory: String; SpaceWidth: Integer = 4): TFontSet; static; native;', @_LapeFontSet_Create);
136137
addGlobalFunc('function TFontSet.TextToMatrix(Text: String): TIntegerMatrix; native;', @_LapeFontSet_TextToMatrix);
137138
addGlobalFunc('function TFontSet.TextToTPA(Text: String): TPointArray; native;', @_LapeFontSet_TextToTPA);
138139

139-
addGlobalFunc('function TSimpleOCR.Recognize(Filter: TOCRFilter; Font: TFontSet): String; native;', @_LapeSimpleOCR_Recognize);
140-
addGlobalFunc('function TSimpleOCR.RecognizeLines(Filter: TOCRFilter; Font: TFontSet): TStringArray; native;', @_LapeSimpleOCR_RecognizeLines);
141-
addGlobalFunc('function TSimpleOCR.RecognizeStatic(Filter: TOCRFilter; Font: TFontSet): String; native;', @_LapeSimpleOCR_RecognizeStatic);
140+
addGlobalFunc('function TSimpleOCR.Recognize(Target: TOCRTarget; Filter: TOCRFilter; Font: TFontSet): String; native;', @_LapeSimpleOCR_Recognize);
141+
addGlobalFunc('function TSimpleOCR.RecognizeLines(Target: TOCRTarget; Filter: TOCRFilter; Font: TFontSet): TStringArray; native;', @_LapeSimpleOCR_RecognizeLines);
142+
addGlobalFunc('function TSimpleOCR.RecognizeStatic(Target: TOCRTarget; Filter: TOCRFilter; Font: TFontSet): String; native;', @_LapeSimpleOCR_RecognizeStatic);
142143

143-
addGlobalFunc('function TSimpleOCR.LocateText(Text: String; Font: TFontSet; Filter: TOCRFilter): Single; native;', @_LapeSimpleOCR_LocateText);
144+
addGlobalFunc('function TSimpleOCR.Locate(Target: TOCRTarget; Text: String; Font: TFontSet; Filter: TOCRFilter): Single; native;', @_LapeSimpleOCR_Locate);
144145

145146
addCode([
146147
'{$IFDEF SIMPLEOCR_CHECK_SIZES}',
@@ -183,24 +184,15 @@ initialization
183184
' raise "TOCRColorFilter.Create: Length(Colors) <> Length(Tolerances)";',
184185
'',
185186
' Result.FilterType := 1;',
186-
'',
187-
' SetLength(Result.ColorFilter.Colors, Length(Colors));',
188-
' for I := 0 to High(Colors) do',
189-
' begin',
190-
' Result.ColorFilter.Colors[I].Color := Colors[I];',
191-
' Result.ColorFilter.Colors[I].Tolerance := Tolerances[I];',
192-
' end;',
187+
' Result.ColorFilter.Colors := TIntegerArray(Colors);',
188+
' Result.ColorFilter.Tolerances := Tolerances;',
193189
'end;',
194190
'',
195191
'function TOCRColorFilter.Create(Colors: TColorArray): TOCRColorFilter; static; overload;',
196-
'var',
197-
' I: Integer;',
198192
'begin',
199193
' Result.FilterType := 1;',
200-
'',
201-
' SetLength(Result.ColorFilter.Colors, Length(Colors));',
202-
' for I := 0 to High(Colors) do',
203-
' Result.ColorFilter.Colors[I].Color := Colors[I];',
194+
' Result.ColorFilter.Colors := TIntegerArray(Colors);',
195+
' SetLength(Result.ColorFilter.Tolerances, Length(Colors));',
204196
'end;',
205197
'',
206198
'function TOCRInvertColorFilter.Create(Colors: TColorArray; Tolerances: TSingleArray): TOCRInvertColorFilter; static; overload;',
@@ -212,13 +204,8 @@ initialization
212204
'',
213205
' Result.FilterType := 4;',
214206
' Result.ColorFilter.Invert := True;',
215-
'',
216-
' SetLength(Result.ColorFilter.Colors, Length(Colors));',
217-
' for I := 0 to High(Colors) do',
218-
' begin',
219-
' Result.ColorFilter.Colors[I].Color := Colors[I];',
220-
' Result.ColorFilter.Colors[I].Tolerance := Tolerances[I];',
221-
' end;',
207+
' Result.ColorFilter.Colors := TIntegerArray(Colors);',
208+
' Result.ColorFilter.Tolerances := Tolerances;',
222209
'end;',
223210
'',
224211
'function TOCRInvertColorFilter.Create(Colors: TColorArray): TOCRInvertColorFilter; static; overload;',
@@ -227,17 +214,15 @@ initialization
227214
'begin',
228215
' Result.FilterType := 4;',
229216
' Result.ColorFilter.Invert := True;',
230-
'',
231-
' SetLength(Result.ColorFilter.Colors, Length(Colors));',
232-
' for I := 0 to High(Colors) do',
233-
' Result.ColorFilter.Colors[I].Color := Colors[I];',
217+
' Result.ColorFilter.Colors := TIntegerArray(Colors);',
218+
' SetLength(Result.ColorFilter.Tolerances, Length(Colors));',
234219
'end;',
235220
'',
236-
'function TOCRThresholdFilter.Create(Amount: Integer; Invert: Boolean = False): TOCRThresholdFilter; static;',
221+
'function TOCRThresholdFilter.Create(Invert: Boolean = False; C: Integer = 0): TOCRThresholdFilter; static;',
237222
'begin',
238223
' Result.FilterType := 2;',
239-
' Result.ThresholdFilter.Amount := Amount;',
240224
' Result.ThresholdFilter.Invert := Invert;',
225+
' Result.ThresholdFilter.C := C;',
241226
'end;',
242227
'',
243228
'function TOCRShadowFilter.Create(MaxShadowValue: Integer = 25; Tolerance: Single = 5): TOCRShadowFilter; static;',
@@ -249,28 +234,23 @@ initialization
249234
'',
250235
'function TSimpleOCR.Recognize(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet): String; overload;',
251236
'begin',
252-
' Self.Client := TSimpleOCR._GetColorsMatrix(Area);',
253237
' Self.Offset := [Area.X1, Area.Y1];',
254238
'',
255-
' Result := Self.Recognize(Filter, Font);',
239+
' Result := Self.Recognize(TSimpleOCR._GetColorsMatrix(Area), Filter, Font);',
256240
'end;',
257241
'',
258242
'function TSimpleOCR.RecognizeStatic(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet): String; overload;',
259243
'begin',
260-
' Self.Client := TSimpleOCR._GetColorsMatrix(Area);',
261244
' Self.Offset := [Area.X1, Area.Y1];',
262245
'',
263-
' Result := Self.RecognizeStatic(Filter, Font);',
246+
' Result := Self.RecognizeStatic(TSimpleOCR._GetColorsMatrix(Area), Filter, Font);',
264247
'end;',
265248
'',
266249
'function TSimpleOCR.RecognizeLines(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet): TStringArray; overload;',
267-
'var',
268-
' I: Integer;',
269250
'begin',
270-
' Self.Client := TSimpleOCR._GetColorsMatrix(Area);',
271251
' Self.Offset := [Area.X1, Area.Y1];',
272252
'',
273-
' Result := Self.RecognizeLines(Filter, Font);',
253+
' Result := Self.RecognizeLines(TSimpleOCR._GetColorsMatrix(Area), Filter, Font);',
274254
'end;',
275255
'',
276256
'function TSimpleOCR.RecognizeNumber(Area: TBox; Filter: TOCRFilter; constref Font: TFontSet): Int64;',
@@ -288,12 +268,11 @@ initialization
288268
' Result := StrToInt64(Text);',
289269
'end;',
290270
'',
291-
'function TSimpleOCR.LocateText(Area: TBox; Text: String; constref Font: TFontSet; Filter: TOCRFilter): Single; overload;',
271+
'function TSimpleOCR.Locate(Area: TBox; Text: String; constref Font: TFontSet; Filter: TOCRFilter): Single; overload;',
292272
'begin',
293-
' Self.Client := TSimpleOCR._GetColorsMatrix(Area);',
294273
' Self.Offset := [Area.X1, Area.Y1];',
295274
'',
296-
' Result := Self.LocateText(Text, Font, Filter);',
275+
' Result := Self.Locate(TSimpleOCR._GetColorsMatrix(Area), Text, Font, Filter);',
297276
'end;'
298277
]);
299278

simpleocr.base.pas

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ interface
1414
Classes, SysUtils,
1515
IntfGraphics;
1616

17+
const
18+
ALPHA_NUMERIC_SYM = [
19+
'a'..'z', 'A'..'Z', '0'..'9',
20+
'%', '&', '#', '$', '[', ']', '{', '}', '@', '!', '?'
21+
];
22+
1723
type
1824
TStringArray = array of String;
1925
TIntegerArray = array of Integer;
@@ -48,8 +54,11 @@ TColorRGBA = record
4854
property Height: Integer read GetHeight;
4955
end;
5056

57+
function ContainsAlphaNumSym(const Text: string): Boolean; inline;
58+
5159
procedure Swap(var A, B: TPoint); inline;
5260
procedure Swap(var A, B: Integer); inline;
61+
procedure Swap(var A, B: TColorRGBA); inline;
5362

5463
function TPABounds(const TPA: TPointArray): TBox;
5564
function InvertTPA(const TPA: TPointArray): TPointArray;
@@ -82,6 +91,15 @@ implementation
8291
uses
8392
GraphType, Graphics;
8493

94+
function ContainsAlphaNumSym(const Text: string): Boolean;
95+
var i: Int32;
96+
begin
97+
Result := False;
98+
for i:=1 to Length(text) do
99+
if Text[i] in ALPHA_NUMERIC_SYM then
100+
Exit(True);
101+
end;
102+
85103
procedure Swap(var A, B: TPoint);
86104
var
87105
C: TPoint;
@@ -100,6 +118,15 @@ procedure Swap(var A, B: Integer);
100118
B := C;
101119
end;
102120

121+
procedure Swap(var A, B: TColorRGBA);
122+
var
123+
C: TColorRGBA;
124+
begin
125+
C := A;
126+
A := B;
127+
B := C;
128+
end;
129+
103130
function TPABounds(const TPA: TPointArray): TBox;
104131
var
105132
I, L: Integer;

0 commit comments

Comments
 (0)