-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOCR3.java
More file actions
2180 lines (1777 loc) · 81.3 KB
/
OCR3.java
File metadata and controls
2180 lines (1777 loc) · 81.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ocr3;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.Separator;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToolBar;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
/**
*
* @author gsb
*/
public class OCR3 extends Application {
Scene scene;
HBox root = new HBox();
double prefSceneHeight = 600;
double prefSceneWidth = 1200;
ToolBar toolbarLeft;
ToolBar toolbarRight;
Text imageText;
//image width and height
//To be initialized at getHalfSizedCanvasImage() method.
int iWidth, iHeight;
int blackMinX = 10000;
int blackMinY = 10000; // top black row
int blackMaxX = 0;
int blackMaxY = 0; // bottom black row
int bwidth = 0;
int bheight = 0;
CharacterModel cModel = new CharacterModel();
ArrayList<Integer[]> yBlockSize;
ArrayList<Integer[]> bpBlockList;
String characterKey = "";
// String text = "";
Color black = Color.color(0,0,0,1);
int blockPositionStored = 0;
int blockCount=0;
int blockType = 0;
boolean blockType1 =false;
boolean blockType2 = false;
boolean blockType3 = false;
boolean blockType4 = false;
boolean blockType5 = false;
boolean blockType6 = false;
boolean blockType7 = false;
boolean blockType8 = false;
boolean blockType9 = false;
boolean blockType10 = false;
boolean blockType11 = false;
boolean blockType12 = false;
boolean blockTypeUnknown = false;
boolean paraStart = false;
boolean needToAddDwetho = false;
boolean needToAddDoubleDaari = false;
Canvas mainCanvas;
Image mainImage;
// HBox imBox = new HBox();
ScrollBar scrollbar;//scrollbar for image scrolling
ImageView imView;//It will show the image of the page
TextArea textArea; // It will show the extracted Text
ArrayList<Integer> minbBlockRowList = new ArrayList<>();
Backgrounds bgs = new Backgrounds();
Borders borders = new Borders();
@Override
public void start(Stage primaryStage) throws SQLException, ClassNotFoundException {
scene = new Scene(root, prefSceneWidth, prefSceneHeight);
//A Heading for the image
imageText = new Text("Image");
imageText.setFont(Font.font("Arial", FontWeight.NORMAL, 16));
//Initialize textArea at the outset, otherwise an ugly nullpointer exception
//is thrown.
textArea = new TextArea();
textArea.appendText("");
textArea.prefWidthProperty().bind((scene.widthProperty().divide(2)).add(100));
textArea.prefHeightProperty().bind(scene.heightProperty().subtract(100));
textArea.setFont(Font.font("Noto Sans Myanmar UI", FontWeight.NORMAL, 20));;
textArea.setWrapText(true);
// textArea.setEditable(false);
//put that halfImage in the viewport
imView = new ImageView();
Rectangle2D viewport = new Rectangle2D(0,0, 700, prefSceneHeight);
imView.setViewport(viewport);
//-----------scrolling imageView---------------------------------------
scrollbar = new ScrollBar();
scrollbar.setOrientation(Orientation.VERTICAL);
scrollbar.setMin(0);
if(imView.getImage() != null){
scrollbar.setMax(imView.getImage().getHeight());
System.out.println("imview height: "+ imView.getImage().getHeight());
} else scrollbar.setMax(0);
scrollbar.maxHeightProperty().bind(scene.heightProperty()
.subtract(80));
scrollbar.valueProperty().addListener(
(observable, oldvalue, newvalue) ->{
int i = newvalue.intValue();
imView.setViewport( new Rectangle2D(0, i, 700, prefSceneHeight));
});
HBox box = new HBox(10, imView, scrollbar);
box.setPadding(new Insets(20,0,0,20));
box.setAlignment(Pos.TOP_CENTER);
//-------------imageView ends-------------------------------------------
//get a toolbar
toolbarLeft = getToolBarLeft();
toolbarRight = getToolBarRight();
//get vbleft
VBox vbleft = new VBox(imageText, toolbarLeft, box);
vbleft.setBorder(borders.borderLightBlue);
vbleft.setAlignment(Pos.TOP_CENTER);
//A Heading for the textAREA
Text textAreaHeader = new Text("Captured Text");
textAreaHeader.setFont(Font.font("Arial", FontWeight.NORMAL, 16));
//get vbright
VBox vbright = new VBox(textAreaHeader, toolbarRight, textArea);
vbright.setBorder(borders.borderLightBlue);
vbright.setAlignment(Pos.TOP_CENTER);
root.getChildren().addAll(vbleft, vbright);
primaryStage.setTitle("OCR3");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public void loadImage() throws IOException, SQLException, ClassNotFoundException{
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Image file");
fileChooser.getExtensionFilters().add(
new ExtensionFilter("Image Files", "*.png", "*.jpg"));
File selectedFile = fileChooser.showOpenDialog(null);
if(selectedFile == null) return;
System.out.println(selectedFile.getName());
System.out.println(selectedFile.getAbsolutePath());
String fileName = "file://"+selectedFile.getAbsolutePath();
mainImage = new Image(fileName);
//Image ss = new Image("file:///home/gsb/NetBeansProjects/MyFaFaR3/src/myfafar3/images/zoomin.png");
//Load an image
//image = new Image(getClass().getResourceAsStream("images/mlines.png"));
mainCanvas = getCanvas(mainImage, false); //grid = false
//get half sized writable image of a canvas containing the above image
Image halfImage = getHalfSizedCanvasImage(mainCanvas);
imView.setImage(halfImage);
scrollbar.setMax(imView.getImage().getHeight());
}
// public void startTask(){
// //Create a runnable
// Runnable task = () -> runTask();
//
// }
// public
public Canvas getCanvas(Image image, boolean showGrid) throws SQLException, ClassNotFoundException{
if(image == null)return null;
iWidth = (int)image.getWidth();
iHeight = (int)image.getHeight();
//Create a canvas and draw the image on it
Canvas canvas = new Canvas(iWidth, iHeight);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.drawImage(image, 0, 0);
//read the pixels and get the lines and characters in the canvas
if(showGrid == true)readPixelsInfo(gc, image, canvas, showGrid);
return canvas;
}
public Image getHalfSizedCanvasImage(Canvas canvas){
//take a snapshot of the whole canvas and turn it to an image
//All your jobs of finding lines and characters should be done
//on the canvas before this lines!
if(canvas == null) return null;
WritableImage canvasImage = canvas.snapshot(null, null);
//Put the canvasImage to an imageView of half Width, preserving
//aspect ratio.
ImageView tempImView = new ImageView(canvasImage);
tempImView.setFitWidth(700);
tempImView.setPreserveRatio(true);
//Take a snapshot of the halfSized imageView and return it
//for further processing.
WritableImage halfImageOfImageView = tempImView.snapshot(null, null);
return halfImageOfImageView;
}
public ArrayList<Integer[]> getCharacters(int imageW, int lineTop, int lineBottom,
Canvas canvas, PixelReader pixelReader){
// System.out.println("\n//----------getCharacters() method starting ...-----");
//int lineHeight = lineBottomNow - lineTopNow;
boolean blackColumnOn = false;
int blackStart = 0;
int blackEnd = 0;
//paraStart = false;
ArrayList<Integer[]> charactersXY = new ArrayList<>();
// System.out.println("lineTopNow: "+lineTopNow+", lineHeight: "+lineHeight);
//Read all pixels column by column
for(int x=0; x<imageW; x++){
//System.out.println("x = "+x);
for(int y=lineTop; y<lineBottom; y++){
// System.out.println(" y = "+y);
Color color = pixelReader.getColor(x, y);
if(color.equals(black)){
//System.out.println("pixel is black at x = "+x);
if(!blackColumnOn) {
blackColumnOn = true;
// System.out.println("character starts at x = "+x);
blackStart = x;
//Mark paragraph start
if(blackStart<100) paraStart = true;
break;
}
else if(x == imageW-1){
blackEnd = x;
break;
}
else break; //continue to next column
}
else{
// System.out.println("pixel is not black at x = "+x);
// System.out.println("blackColumnOn = "+blackColumnOn);
//
if(blackColumnOn && y == lineBottom-1){
blackColumnOn = false;
// System.out.println("character ends at x = "+x);
blackEnd = x-1;
Integer[] s = {blackStart, blackEnd};
charactersXY.add(s);
}
}
}//one column of pixels finished!
}
System.out.println("Found "+charactersXY.size()+" characters");
for(int i=0; i<charactersXY.size(); i++){
System.out.println((i+1)+". blackStart: "+charactersXY.get(i)[0]+", blackEnd: "
+charactersXY.get(i)[1]+", width: "
+(charactersXY.get(i)[1] - charactersXY.get(i)[0]));
}
return charactersXY;
}
public void readPixelsInfo(GraphicsContext gc, Image image,
Canvas canvas, boolean showGrid) throws SQLException, ClassNotFoundException{
minbBlockRowList.clear();
//Obtain pixelReader from image
PixelReader pixelReader = image.getPixelReader();
if(pixelReader == null){
System.out.println("Error reading pixels from image. Exiting.");
return;
}
//get the number of lines
ArrayList<Integer[]> myLines = getLines(canvas, image, gc, pixelReader);
System.out.println("rows of minimum black blocks:");
for(int in = 0; in<minbBlockRowList.size(); in++){
System.out.println(in+". y = "+minbBlockRowList.get(in));
}
//for each line, get the characters--------------------------------------
for(int c=0; c<myLines.size(); c++){
//Enable or disable the following statement to test a particular line
//if (c>0) break;
paraStart = false;
ArrayList<Integer[]> chars
= getCharacters(iWidth, minbBlockRowList.get(c),
minbBlockRowList.get(c+1), canvas, pixelReader);
// Add newLine in the beginning of new paragraph
if(paraStart)textArea.appendText("\n");
for(int i=0; i< chars.size(); i++){
System.out.println("Line: "+c+", Character: "+i);
//Add space between characters if found
if(i>0){
int whiteSpaceWidth = chars.get(i)[0]-chars.get(i-1)[1];
if(whiteSpaceWidth > 20) textArea.appendText(" ");
}
//Now identify characters
String abortHint = identifyCharacters(chars.get(i)[0], chars.get(i)[1],
minbBlockRowList.get(c), minbBlockRowList.get(c+1),
pixelReader, gc, canvas, textArea);
if(abortHint.equals("abort")) return;
System.out.println("------------------------------");
}
textArea.appendText(" ");
}
}
/*
public void readPixelsInfo(GraphicsContext gc, Image image,
Canvas canvas, boolean showGrid) throws SQLException, ClassNotFoundException{
minbBlockRowList.clear();
//Obtain pixelReader from image
PixelReader pixelReader = image.getPixelReader();
if(pixelReader == null){
System.out.println("Error reading pixels from image. Exiting.");
return;
}
//get the number of lines
ArrayList<Integer[]> myLines = getLines(canvas, image, gc, pixelReader);
System.out.println("rows of minimum black blocks:");
for(int in = 0; in<minbBlockRowList.size(); in++){
System.out.println(in+". y = "+minbBlockRowList.get(in));
}
//for each line, get the characters--------------------------------------
for(int c=0; c<myLines.size(); c++){
//Enable or disable the following statement to test a particular line
//if (c>0) break;
paraStart = false;
ArrayList<Integer[]> chars
= getCharacters(iWidth, minbBlockRowList.get(c),
minbBlockRowList.get(c+1), canvas, pixelReader);
//Add newLine in the beginning of new paragraph
if(paraStart)textArea.appendText("\n");
for(int i=0; i< chars.size(); i++){
System.out.println("Line: "+c+", Character: "+i);
//Add space between characters if found
if(i>0){
int whiteSpaceWidth = chars.get(i)[0]-chars.get(i-1)[1];
if(whiteSpaceWidth > 20) textArea.appendText(" ");
}
//Now identify characters
String abortHint = identifyCharacters(chars.get(i)[0], chars.get(i)[1],
minbBlockRowList.get(c), minbBlockRowList.get(c+1),
pixelReader, gc, canvas, textArea);
if(abortHint.equals("abort")) return;
System.out.println("------------------------------");
}
textArea.appendText(" ");
}
}
*/
public String identifyCharacters(int bMinX, int bMaxX, int bMinY, int bMaxY,
PixelReader pixelReader, GraphicsContext gc,
Canvas canvas, TextArea textArea) throws SQLException, ClassNotFoundException{
// System.out.println("\n//----------identifyCharacters() method starting ...-----");
// System.out.println("bMin: "+bMin);
blackMinY = 10000;
blackMaxY = 0;
boolean wBlockOn = false;
int wBlockCount = 0;
// to store y value of the beginning and end of white blocks
int wBlockStart = 0;
int wBlockEnd = 0;
ArrayList<Integer> wBlockStartList = new ArrayList<>();
ArrayList<Integer> wBlockEndList = new ArrayList<>();
//Read all pixels row by row
for(int y=bMinY; y<bMaxY; y++){
for(int x=bMinX; x<=bMaxX; x++){
Color color = pixelReader.getColor(x, y);
if(color.equals(black)){
//To eliminate unnecessary black spots on the top and bottom side
//of the line, we need to configure blackMiny and blackMaxY so that
// if there are more than 15 rows without any black block, then
//we can safely assume that it is safe to exclude that unnecessary part.
if(wBlockOn) {
// System.out.println("WBlock ended");
wBlockOn = false;
// System.out.println("wBlockCount: "+wBlockCount);
if(wBlockCount > 20) {
wBlockStartList.add(wBlockStart);
wBlockEndList.add(y);
if(y<(bMinY+(bMaxY-bMinY)/2))blackMinY = y;
wBlockCount = 0;
}
break;
}
//if(x < blackMinX) blackMinX = x;
//if(x > blackMaxX) blackMaxX = x;
if(y < blackMinY) blackMinY = y;
if(y > blackMaxY) {
blackMaxY = y;
// System.out.println("blackMaxY set at y="+blackMaxY);
}
break;
}
else if(x == bMaxX){
if(!wBlockOn ){
wBlockOn = true;
wBlockStart = y;
wBlockCount++;
}
else wBlockCount++;
}
}//one row of pixels finished!
}
//setting blackMaxY
if(wBlockStartList.size()>0){
wBlockStartList.forEach(a -> {
if(a>= (bMinY+(bMaxY-bMinY)/2) && a <= bMaxY){
blackMaxY = a;
System.out.println("blackMaxY set at y="+a);
}
});
}
blackMinX = bMinX;
blackMaxX = bMaxX;
// System.out.println("blackMinX= "+blackMinX);
// System.out.println("blackMinY= "+blackMinY);
// System.out.println("blackMaxX= "+blackMaxX);
// System.out.println("blackMaxY= "+blackMaxY);
//
int cWidth = blackMaxX - blackMinX;
int cHeight = blackMaxY - blackMinY;
//System output the info of characer width and height
System.out.println("cWidth: "+cWidth+", cHeight: "+cHeight);
String aspectRatio = String.format("%.5g%n",(double)cWidth/cHeight);
System.out.println("Aspect Ratio: "+aspectRatio);
//Get a surrounding red rectangle
gc.setStroke(Color.RED);
gc.strokeRect(blackMinX-1, blackMinY-1,
blackMaxX - blackMinX+1, blackMaxY - blackMinY+1);
//update imageView after drawing red boundaries on each character
this.updateImView(canvas);
//take a snapshot of the character to be used in the dialog
//for adding new character to database if needed
WritableImage charSnapShot;
if(blackMinX-2 >= 0 && blackMinY-2 >= 0 &&
blackMaxX-blackMinX+2 >=0 && blackMaxY-blackMinY+2 >= 0){
SnapshotParameters params = new SnapshotParameters();
Rectangle2D viewport = new Rectangle2D(blackMinX-2,blackMinY-2,
blackMaxX-blackMinX+2, blackMaxY-blackMinY+2);
params.setViewport(viewport);
charSnapShot = canvas.snapshot(params, null);
}
else charSnapShot = null;
characterKey = "";
//get the number of black pixels in the blocks
getBlackBlocks(bMinX, bMaxX, bMaxY, pixelReader);
//
System.out.println("Image characterKey : "+characterKey);
//Verify character by comparing characterKey from database
String cc;
if(charSnapShot != null){
cc = cModel.verifyCharacter(bwidth, bheight,
charSnapShot, characterKey, textArea);
if("abort".equals(cc)) return cc;
}
else cc = "";
System.out.println("character: "+cc);
System.out.println("cModel dwetho: "+cModel.dwethoAtTheEnd);
System.out.println("needToAddDwetho now: "+needToAddDwetho);
//handle dwetho problem separately
this.handleDwethoProblem(cc);
//Resetting needToAddDwetho for the next character
needToAddDwetho = cModel.dwethoAtTheEnd;
needToAddDoubleDaari = cModel.daariAtTheEnd;
return "";
// System.out.println("//----------identifyCharacters() method ended ...-----\n");
}
/*
public ArrayList<Integer[]> getLines(Canvas canvas, Image image, GraphicsContext gc,
PixelReader pixelReader) throws SQLException, ClassNotFoundException{
//ArrayList black
boolean blackBlockOn = false;
// int blackStart = 0;
// int blackEnd = 0;
int blackBlockCount = 0;
boolean firstBlockOn = false;
// boolean smallLineOn = false;
// int smallLines = 0;
// int smallLineTop = 0;
// int smallLineBottom = 0;
// int firstBlockX = 0;
int lastBlockX = 0;
int lineGapCount = 0; // used to count lines from the bottom of a full line
// int minbBlockRow = 0;
// int minbBlock = 10000;
//
// int[] rowsWithbBlocks = new int[iHeight];
boolean lineOn = false;
int lineTopNow = 0; int lineBottomNow = 0;
int lineTopBefore = 0; int lineBottomBefore = 0;
ArrayList<Integer[]> lines = new ArrayList<>();
//Read all pixels row by row
//------------------------------------y loop starts---------------------
for(int y=0; y<iHeight; y++){
blackBlockCount = 0;
firstBlockOn =false;
// lastBlockOn =false;
// firstBlockX = 0;
lastBlockX = 0;
//------------------------------------x loop starts-----------------
for(int x=0; x<iWidth; x++){
Color color = pixelReader.getColor(x, y);
if(color.equals(black)){
if(!blackBlockOn) { // after a white block
blackBlockOn = true;
blackBlockCount++;
if(!firstBlockOn){
firstBlockOn = true;
// firstBlockX = x;
lastBlockX = x;
}
//if first block on
else{
if(x>lastBlockX) lastBlockX = x;
}
// System.out.println("bakck Column starts at x = "+x);
//blackStart = x;
}
else if(x == iWidth-1){
//end of black column at the endX
// blackEnd = x;
blackBlockCount++;
blackBlockOn = false;
}
}
else{ //if pixel is white
if(blackBlockOn){
// System.out.println("blackBlock ends at "+x);
// blackEnd = x;
blackBlockOn = false; // end of black block
//blackBlockCount++;
// System.out.println("blackBlock: "+blackBlockCount);
}
}
}//------------------------------------x loop ends-----------------
//processing
if(blackBlockCount>=20){
// System.out.println(" Entered into blackBlockCount >= 20 at y="+y);
if(!lineOn) {
lineOn = true;
lineTopNow = y;
lineGapCount = 0;
}
}
else{
// System.out.println(" Not blackBlockCount >= 20 at y="+y);
if(lineOn){
lineOn = false;
lineBottomNow = y;
// System.out.println(" line on.");
// System.out.println("line height: "+(lineBottomNow-lineTopNow));
if(lineBottomNow-lineTopNow >= 15){
if(lineTopNow-lineBottomBefore > 150){
System.out.println("small line found after line"+(lines.size()-1));
System.out.println("smallLines Top will be: "+(lineBottomBefore+60));
System.out.println("smallLines Bottom will be: "+(lineBottomBefore+100));
Integer[] l = {(lineBottomBefore+60), lineBottomBefore+100};
lines.add(l);
}
Integer[] l = {lineTopNow, lineBottomNow};
lines.add(l);
//After adding store them for the next line
lineTopBefore = lineTopNow;
lineBottomBefore = lineBottomNow;
lineTopNow = 0;
lineBottomNow = 0;
// System.out.println(" line added.");
lineGapCount++;
}
}
}
}//------------------------------------y loop ends-----------------
//Let's try to find undetected smaller lines
//create a working copy of lines and add found lines there in that copy.
ArrayList<Integer[]> lines2 = lines;
for(int i=0; i<lines.size(); i++){
System.out.println("line "+i+". Top: "+lines.get(i)[0]
+", Bottom: "+lines.get(i)[1]);
if(i == lines.size()-1){
int lineGap = iHeight - lines.get(i)[1];
if(lineGap>150){
System.out.println("line found!");
Integer[] l = {(lines.get(i)[1]+60), (lines.get(i)[1]+100)};
lines.add(l);
}
}
//Get a surrounding blue rectangle for minimum lineHeight
gc.setStroke(Color.BLUE);
gc.strokeRect(0, lines.get(i)[0],
iWidth+1, lines.get(i)[1] - lines.get(i)[0]+1);
//blueR.setFill(null);
//root.getChildren().add(blueR);
}
//----------------Smaller lines detection ------------------------------
System.out.println("----------------Detecting smaller lines--------------------------");
//Detect smaller lines
// for(int i=0; i<lines.size();i++){
// if((i<lines.size()-1) && (lines.get(i+1)[0]-lines.get(i)[1])>50)
// }
//-----------------------------Smaller line detection ends -------------
//
//update imageView with recently deteted lines
// mainImage = image;
this.updateImView(canvas);
return lines;
}
*/
public ArrayList<Integer[]> getLines(Canvas canvas, Image image, GraphicsContext gc,
PixelReader pixelReader) throws SQLException, ClassNotFoundException{
//ArrayList black
boolean blackBlockOn = false;
int blackStart = 0;
int blackEnd = 0;
int blackBlockCount = 0;
boolean firstBlockOn = false;
boolean smallLineOn = false;
int smallLines = 0;
int smallLineTop = 0;
int smallLineBottom = 0;
int firstBlockX = 0;
int lastBlockX = 0;
int lineGapCount = 0; // used to count lines from the bottom of a full line
int minbBlockRow = 0;
int minbBlock = 10000;
int[] rowsWithbBlocks = new int[iHeight];
boolean lineOn = false;
int lineTopNow = 0; int lineBottomNow = 0;
ArrayList<Integer[]> lines = new ArrayList<>();
//Read all pixels row by row
for(int y=0; y<iHeight; y++){
blackBlockCount = 0;
firstBlockOn =false;
// lastBlockOn =false;
firstBlockX = 0;
lastBlockX = 0;
for(int x=0; x<iWidth; x++){
Color color = pixelReader.getColor(x, y);
if(color.equals(black)){
if(!blackBlockOn) { // after a white block
blackBlockOn = true;
blackBlockCount++;
if(!firstBlockOn){
firstBlockOn = true;
firstBlockX = x;
lastBlockX = x;
}
//if first block on
else{
if(x>lastBlockX) lastBlockX = x;
}
// System.out.println("bakck Column starts at x = "+x);
//blackStart = x;
}
else if(x == iWidth-1){
//end of black column at the endX
blackEnd = x;
blackBlockCount++;
blackBlockOn = false;
}
}
else{ //if pixel is white
if(blackBlockOn){
// System.out.println("blackBlock ends at "+x);
blackEnd = x;
blackBlockOn = false; // end of black block
//blackBlockCount++;
// System.out.println("blackBlock: "+blackBlockCount);
}
}
}//for x ends; one row of pixels finished!
rowsWithbBlocks[y] = blackBlockCount;
// if(blackBlockCount<15 && blackBlockCount < minbBlock){
// minbBlock = blackBlockCount;
// minbBlockRow = y;
// }
if(blackBlockCount>=20){
// System.out.println(" Entered into blackBlockCount >= 20 at y="+y);
if(!lineOn) {
lineOn = true;
lineTopNow = y;
lineGapCount = 0;
}
}
else{
// System.out.println(" Not blackBlockCount >= 20 at y="+y);
if(lineOn){
lineOn = false;
lineBottomNow = y;
// System.out.println(" line on.");
// System.out.println("line height: "+(lineBottomNow-lineTopNow));
if(lineBottomNow-lineTopNow >= 15){
Integer[] l = {lineTopNow, lineBottomNow};
lines.add(l);
// System.out.println(" line added.");
lineGapCount++;
// System.out.println("minbBlock: "+minbBlock+" in line: y="+minbBlockRow);
if(minbBlockRow == 0) minbBlockRow = smallLineBottom+30;
minbBlockRowList.add(minbBlockRow);
minbBlock = 10000;
minbBlockRow = 0;
}
}
else{
// System.out.println(" line not on.");
}
}
if(blackBlockCount<20){
// System.out.println("Entering blackBlockCount < 20 at y = "+y);
lineGapCount++;
// System.out.println("lineGap: "+lineGapCount+"blackBlockCount: "+blackBlockCount+", "
// + "minbBlock: "+minbBlock+", minbBlockRow: "+minbBlockRow);
if((lineGapCount<= 60 && blackBlockCount<10 && blackBlockCount < minbBlock)){
// System.out.println("minbBlock will be set.");
minbBlock = blackBlockCount;
minbBlockRow = y;
}
if(lineGapCount>100 && blackBlockCount<10 &&
blackBlockCount < minbBlock && y<smallLineTop) {
minbBlock = blackBlockCount;
minbBlockRow = y;
}
//put a for loop to detect short left-aligned sentence.
// System.out.println("y = "+y+", "+"lastBlock: "+lastBlockX+", "
// + "firstBlock: "+firstBlockX+", width = "+(lastBlockX - firstBlockX));
if(lineGapCount > 60 && (lastBlockX - firstBlockX)>20 &&
(lastBlockX - firstBlockX)< 400 && firstBlockX<300 && lastBlockX<700){
// System.out.println("fits as small line.");
// System.out.println(" fit as small line");
// System.out.println(" lineGap: "+lineGapCount);
// System.out.println(" y = "+y+", "+"lastBlock: "+lastBlockX+", "
// + "firstBlock: "+firstBlockX+", width = "+(lastBlockX - firstBlockX));
if(smallLineOn){
smallLines++;
// System.out.println(" small line counting.");
}
else{
smallLineOn = true;
smallLineTop = y;
smallLines++;
// System.out.println(" small line starting new...");
}
}
else {
// System.out.println(" not fit as small line. small line: "+smallLines);
// System.out.println(" lineGap: "+lineGapCount);
// System.out.println(" y = "+y+", "+"lastBlock: "+lastBlockX+", "
// + "firstBlock: "+firstBlockX+", width = "+(lastBlockX - firstBlockX));
if(smallLineOn){
smallLineOn = false;
smallLineBottom = y;
if(lineGapCount>100) {
smallLineBottom = smallLineTop + 40;
}
// System.out.println(" Small line number: "+smallLines);
if(smallLines >= 50){
Integer[] l = {smallLineTop, smallLineBottom};
lines.add(l);
// System.out.println("small line added.");
// System.out.println("minbBlock: "+minbBlock+" in line: y="+minbBlockRow);
if(minbBlockRow == 0) minbBlockRow = smallLineBottom+30;
minbBlockRowList.add(minbBlockRow);
minbBlock = 10000;
minbBlockRow = 0;