This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code updated
- Loading branch information
lewix
committed
Feb 18, 2019
1 parent
0192387
commit c968b77
Showing
3 changed files
with
115 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:camera/camera.dart'; | ||
import 'package:firebase_ml_vision/firebase_ml_vision.dart'; | ||
import 'dart:typed_data'; | ||
import 'package:flutter/foundation.dart'; | ||
|
||
class OcrManager { | ||
|
||
static Future<bool> scanText(CameraImage availableImage) async { | ||
|
||
print("scanning!..."); | ||
|
||
/* | ||
* https://firebase.google.com/docs/ml-kit/android/recognize-text | ||
* .setWidth(480) // 480x360 is typically sufficient for | ||
* .setHeight(360) // image recognition | ||
*/ | ||
|
||
final FirebaseVisionImageMetadata metadata = FirebaseVisionImageMetadata( | ||
rawFormat: availableImage.format.raw, | ||
size: Size(availableImage.width.toDouble(),availableImage.height.toDouble()), | ||
planeData: availableImage.planes.map((currentPlane) => FirebaseVisionImagePlaneMetadata( | ||
bytesPerRow: currentPlane.bytesPerRow, | ||
height: currentPlane.height, | ||
width: currentPlane.width | ||
)).toList(), | ||
rotation: ImageRotation.rotation90 | ||
); | ||
|
||
final FirebaseVisionImage visionImage = FirebaseVisionImage.fromBytes(availableImage.planes[0].bytes, metadata); | ||
final TextRecognizer textRecognizer = FirebaseVision.instance.textRecognizer(); | ||
final VisionText visionText = await textRecognizer.processImage(visionImage); | ||
|
||
print("--------------------visionText:${visionText.text}"); | ||
for (TextBlock block in visionText.blocks) { | ||
// final Rectangle<int> boundingBox = block.boundingBox; | ||
// final List<Point<int>> cornerPoints = block.cornerPoints; | ||
print(block.text); | ||
//final List<RecognizedLanguage> languages = block.recognizedLanguages; | ||
|
||
for (TextLine line in block.lines) { | ||
// Same getters as TextBlock | ||
print(line.text); | ||
for (TextElement element in line.elements) { | ||
// Same getters as TextBlock | ||
print(element.text); | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/* | ||
* code by | ||
* https://github.com/bparrishMines/mlkit_demo/blob/master/lib/main.dart | ||
*/ | ||
|
||
Uint8List concatenatePlanes(List<Plane> planes) { | ||
final WriteBuffer allBytes = WriteBuffer(); | ||
planes.forEach((Plane plane) => allBytes.putUint8List(plane.bytes)); | ||
return allBytes.done().buffer.asUint8List(); | ||
} | ||
|
||
FirebaseVisionImageMetadata buildMetaData(CameraImage image) { | ||
return FirebaseVisionImageMetadata( | ||
rawFormat: image.format.raw, | ||
size: Size(image.width.toDouble(), image.height.toDouble()), | ||
rotation: ImageRotation.rotation270, | ||
planeData: image.planes.map((Plane plane) { | ||
return FirebaseVisionImagePlaneMetadata( | ||
bytesPerRow: plane.bytesPerRow, | ||
height: plane.height, | ||
width: plane.width, | ||
); | ||
}).toList(), | ||
); | ||
} | ||
} |