forked from aarongrider/vision-camera-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd88556
commit a6a2d35
Showing
4 changed files
with
113 additions
and
27 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
113 changes: 107 additions & 6 deletions
113
android/src/main/java/com/visioncameraocr/OCRFrameProcessorPlugin.kt
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 |
---|---|---|
@@ -1,23 +1,124 @@ | ||
package com.visioncameraocr | ||
|
||
import android.annotation.SuppressLint | ||
import android.graphics.Point | ||
import android.graphics.Rect | ||
import android.media.Image | ||
import androidx.camera.core.ImageProxy | ||
import com.facebook.react.bridge.WritableNativeArray | ||
import com.facebook.react.bridge.WritableNativeMap | ||
import com.google.android.gms.tasks.Task | ||
import com.google.android.gms.tasks.Tasks | ||
import com.google.mlkit.vision.common.InputImage | ||
import com.google.mlkit.vision.text.Text | ||
import com.google.mlkit.vision.text.TextRecognition | ||
import com.google.mlkit.vision.text.latin.TextRecognizerOptions | ||
import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin | ||
|
||
|
||
class OCRFrameProcessorPlugin: FrameProcessorPlugin("scanOCR") { | ||
|
||
override fun callback(image: ImageProxy, params: Array<Any>): Any? { | ||
private fun getBlockArray(blocks: MutableList<Text.TextBlock>): WritableNativeArray { | ||
val blockArray = WritableNativeArray() | ||
|
||
for (block in blocks) { | ||
val blockMap = WritableNativeMap() | ||
|
||
blockMap.putString("text", block.text) | ||
blockMap.putArray("recognizedLanguages", getRecognizedLanguages(block.recognizedLanguage)) | ||
blockMap.putArray("cornerPoints", block.cornerPoints?.let { getCornerPoints(it) }) | ||
blockMap.putMap("frame", getFrame(block.boundingBox)) | ||
blockMap.putArray("lines", getLineArray(block.lines)) | ||
|
||
blockArray.pushMap(blockMap) | ||
} | ||
return blockArray | ||
} | ||
|
||
private fun getLineArray(lines: MutableList<Text.Line>): WritableNativeArray { | ||
val lineArray = WritableNativeArray() | ||
|
||
for (line in lines) { | ||
val lineMap = WritableNativeMap() | ||
|
||
lineMap.putString("text", line.text) | ||
lineMap.putArray("recognizedLanguages", getRecognizedLanguages(line.recognizedLanguage)) | ||
lineMap.putArray("cornerPoints", line.cornerPoints?.let { getCornerPoints(it) }) | ||
lineMap.putMap("frame", getFrame(line.boundingBox)) | ||
lineMap.putArray("elements", getElementArray(line.elements)) | ||
|
||
lineArray.pushMap(lineMap) | ||
} | ||
return lineArray | ||
} | ||
|
||
private fun getElementArray(elements: MutableList<Text.Element>): WritableNativeArray { | ||
val elementArray = WritableNativeArray() | ||
|
||
for (element in elements) { | ||
val elementMap = WritableNativeMap() | ||
|
||
elementMap.putString("text", element.text) | ||
elementMap.putArray("cornerPoints", element.cornerPoints?.let { getCornerPoints(it) }) | ||
elementMap.putMap("frame", getFrame(element.boundingBox)) | ||
} | ||
return elementArray | ||
} | ||
|
||
private fun getRecognizedLanguages(recognizedLanguage: String): WritableNativeArray { | ||
val recognizedLanguages = WritableNativeArray() | ||
recognizedLanguages.pushString(recognizedLanguage) | ||
return recognizedLanguages | ||
} | ||
|
||
private fun getCornerPoints(points: Array<Point>): WritableNativeArray { | ||
val cornerPoints = WritableNativeArray() | ||
|
||
for (point in points) { | ||
val pointMap = WritableNativeMap() | ||
pointMap.putInt("x", point.x) | ||
pointMap.putInt("y", point.y) | ||
cornerPoints.pushMap(pointMap) | ||
} | ||
return cornerPoints | ||
} | ||
|
||
private fun getFrame(boundingBox: Rect?): WritableNativeMap { | ||
val frame = WritableNativeMap() | ||
|
||
if (boundingBox != null) { | ||
frame.putDouble("x", boundingBox.exactCenterX().toDouble()) | ||
frame.putDouble("y", boundingBox.exactCenterY().toDouble()) | ||
frame.putInt("width", boundingBox.width()) | ||
frame.putInt("height", boundingBox.height()) | ||
frame.putInt("boundingCenterX", boundingBox.centerX()) | ||
frame.putInt("boundingCenterY", boundingBox.centerY()) | ||
} | ||
return frame | ||
} | ||
|
||
override fun callback(frame: ImageProxy, params: Array<Any>): Any? { | ||
|
||
val result = WritableNativeMap() | ||
result.putString("text", "Test Text") | ||
|
||
val blocks = WritableNativeArray() | ||
result.putArray("blocks", blocks) | ||
val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS) | ||
|
||
@SuppressLint("UnsafeOptInUsageError") | ||
val mediaImage: Image? = frame.getImage() | ||
|
||
if (mediaImage != null) { | ||
val image = InputImage.fromMediaImage(mediaImage, frame.imageInfo.rotationDegrees) | ||
val task: Task<Text> = recognizer.process(image) | ||
try { | ||
val text: Text = Tasks.await<Text>(task) | ||
result.putString("text", text.text) | ||
result.putArray("blocks", getBlockArray(text.textBlocks)) | ||
} catch (e: Exception) { | ||
return null | ||
} | ||
} | ||
|
||
val data = WritableNativeMap() | ||
data.putMap("result", result) | ||
|
||
return data | ||
} | ||
} |
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