agoldis / react-native-mlkit-ocr

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

"[TypeError: Cannot read property 'detectFromUri' of null]" #47

Open FranciscoNoble opened 9 months ago

FranciscoNoble commented 9 months ago

Expo Version: '49' React Native Version: '0.72'

So, I'm just trying to follow the example on README but everytime I try to scan any text using any method I get this annoying error "[TypeError: Cannot read property 'detectFromUri' of null]" or "[TypeError: Cannot read property 'detectFromFile' of null]"

I'm giving the function valid URIs but it has no effect, the error is still happening. Idk if this makes a difference, but I'm running the app through the Expo Go App on an Android 13 device

const OCR = async ( ) => {
     try{
         const result = await MlkitOcr.detectFromUri(valid_URI);
         console.log(result);
     }
     catch(error){
         console.error(error)
      }
}
ashylen commented 8 months ago

Any update to that?

BrunoVillanova commented 8 months ago

I will check this weekend. I hadn't had enough time. On 13 Mar 2024 at 20:27 -0300, Dominik Urban @.***>, wrote:

Any update to that? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

ashylen commented 8 months ago

@BrunoVillanova, please ignore, I just resolved the issue on my side and the library is totally fine. It's just problem when you try to run it with Expo Go.

For Expo Go users:

As per https://github.com/agoldis/react-native-mlkit-ocr/issues/33#issuecomment-1864817055 it seems that Expo Go cannot build your project with MlKit for some reason (despite the fact that you can use functions, it will always return "[TypeError: Cannot read property 'detectFromUri' of null]".

In order to make it work, you need to create an account at https://expo.dev/, setup your project and then build your project with EAS. There is plenty of Guides available on youtube how to do that.

My package.json file for reference.

{
  "name": "my-app",
  "version": "1.0.0",
  "main": "expo-router/entry",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "test": "jest"
  },
  "dependencies": {
    "@expo/metro-runtime": "~3.1.3",
    "@react-native-async-storage/async-storage": "1.21.0",
    "expo": "~50.0.11",
    "expo-constants": "~15.4.5",
    "expo-font": "~11.10.3",
    "expo-image-picker": "~14.7.1",
    "expo-linking": "~6.2.2",
    "expo-router": "~3.4.8",
    "expo-splash-screen": "~0.26.4",
    "expo-status-bar": "~1.11.1",
    "expo-system-ui": "~2.9.3",
    "jest": "^29.3.1",
    "jest-expo": "~50.0.3",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-native": "0.73.4",
    "react-native-mlkit-ocr": "0.3.0",
    "react-native-safe-area-context": "4.8.2",
    "react-native-screens": "~3.29.0",
    "react-native-web": "~0.19.6",
    "expo-dev-client": "~3.3.9"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0"
  },
  "private": true,
  "browser": {
    "crypto": false
  },
  "jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
    ]
  }
}

Possible duplicate of https://github.com/agoldis/react-native-mlkit-ocr/issues/33

huw-rhys-jones commented 7 months ago

I was having the same problem also and managed to get it to work by connecting my mobile Android device to my computer with a usb cable and running:

npm run android

Other than that I just followed the method in the readme