agoldis / react-native-mlkit-ocr

Google on-device MLKit Text Recognition for React Native
MIT License
176 stars 41 forks source link

detectFromFile not implemented on Android #8

Closed dllz closed 3 years ago

dllz commented 3 years ago

Hey, using the latest version 0.2.2 and it seems detectFromFile is not implemented in the android project just detectFromUri. 00:10:27 | WARN | Failed to do text recognition MlkitOcr.detectFromFile is not a function. (In 'MlkitOcr.detectFromFile(path)', 'MlkitOcr.detectFromFile' is undefined)

agoldis commented 3 years ago

oh shoot

Nilesh8451 commented 3 years ago

Any updates on this issue ?

ahmed-ismail-nt commented 3 years ago

@Nilesh8451 You can use detectFromUri with file paths. See the below extract from the ML Kit docs.

To create an InputImage object from a file URI, pass the app context and file URI to InputImage.fromFilePath(). This is useful when you use an ACTION_GET_CONTENT intent to prompt the user to select an image from their gallery app.

Inspecting detectFromUri shows that it uses InputImage.fromFilePath as expected:

@ReactMethod
    fun detectFromUri(uri: String, promise: Promise) {
      val image: InputImage;
      try {
        image = InputImage.fromFilePath(reactApplicationContext,  Uri.parse(uri));
        val recognizer = TextRecognition.getClient();
        recognizer.process(image).addOnSuccessListener { visionText ->
          promise.resolve(getDataAsArray(visionText))
        }.addOnFailureListener { e ->
          promise.reject(e);
          e.printStackTrace();
        }
      } catch (e: Exception) {
        promise.reject(e);
        e.printStackTrace();
      }
    }

TLDR: I believe detectFromFile would be an alias for the detectFromUri function for Android.