Skip to content

Commit e9d9c1f

Browse files
committed
Recognize Text
1 parent 3e8f5a0 commit e9d9c1f

12 files changed

+131
-102
lines changed

README.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Liked? :star: Star the repo to support the project!
1313
## Features
1414

1515
* [ ] Android
16-
* [ ] Barcode Scan
16+
* [x] Barcode Scan
1717
* [x] Simple scan.
1818
* [x] Toggle torch.
1919
* [x] Toggle auto focus.
@@ -22,11 +22,13 @@ Liked? :star: Star the repo to support the project!
2222
* [x] Select barcode type to be scanned.
2323
* [x] Scan multiple barcodes.
2424
* [x] Barcode coordinates.
25-
* [ ] Recognize Text
25+
* [x] Show barcode text.
26+
* [x] Recognize Text
2627
* [x] Simple OCR.
2728
* [x] Multiple recognition.
2829
* [x] Text language.
2930
* [x] Text coordinates.
31+
* [x] Hide recognized text.
3032
* [ ] Detect Faces
3133
* [ ] _Future Tasks_
3234

@@ -53,7 +55,7 @@ To use this plugin :
5355
dependencies:
5456
flutter:
5557
sdk: flutter
56-
flutter_mobile_vision: ^0.0.2
58+
flutter_mobile_vision: ^0.0.3
5759
```
5860
5961
-----
@@ -65,11 +67,12 @@ To use this plugin :
6567
List<Barcode> barcodes = [];
6668
try {
6769
barcodes = await FlutterMobileVision.scan(
68-
flash: _torch,
69-
autoFocus: _autoFocus,
70-
formats: _onlyFormat,
71-
multiple: _multiple,
72-
waitTap: _waitTap,
70+
flash: _torchBarcode,
71+
autoFocus: _autoFocusBarcode,
72+
formats: _onlyFormatBarcode,
73+
multiple: _multipleBarcode,
74+
waitTap: _waitTapBarcode,
75+
showText: _showTextBarcode,
7376
);
7477
} on Exception {
7578
barcodes.add(new Barcode('Failed to get barcode.'));
@@ -107,9 +110,10 @@ try {
107110
flash: _torchOcr,
108111
autoFocus: _autoFocusOcr,
109112
multiple: _multipleOcr,
113+
showText: _showTextOcr,
110114
);
111115
} on Exception {
112-
texts.add(new OcrText('Failed to get barcode.'));
116+
texts.add(new OcrText('Failed to recognize text.'));
113117
}
114118
//...
115119
```

android/src/main/java/io/github/edufolly/fluttermobilevision/FlutterMobileVisionPlugin.java

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class FlutterMobileVisionPlugin implements MethodCallHandler,
4343
private int formats = Barcode.ALL_FORMATS;
4444
private boolean multiple = false;
4545
private boolean waitTap = false;
46+
private boolean showText = false;
4647

4748
/**
4849
* Plugin registration.
@@ -94,6 +95,10 @@ public void onMethodCall(MethodCall call, Result result) {
9495
waitTap = true;
9596
}
9697

98+
if (arguments.containsKey("showText")) {
99+
showText = (boolean) arguments.get("showText");
100+
}
101+
97102
int rc = ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
98103
if (rc != PackageManager.PERMISSION_GRANTED) {
99104
ActivityCompat.requestPermissions(activity, new
@@ -116,6 +121,10 @@ public void onMethodCall(MethodCall call, Result result) {
116121
multiple = (boolean) arguments.get("multiple");
117122
}
118123

124+
if (arguments.containsKey("showText")) {
125+
showText = (boolean) arguments.get("showText");
126+
}
127+
119128
int rc = ActivityCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
120129
if (rc != PackageManager.PERMISSION_GRANTED) {
121130
ActivityCompat.requestPermissions(activity, new
@@ -136,6 +145,7 @@ private void scanBarcode() {
136145
intent.putExtra(BarcodeCaptureActivity.FORMATS, formats);
137146
intent.putExtra(BarcodeCaptureActivity.MULTIPLE, multiple);
138147
intent.putExtra(BarcodeCaptureActivity.WAIT_TAP, waitTap);
148+
intent.putExtra(BarcodeCaptureActivity.SHOW_TEXT, showText);
139149
activity.startActivityForResult(intent, RC_BARCODE_SCAN);
140150
}
141151

@@ -144,6 +154,7 @@ private void ocrRead() {
144154
intent.putExtra(OcrCaptureActivity.AUTO_FOCUS, autoFocus);
145155
intent.putExtra(OcrCaptureActivity.USE_FLASH, useFlash);
146156
intent.putExtra(OcrCaptureActivity.MULTIPLE, multiple);
157+
intent.putExtra(OcrCaptureActivity.SHOW_TEXT, showText);
147158
activity.startActivityForResult(intent, RC_OCR_READ);
148159
}
149160

android/src/main/java/io/github/edufolly/fluttermobilevision/barcode/BarcodeCaptureActivity.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class BarcodeCaptureActivity extends Activity
5555
public static final String FORMATS = "FORMATS";
5656
public static final String MULTIPLE = "MULTIPLE";
5757
public static final String WAIT_TAP = "WAIT_TAP";
58+
public static final String SHOW_TEXT = "SHOW_TEXT";
5859

5960
public static final String BARCODE_OBJECT = "Barcode";
6061
public static final String ERROR = "Error";
@@ -65,8 +66,11 @@ public final class BarcodeCaptureActivity extends Activity
6566

6667
private GestureDetector gestureDetector;
6768

69+
private boolean autoFocus;
70+
private boolean useFlash;
6871
private boolean multiple;
6972
private boolean waitTap;
73+
private boolean showText;
7074

7175
@Override
7276
public void onCreate(Bundle icicle) {
@@ -82,15 +86,15 @@ public void onCreate(Bundle icicle) {
8286
mPreview = findViewById(R.id.preview);
8387
mGraphicOverlay = findViewById(R.id.graphic_overlay);
8488

85-
boolean autoFocus = getIntent().getBooleanExtra(AUTO_FOCUS, false);
86-
boolean useFlash = getIntent().getBooleanExtra(USE_FLASH, false);
87-
89+
autoFocus = getIntent().getBooleanExtra(AUTO_FOCUS, false);
90+
useFlash = getIntent().getBooleanExtra(USE_FLASH, false);
8891
multiple = getIntent().getBooleanExtra(MULTIPLE, false);
8992
waitTap = getIntent().getBooleanExtra(WAIT_TAP, false);
93+
showText = getIntent().getBooleanExtra(SHOW_TEXT, false);
9094

9195
int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
9296
if (rc == PackageManager.PERMISSION_GRANTED) {
93-
createCameraSource(autoFocus, useFlash);
97+
createCameraSource();
9498
} else {
9599
throw new MobileVisionException("Camera permission is needed.");
96100
}
@@ -107,21 +111,22 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
107111
}
108112

109113
@SuppressLint("InlinedApi")
110-
private void createCameraSource(boolean autoFocus, boolean useFlash) throws MobileVisionException {
114+
private void createCameraSource() throws MobileVisionException {
111115
Context context = getApplicationContext();
112116

113117
BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(context)
114118
.setBarcodeFormats(getIntent().getIntExtra(FORMATS, Barcode.ALL_FORMATS))
115119
.build();
116120

117-
BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay, this);
121+
BarcodeTrackerFactory barcodeFactory = new BarcodeTrackerFactory(mGraphicOverlay,
122+
this, showText);
118123

119124
barcodeDetector.setProcessor(
120125
new MultiProcessor.Builder<>(barcodeFactory).build());
121126

122127
if (!barcodeDetector.isOperational()) {
123-
IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
124-
boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;
128+
IntentFilter lowStorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
129+
boolean hasLowStorage = registerReceiver(null, lowStorageFilter) != null;
125130

126131
if (hasLowStorage) {
127132
throw new MobileVisionException("Low Storage.");

android/src/main/java/io/github/edufolly/fluttermobilevision/barcode/BarcodeGraphic.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ public class BarcodeGraphic extends GraphicOverlay.Graphic {
3737
private static int mCurrentColorIndex;
3838
private int mId;
3939
private Paint mRectPaint;
40-
// private Paint mTextPaint;
40+
private Paint mTextPaint;
4141
private volatile Barcode mBarcode;
4242

4343
static {
4444
mCurrentColorIndex = 0;
4545
}
4646

47-
BarcodeGraphic(GraphicOverlay overlay) {
47+
BarcodeGraphic(GraphicOverlay overlay, boolean showText) {
4848
super(overlay);
4949

50+
5051
mCurrentColorIndex = (mCurrentColorIndex + 1) % COLOR_CHOICES.length;
5152
final int selectedColor = COLOR_CHOICES[mCurrentColorIndex];
5253

@@ -55,9 +56,11 @@ public class BarcodeGraphic extends GraphicOverlay.Graphic {
5556
mRectPaint.setStyle(Paint.Style.STROKE);
5657
mRectPaint.setStrokeWidth(4.0f);
5758

58-
// mTextPaint = new Paint();
59-
// mTextPaint.setColor(selectedColor);
60-
// mTextPaint.setTextSize(36.0f);
59+
if (showText) {
60+
mTextPaint = new Paint();
61+
mTextPaint.setColor(selectedColor);
62+
mTextPaint.setTextSize(36.0f);
63+
}
6164
}
6265

6366
public int getId() {
@@ -98,7 +101,7 @@ public boolean contains(float x, float y) {
98101
* Updates the barcode instance from the detection of the most recent frame. Invalidates the
99102
* relevant portions of the overlay to trigger a redraw.
100103
*/
101-
void updateItem(Barcode barcode) {
104+
public void updateItem(Barcode barcode) {
102105
mBarcode = barcode;
103106
postInvalidate();
104107
}
@@ -121,7 +124,9 @@ public void draw(Canvas canvas) {
121124
rect.bottom = translateY(rect.bottom);
122125
canvas.drawRect(rect, mRectPaint);
123126

124-
// // Draws a label at the bottom of the barcode indicate the barcode value that was detected.
125-
// canvas.drawText(barcode.rawValue, rect.left, rect.bottom, mTextPaint);
127+
if (mTextPaint != null) {
128+
// Draws a label at the bottom of the barcode indicate the barcode value that was detected.
129+
canvas.drawText(barcode.rawValue, rect.left, rect.bottom, mTextPaint);
130+
}
126131
}
127132
}

android/src/main/java/io/github/edufolly/fluttermobilevision/barcode/BarcodeTrackerFactory.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,19 @@
3535
public class BarcodeTrackerFactory implements MultiProcessor.Factory<Barcode> {
3636
private GraphicOverlay<BarcodeGraphic> mGraphicOverlay;
3737
private Context mContext;
38+
private boolean showText;
3839

3940
public BarcodeTrackerFactory(GraphicOverlay<BarcodeGraphic> mGraphicOverlay,
40-
Context mContext) {
41+
Context mContext, boolean showText) {
4142

4243
this.mGraphicOverlay = mGraphicOverlay;
4344
this.mContext = mContext;
45+
this.showText = showText;
4446
}
4547

4648
@Override
4749
public Tracker<Barcode> create(Barcode barcode) {
48-
BarcodeGraphic graphic = new BarcodeGraphic(mGraphicOverlay);
50+
BarcodeGraphic graphic = new BarcodeGraphic(mGraphicOverlay, showText);
4951
try {
5052
return new BarcodeGraphicTracker(mGraphicOverlay, graphic, mContext);
5153
} catch (Exception ex) {

android/src/main/java/io/github/edufolly/fluttermobilevision/ocr/OcrCaptureActivity.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public final class OcrCaptureActivity extends Activity {
5050
public static final String AUTO_FOCUS = "AUTO_FOCUS";
5151
public static final String USE_FLASH = "USE_FLASH";
5252
public static final String MULTIPLE = "MULTIPLE";
53+
public static final String SHOW_TEXT = "SHOW_TEXT";
5354

5455
public static final String TEXT_OBJECT = "Text";
5556
public static final String ERROR = "Error";
@@ -60,7 +61,10 @@ public final class OcrCaptureActivity extends Activity {
6061

6162
private GestureDetector gestureDetector;
6263

64+
private boolean useFlash;
65+
private boolean autoFocus;
6366
private boolean multiple;
67+
private boolean showText;
6468

6569
@Override
6670
public void onCreate(Bundle icicle) {
@@ -76,14 +80,14 @@ public void onCreate(Bundle icicle) {
7680
mPreview = findViewById(R.id.preview);
7781
mGraphicOverlay = findViewById(R.id.graphic_overlay);
7882

79-
boolean autoFocus = getIntent().getBooleanExtra(AUTO_FOCUS, false);
80-
boolean useFlash = getIntent().getBooleanExtra(USE_FLASH, false);
81-
83+
useFlash = getIntent().getBooleanExtra(USE_FLASH, false);
84+
autoFocus = getIntent().getBooleanExtra(AUTO_FOCUS, false);
8285
multiple = getIntent().getBooleanExtra(MULTIPLE, false);
86+
showText = getIntent().getBooleanExtra(SHOW_TEXT, false);
8387

8488
int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
8589
if (rc == PackageManager.PERMISSION_GRANTED) {
86-
createCameraSource(autoFocus, useFlash);
90+
createCameraSource();
8791
} else {
8892
throw new MobileVisionException("Camera permission is needed.");
8993
}
@@ -101,11 +105,11 @@ public boolean onSingleTapConfirmed(MotionEvent e) {
101105
}
102106

103107
@SuppressLint("InlinedApi")
104-
private void createCameraSource(boolean autoFocus, boolean useFlash) throws MobileVisionException {
108+
private void createCameraSource() throws MobileVisionException {
105109
Context context = getApplicationContext();
106110

107111
TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build();
108-
textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay));
112+
textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay, showText));
109113

110114
if (!textRecognizer.isOperational()) {
111115
IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);

android/src/main/java/io/github/edufolly/fluttermobilevision/ocr/OcrDetectorProcessor.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
*/
2929
public class OcrDetectorProcessor implements Detector.Processor<TextBlock> {
3030

31-
private GraphicOverlay<OcrGraphic> mGraphicOverlay;
31+
private GraphicOverlay<OcrGraphic> graphicOverlay;
32+
private boolean showText;
3233

33-
OcrDetectorProcessor(GraphicOverlay<OcrGraphic> ocrGraphicOverlay) {
34-
mGraphicOverlay = ocrGraphicOverlay;
34+
OcrDetectorProcessor(GraphicOverlay<OcrGraphic> graphicOverlay, boolean showText) {
35+
this.graphicOverlay = graphicOverlay;
36+
this.showText = showText;
3537
}
3638

3739
/**
@@ -43,12 +45,12 @@ public class OcrDetectorProcessor implements Detector.Processor<TextBlock> {
4345
*/
4446
@Override
4547
public void receiveDetections(Detector.Detections<TextBlock> detections) {
46-
mGraphicOverlay.clear();
48+
graphicOverlay.clear();
4749
SparseArray<TextBlock> items = detections.getDetectedItems();
4850
for (int i = 0; i < items.size(); ++i) {
4951
TextBlock item = items.valueAt(i);
50-
OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
51-
mGraphicOverlay.add(graphic);
52+
OcrGraphic graphic = new OcrGraphic(graphicOverlay, item, showText);
53+
graphicOverlay.add(graphic);
5254
}
5355
}
5456

@@ -57,6 +59,6 @@ public void receiveDetections(Detector.Detections<TextBlock> detections) {
5759
*/
5860
@Override
5961
public void release() {
60-
mGraphicOverlay.clear();
62+
graphicOverlay.clear();
6163
}
6264
}

0 commit comments

Comments
 (0)