|
| 1 | +import android.R.attr.label |
| 2 | +import android.annotation.SuppressLint |
| 3 | +import android.graphics.Rect |
| 4 | +import android.media.Image |
1 | 5 | import androidx.camera.core.ImageProxy
|
| 6 | +import com.facebook.react.bridge.ReadableArray |
| 7 | +import com.facebook.react.bridge.WritableArray |
| 8 | +import com.facebook.react.bridge.WritableNativeArray |
| 9 | +import com.facebook.react.bridge.WritableNativeMap |
| 10 | +import com.google.android.gms.tasks.Task |
| 11 | +import com.google.android.gms.tasks.Tasks |
| 12 | +import com.google.mlkit.vision.common.InputImage |
| 13 | +import com.google.mlkit.vision.text.Text |
| 14 | +import com.google.mlkit.vision.text.TextRecognition |
| 15 | +import com.google.mlkit.vision.text.TextRecognizer |
| 16 | +import com.google.mlkit.vision.text.latin.TextRecognizerOptions |
2 | 17 | import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin
|
| 18 | +import com.mrousavy.camera.utils.pushInt |
| 19 | + |
3 | 20 |
|
4 | 21 | class VisionCameraOcrPlugin: FrameProcessorPlugin("scanOCR") {
|
5 | 22 |
|
6 |
| - override fun callback(image: ImageProxy, params: Array<Any>): Any? { |
7 |
| - // code goes here |
8 |
| - return null |
| 23 | + override fun callback(frame: ImageProxy, params: Array<Any>): Any? { |
| 24 | + @SuppressLint("UnsafeOptInUsageError") |
| 25 | + val mediaImage: Image? = frame.image |
| 26 | + val textRecognizer: TextRecognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS) |
| 27 | + val array = WritableNativeArray() |
| 28 | + |
| 29 | + |
| 30 | + if (mediaImage != null) { |
| 31 | + val image = InputImage.fromMediaImage(mediaImage, frame.imageInfo.rotationDegrees) |
| 32 | + var task: Task<Text> = textRecognizer.process(image) |
| 33 | + try { |
| 34 | + var extractedInfo = Tasks.await(task) |
| 35 | + for (block in extractedInfo.textBlocks) { |
| 36 | + for (line in block.lines) { |
| 37 | + val lineText = line.text |
| 38 | + val lineBoundingBox: Rect? = line.boundingBox |
| 39 | + val map = WritableNativeMap() |
| 40 | + map.putString("text", lineText) |
| 41 | + map.putInt("height", image.height) |
| 42 | + map.putInt("width", image.width) |
| 43 | + val bounds = WritableNativeArray() |
| 44 | + bounds.pushInt(lineBoundingBox?.left) |
| 45 | + bounds.pushInt(lineBoundingBox?.bottom) |
| 46 | + bounds.pushInt(lineBoundingBox?.right) |
| 47 | + bounds.pushInt(lineBoundingBox?.top) |
| 48 | + map.putArray("bounds", bounds) |
| 49 | + array.pushMap(map) |
| 50 | + } |
| 51 | + } |
| 52 | + } catch (e: Exception) { |
| 53 | + e.printStackTrace() |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + return array |
9 | 58 | }
|
10 | 59 | }
|
0 commit comments