Skip to content

Commit 73a09c1

Browse files
committed
feat: add rotation attribute to DetectImageOptions
1 parent b047421 commit 73a09c1

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ Tries to detect text from the given image
100100

101101
#### DetectImageOptions
102102

103-
| Prop | Type | Description |
104-
| ----------------- | ------------------- | ------------------------------ |
105-
| **`base64Image`** | <code>string</code> | The image to detect texts from |
103+
| Prop | Type | Description |
104+
| ----------------- | ------------------- | ------------------------------------------------------------------------------------------------ |
105+
| **`base64Image`** | <code>string</code> | The image to detect texts from |
106+
| **`rotation`** | <code>number</code> | The image's counter-clockwise orientation degrees. Only 0, 90, 180, 270 are supported. Default 0 |
106107

107108
</docgen-api>

android/src/main/java/com/pantrist/ml/CapacitorPluginMlKitTextRecognition.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CapacitorPluginMlKitTextRecognition : Plugin() {
2222
call.reject("No image is given!")
2323
return
2424
}
25+
val rotation = call.getInt("rotation") ?: 0
2526

2627
val image: InputImage
2728
try {
@@ -31,7 +32,7 @@ class CapacitorPluginMlKitTextRecognition : Plugin() {
3132
call.reject("Decoded image is null")
3233
return
3334
}
34-
image = InputImage.fromBitmap(decodedByte, 0);
35+
image = InputImage.fromBitmap(decodedByte, rotation)
3536
} catch (e: IOException) {
3637
call.reject("Unable to parse image")
3738
return

ios/Plugin/CapacitorPluginMlKitTextRecognitionPlugin.swift

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CapacitorPluginMlKitTextRecognitionPlugin: CAPPlugin {
1313
call.reject("No image is given!")
1414
return
1515
}
16+
let rotation = call.getInt("rotation") ?? 0
1617

1718
let dataDecoded : Data = Data(base64Encoded: encodedImage, options: .ignoreUnknownCharacters)!
1819
guard let image = UIImage(data: dataDecoded) else {
@@ -23,6 +24,7 @@ public class CapacitorPluginMlKitTextRecognitionPlugin: CAPPlugin {
2324
let latinOptions = TextRecognizerOptions()
2425
let textRecognizer = TextRecognizer.textRecognizer(options: latinOptions)
2526
let visionImage = VisionImage(image: image)
27+
visionImage.orientation = visionImageOrientation(rotation: rotation)
2628

2729
textRecognizer.process(visionImage) { result, error in
2830
guard error == nil, let result = result else {
@@ -81,4 +83,19 @@ public class CapacitorPluginMlKitTextRecognitionPlugin: CAPPlugin {
8183
])
8284
}
8385
}
86+
87+
public func visionImageOrientation(rotation: Int) -> UIImage.Orientation {
88+
switch rotation {
89+
case 0:
90+
return .up
91+
case 90:
92+
return .left
93+
case 180:
94+
return .down
95+
case 270:
96+
return .right
97+
default:
98+
return .up
99+
}
100+
}
84101
}

src/definitions.ts

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export interface DetectImageOptions {
1212
* The image to detect texts from
1313
*/
1414
base64Image: string;
15+
/**
16+
* The image's counter-clockwise orientation degrees. Only 0, 90, 180, 270 are supported. Default 0
17+
*/
18+
rotation?: number;
1519
}
1620

1721
export interface TextDetectionResult {

0 commit comments

Comments
 (0)