@@ -86,6 +86,9 @@ interface
86
86
end ;
87
87
88
88
MinCharacterMatch: Char;
89
+
90
+ function IsEmpty : Boolean;
91
+ class function Empty : TOCRFilter; static;
89
92
end ;
90
93
91
94
PSimpleOCR = ^TSimpleOCR;
@@ -97,8 +100,7 @@ interface
97
100
FHeight: Integer;
98
101
FSearchArea: TBox;
99
102
100
- function Init (const FontSet: TFontSet; const Static: Boolean): Boolean;
101
- function Init (const FontSet: TFontSet; const Filter: TOCRFilter): Boolean;
103
+ function Init (const FontSet: TFontSet; const Filter: TOCRFilter; Static: Boolean): Boolean;
102
104
103
105
function _RecognizeX (Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
104
106
function _RecognizeXY (Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
@@ -124,6 +126,16 @@ implementation
124
126
graphtype, intfgraphics, graphics, lazfileutils, math,
125
127
simpleocr.filters;
126
128
129
+ function TOCRFilter.IsEmpty : Boolean;
130
+ begin
131
+ Result := CompareMem(@Self, @Self.Empty, SizeOf(Self));
132
+ end ;
133
+
134
+ class function TOCRFilter.Empty : TOCRFilter;
135
+ begin
136
+ Result := Default(TOCRFilter);
137
+ end ;
138
+
127
139
function TFontSet.GetCharacterPoints (Character: Char): Integer;
128
140
begin
129
141
if (Character in [FONTSET_START..FONTSET_END]) then
@@ -163,6 +175,11 @@ procedure TFontSet.Load(FontPath: String; Space: Integer);
163
175
FontChar: TFontCharacter;
164
176
begin
165
177
FontPath := IncludeTrailingPathDelimiter(ExpandFileName(FontPath));
178
+ if (not DirectoryExists(FontPath)) then
179
+ begin
180
+ WriteLn(' TFontSet.Load: Font does not exist "' + FontPath + ' "' );
181
+ Halt(1 );
182
+ end ;
166
183
167
184
Self := Default(TFontSet);
168
185
Self.Name := ExtractFileNameOnly(FontPath);
@@ -227,17 +244,33 @@ procedure TFontSet.Load(FontPath: String; Space: Integer);
227
244
Image.Free();
228
245
end ;
229
246
230
- function TSimpleOCR.Init (const FontSet: TFontSet; const Static: Boolean): Boolean;
247
+ function TSimpleOCR.Init (const FontSet: TFontSet; const Filter: TOCRFilter; Static: Boolean): Boolean;
231
248
begin
232
249
Result := MatrixDimensions(FClient, FWidth, FHeight);
233
250
234
251
if Result then
235
252
begin
236
253
FFontSet := FontSet;
237
- FSearchArea := Box(0 , 0 , FWidth - 1 , FHeight - 1 );
238
254
239
- if not Static then
255
+ if not Filter.IsEmpty() then
256
+ begin
257
+ case Filter.FilterType of
258
+ EOCRFilterType.COLOR, EOCRFilterType.INVERT_COLOR:
259
+ Result := SimpleOCRFilter.ApplyColorRule(FClient, TColorRuleArray(Filter.ColorRule.Colors), Filter.ColorRule.Invert, FSearchArea);
260
+
261
+ EOCRFilterType.THRESHOLD:
262
+ Result := SimpleOCRFilter.ApplyThresholdRule(FClient, Filter.ThresholdRule.Invert, Filter.ThresholdRule.Amount, FSearchArea);
263
+
264
+ EOCRFilterType.SHADOW:
265
+ Result := SimpleOCRFilter.ApplyShadowRule(FClient, Filter.ShadowRule.MaxShadowValue, Filter.ShadowRule.Tolerance, FSearchArea);
266
+ end ;
267
+ end ;
268
+
269
+ if Static then
270
+ FSearchArea := Box(0 , 0 , FWidth - 1 , FHeight - 1 )
271
+ else
240
272
begin
273
+ // Filter sets the bounds
241
274
FSearchArea.X1 -= FontSet.MaxWidth div 2 ;
242
275
FSearchArea.Y1 -= FFontSet.MaxHeight div 2 ;
243
276
FSearchArea.X2 += FontSet.MaxWidth div 2 ;
@@ -246,33 +279,6 @@ function TSimpleOCR.Init(const FontSet: TFontSet; const Static: Boolean): Boolea
246
279
end ;
247
280
end ;
248
281
249
- function TSimpleOCR.Init (const FontSet: TFontSet; const Filter: TOCRFilter): Boolean;
250
- begin
251
- Result := MatrixDimensions(FClient, FWidth, FHeight);
252
-
253
- if Result then
254
- begin
255
- FFontSet := FontSet;
256
-
257
- case Filter.FilterType of
258
- EOCRFilterType.COLOR,
259
- EOCRFilterType.INVERT_COLOR:
260
- Result := SimpleOCRFilter.ApplyColorRule(FClient, TColorRuleArray(Filter.ColorRule.Colors), Filter.ColorRule.Invert, FSearchArea);
261
-
262
- EOCRFilterType.THRESHOLD:
263
- Result := SimpleOCRFilter.ApplyThresholdRule(FClient, Filter.ThresholdRule.Invert, Filter.ThresholdRule.Amount, FSearchArea);
264
-
265
- EOCRFilterType.SHADOW:
266
- Result := SimpleOCRFilter.ApplyShadowRule(FClient, Filter.ShadowRule.MaxShadowValue, Filter.ShadowRule.Tolerance, FSearchArea);
267
- end ;
268
-
269
- FSearchArea.X1 -= FontSet.MaxWidth div 2 ;
270
- FSearchArea.Y1 -= FFontSet.MaxHeight div 2 ;
271
- FSearchArea.X2 += FontSet.MaxWidth div 2 ;
272
- FSearchArea.Y2 += FFontSet.MaxHeight div 2 ;
273
- end ;
274
- end ;
275
-
276
282
function TSimpleOCR._RecognizeX (Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
277
283
278
284
function CompareChar (const Character: TFontCharacter; const OffsetX, OffsetY: Integer): Integer; inline;
@@ -521,7 +527,7 @@ function TSimpleOCR.LocateText(const Text: String; const FontSet: TFontSet; out
521
527
TextMatrix := Self.TextToMatrix(Text, FontSet);
522
528
if not MatrixDimensions(TextMatrix, TextWidth, TextHeight) then
523
529
Exit;
524
- if not Self.Init(FontSet, False ) then
530
+ if not Self.Init(FontSet, TOCRFilter.Empty, True ) then
525
531
Exit;
526
532
527
533
SetLength(CharacterIndices, TextWidth * TextHeight);
@@ -609,7 +615,7 @@ function TSimpleOCR.LocateText(const Text: String; const FontSet: TFontSet; out
609
615
function TSimpleOCR.LocateText (const Text: String; const FontSet: TFontSet; const Filter: TOCRFilter; out Bounds: TBox): Single;
610
616
begin
611
617
Result := 0 ;
612
- if Self.Init(FontSet, Filter) then
618
+ if Self.Init(FontSet, Filter, True ) then
613
619
Result := LocateText(Text, FontSet, Bounds);
614
620
end ;
615
621
@@ -619,7 +625,7 @@ function TSimpleOCR.Recognize(const Filter: TOCRFilter; const FontSet: TFontSet)
619
625
Bounds: TBox;
620
626
begin
621
627
Result := ' ' ;
622
- if Self.Init(FontSet, Filter) then
628
+ if Self.Init(FontSet, Filter, False ) then
623
629
Result := _RecognizeXY(FSearchArea, FontSet.CharacterPoints[Filter.MinCharacterMatch], $FFFFFF, Hits, Bounds);
624
630
end ;
625
631
@@ -719,7 +725,7 @@ function TSimpleOCR.RecognizeUpText(const Filter: TOCRFilter; const FontSet: TFo
719
725
Halt(1 );
720
726
end ;
721
727
722
- if Self.Init(FontSet, True) then
728
+ if Self.Init(FontSet, TOCRFilter.Empty, True) then
723
729
begin
724
730
MinPointsNeeded := FontSet.CharacterPoints[Filter.MinCharacterMatch];
725
731
Space := 0 ;
@@ -767,7 +773,7 @@ function TSimpleOCR.RecognizeStatic(const Filter: TOCRFilter; const FontSet: TFo
767
773
Bounds: TBox;
768
774
begin
769
775
Result := ' ' ;
770
- if Self.Init(FontSet, Filter) then
776
+ if Self.Init(FontSet, Filter, True ) then
771
777
Result := Self._RecognizeX(FSearchArea, FontSet.CharacterPoints[Filter.MinCharacterMatch], MaxWalk, Hits, Bounds);
772
778
end ;
773
779
@@ -781,7 +787,7 @@ function TSimpleOCR.RecognizeLines(const Filter: TOCRFilter; const FontSet: TFon
781
787
Result := nil ;
782
788
TextBounds := nil ;
783
789
784
- if Self.Init(FontSet, Filter) then
790
+ if Self.Init(FontSet, Filter, False ) then
785
791
begin
786
792
MinCharacterPoints := FontSet.CharacterPoints[' ,' ];
787
793
0 commit comments