@@ -5,6 +5,7 @@ import com.getcapacitor.*
5
5
import com.getcapacitor.annotation.CapacitorPlugin
6
6
import java.io.IOException
7
7
import android.graphics.BitmapFactory
8
+ import android.graphics.Point
8
9
import android.graphics.Rect
9
10
10
11
import com.google.mlkit.vision.common.InputImage
@@ -13,83 +14,105 @@ import com.google.mlkit.vision.text.latin.TextRecognizerOptions
13
14
14
15
@CapacitorPlugin
15
16
class CapacitorPluginMlKitTextRecognition : Plugin () {
16
- @PluginMethod
17
- fun detectText (call : PluginCall ) {
18
- val recognizer = TextRecognition .getClient(TextRecognizerOptions .DEFAULT_OPTIONS )
17
+ @PluginMethod
18
+ fun detectText (call : PluginCall ) {
19
+ val recognizer = TextRecognition .getClient(TextRecognizerOptions .DEFAULT_OPTIONS )
19
20
20
- val encodedImage = call.getString(" base64Image" )
21
- if (encodedImage == null ) {
22
- call.reject(" No image is given!" )
23
- return
24
- }
25
- val rotation = call.getInt(" rotation" ) ? : 0
26
-
27
- val image: InputImage
28
- try {
29
- val decodedString: ByteArray = Base64 .decode(encodedImage, Base64 .DEFAULT );
30
- val decodedByte = BitmapFactory .decodeByteArray(decodedString, 0 , decodedString.size)
31
- if (decodedByte == null ) {
32
- call.reject(" Decoded image is null" )
33
- return
34
- }
35
- image = InputImage .fromBitmap(decodedByte, rotation)
36
- } catch (e: IOException ) {
37
- call.reject(" Unable to parse image" )
38
- return
39
- }
21
+ val encodedImage = call.getString(" base64Image" )
22
+ if (encodedImage == null ) {
23
+ call.reject(" No image is given!" )
24
+ return
25
+ }
26
+ val rotation = call.getInt(" rotation" ) ? : 0
40
27
41
- recognizer.process(image)
42
- .addOnSuccessListener { visionText ->
43
- val ret = JSObject ()
44
- ret.put(" text" , visionText.text)
28
+ val image: InputImage
29
+ try {
30
+ val decodedString: ByteArray = Base64 .decode(encodedImage, Base64 .DEFAULT );
31
+ val decodedByte = BitmapFactory .decodeByteArray(decodedString, 0 , decodedString.size)
32
+ if (decodedByte == null ) {
33
+ call.reject(" Decoded image is null" )
34
+ return
35
+ }
36
+ image = InputImage .fromBitmap(decodedByte, rotation)
37
+ } catch (e: IOException ) {
38
+ call.reject(" Unable to parse image" )
39
+ return
40
+ }
45
41
46
- val textBlocks = JSArray ()
47
- visionText.textBlocks.forEach { block ->
48
- val blockObject = JSObject ();
49
- blockObject.put(" text" , block.text)
50
- blockObject.put(" boundingBox" , parseRectToJsObject(block.boundingBox))
51
- blockObject.put(" recognizedLanguage" , block.recognizedLanguage)
42
+ recognizer.process(image)
43
+ .addOnSuccessListener { visionText ->
44
+ val ret = JSObject ()
45
+ ret.put(" text" , visionText.text)
52
46
53
- val linesArray = JSArray ()
54
- block.lines.forEach { line ->
55
- val lineObject = JSObject ()
56
- lineObject.put(" text" , line.text)
57
- lineObject.put(" boundingBox" , parseRectToJsObject(line.boundingBox))
58
- lineObject.put(" recognizedLanguage" , line.recognizedLanguage)
47
+ val textBlocks = JSArray ()
48
+ visionText.textBlocks.forEach { block ->
49
+ val blockObject = JSObject ();
50
+ blockObject.put(" text" , block.text)
51
+ blockObject.put(" boundingBox" , parseRectToJsObject(block.boundingBox))
52
+ blockObject.put(" recognizedLanguage" , block.recognizedLanguage)
53
+ blockObject.put(" cornerPoints" , parseCornerPointsToJsObject(block.cornerPoints))
59
54
60
- val elementArray = JSArray ()
61
- line.elements.forEach { element ->
62
- val elementObject = JSObject ()
63
- elementObject.put(" text" , element.text)
64
- elementObject.put(" boundingBox" , parseRectToJsObject(element.boundingBox))
65
- elementObject.put(" recognizedLanguage" , line.recognizedLanguage)
66
- elementArray.put(elementObject)
67
- }
68
- lineObject.put(" elements" , elementArray)
69
- linesArray.put(lineObject)
70
- }
71
- blockObject.put(" lines" , linesArray)
72
- textBlocks.put(blockObject)
73
- };
74
- ret.put(" blocks" , textBlocks)
55
+ val linesArray = JSArray ()
56
+ block.lines.forEach { line ->
57
+ val lineObject = JSObject ()
58
+ lineObject.put(" text" , line.text)
59
+ lineObject.put(" boundingBox" , parseRectToJsObject(line.boundingBox))
60
+ lineObject.put(" recognizedLanguage" , line.recognizedLanguage)
61
+ lineObject.put(" cornerPoints" , parseCornerPointsToJsObject(line.cornerPoints))
75
62
76
- call.resolve(ret)
77
- }
78
- .addOnFailureListener { e ->
79
- call.reject(" Unable process image!" , e)
63
+ val elementArray = JSArray ()
64
+ line.elements.forEach { element ->
65
+ val elementObject = JSObject ()
66
+ elementObject.put(" text" , element.text)
67
+ elementObject.put(" boundingBox" , parseRectToJsObject(element.boundingBox))
68
+ elementObject.put(" recognizedLanguage" , line.recognizedLanguage)
69
+ elementObject.put(" cornerPoints" , parseCornerPointsToJsObject(element.cornerPoints))
70
+ elementArray.put(elementObject)
80
71
}
72
+ lineObject.put(" elements" , elementArray)
73
+ linesArray.put(lineObject)
74
+ }
75
+ blockObject.put(" lines" , linesArray)
76
+ textBlocks.put(blockObject)
77
+ };
78
+ ret.put(" blocks" , textBlocks)
79
+
80
+ call.resolve(ret)
81
+ }
82
+ .addOnFailureListener { e ->
83
+ call.reject(" Unable process image!" , e)
84
+ }
85
+ }
86
+
87
+ private fun parseRectToJsObject (rect : Rect ? ): JSObject ? {
88
+ if (rect == null ) {
89
+ return null
81
90
}
82
91
83
- private fun parseRectToJsObject (rect : Rect ? ): JSObject ? {
84
- if (rect == null ) {
85
- return null
86
- }
92
+ val returnObject = JSObject ();
93
+ returnObject.put(" left" , rect.left)
94
+ returnObject.put(" top" , rect.top)
95
+ returnObject.put(" right" , rect.right)
96
+ returnObject.put(" bottom" , rect.bottom)
97
+ return returnObject;
98
+ }
87
99
88
- val returnObject = JSObject ();
89
- returnObject.put(" left" , rect.left)
90
- returnObject.put(" top" , rect.top)
91
- returnObject.put(" right" , rect.right)
92
- returnObject.put(" bottom" , rect.bottom)
93
- return returnObject;
100
+ private fun parseCornerPointsToJsObject (cornerPoints : Array <Point >? ): JSObject ? {
101
+ if (cornerPoints == null || cornerPoints.size != 4 ) {
102
+ return null ;
94
103
}
95
- }
104
+ val res = JSObject ();
105
+ res.put(" topLeft" , pointToJsObject(cornerPoints[0 ]));
106
+ res.put(" topRight" , pointToJsObject(cornerPoints[1 ]));
107
+ res.put(" bottomRight" , pointToJsObject(cornerPoints[2 ]));
108
+ res.put(" bottomLeft" , pointToJsObject(cornerPoints[3 ]));
109
+ return res;
110
+ }
111
+
112
+ private fun pointToJsObject (pt : Point ): JSObject {
113
+ val res = JSObject ();
114
+ res.put(" x" , pt.x.toDouble());
115
+ res.put(" y" , pt.y.toDouble());
116
+ return res;
117
+ }
118
+ }
0 commit comments