OCR in Flutter

0

Optical character recognition is a process of conversion of typed images, printed text into the machine-encoded text, which means it will give us a text from images that contains the text.

This comes very handy with google mobile vision api, we just need to follow below steps

Step 1: Add dependency in pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
    flutter_mobile_vision: ^0.1.3

Step 2: Add Permissions

For Android, you must do the following before you can use the plugin:

Add the camera permission to your AndroidManifest.xml

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />

Add the OCR activity to your AndroidManifest.xml (after other activity nodes)

<activity android:name="io.github.edufolly.fluttermobilevision.ocr.OcrCaptureActivity" />

Note: You have to go manually at your application permission settings and then turn on camera permission, if you are doing this then only you can open camera from your application.

Step 3 : Write a code to open camera and take capture text.

List<OcrText> texts = [];
int _cameraOcr = FlutterMobileVision.CAMERA_BACK;
Size _previewOcr;

  @override
  void initState() {
    super.initState();
    FlutterMobileVision.start().then((previewSizes) => setState(() {
      _previewOcr = previewSizes[_cameraOcr].first;
    }));
  }


Future<Null> _read() async {
  try {
    texts = await FlutterMobileVision.read(
      camera: _cameraOcr,
      waitTap: true,
    );

    setState(() {
      _textValue = texts[0].value; // Getting first text block....
    });
  } on Exception {
    texts.add(new OcrText('Failed to recognize text.'));
  }
}

Screenshots:

Leave a Reply

Your email address will not be published. Required fields are marked *