jonathanpalma / react-native-tesseract-ocr

Tesseract OCR wrapper for React Native
MIT License
565 stars 172 forks source link

OCR path invalid (?) #68

Closed YeisonVelez11 closed 4 years ago

YeisonVelez11 commented 4 years ago

I am using react-native

this is my file generated by (react-native-camera)

file:///data/user/0/com.testproject/cache/Camera/75e0563e-d2c8-47b5-928b-b6d5be8c13c1.jpg

uri=file:///data/user/0/com.testproject/cache/Camera/75e0563e-d2c8-47b5-928b-b6d5be8c13c1.jpg

   RNTesseractOcr.recognize(uri, 'LANG_ENGLISH',     tessOptions)
    .then(result => {`
vbridier commented 4 years ago

const path = uri.replace('file://', '');

mihailpreda commented 4 years ago

The path is correct, but you need to grant read/write permissions : add in android/app/src/main/AndroidManifest.xml : <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Then you need to use AndroidPermissions , so import {PermissionsAndroid} from 'react-native'; and then define something like this :

 requestRWPermissions= async ()=> {
        const checkReadExternalStorage = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE);
        const checkWriteExternalStorage = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
        if (checkReadExternalStorage === PermissionsAndroid.RESULTS.GRANTED && checkWriteExternalStorage === PermissionsAndroid.RESULTS.GRANTED ) {
            //alert("You've have read/write permission");
        } else {
            try {
                const grantedRead = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
                    {
                        'title': 'FollowUP App required read external storage permission',
                        'message': 'We required read external storage permission in order to get device location ' +
                            'Please grant us.'
                    }
                )
                const grantedWrite = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
                    {
                        'title': 'FollowUP App required write external storage permission',
                        'message': 'We required write external storage permission in order to get device location ' +
                            'Please grant us.'
                    }
                )
                if (grantedRead === PermissionsAndroid.RESULTS.GRANTED && grantedWrite === PermissionsAndroid.RESULTS.GRANTED) {
                    this.takePicture();

                } else {
                    //alert("You don't have read/write permission");
                }
            } catch (err) {
                alert(err)
            }
        }
    };

and takePicture is something like :

    takePicture = async () => {
        if (this.camera) {
            const options = { quality: 0.9, base64: true };
            const data = await this.camera.takePictureAsync(options);

            const tessOptions = {
                whitelist: null, 
                blacklist: '1234567890\'!"#$%&/()={}[]+*-_:;<>'
              };

            RNTesseractOcr.recognize(data.uri.replace('file://', ''), 'LANG_ENGLISH', tessOptions)
                .then((result) => {
                  this.setState({ ocrResult: result });
                  console.log("OCR Result: ", result);
                })
                .catch((err) => {
                  console.log("OCR Error: ", err);
                })
                .done();
        }
    }

or try after you install it on phone to give manually from file explorer the permissions

jonathanpalma commented 4 years ago

Hi @YeisonVelez11, as @mihailpreda commented it is necessary to grant read/write permissions and also remove the substring file:// from your path. You don't have to do this manually anymore since it is handled by the library since v2.

https://github.com/jonathanpalma/react-native-tesseract-ocr/blob/8bd4851edb0902a8100a33ec5f4532df0589a262/android/src/main/java/com/reactlibrary/TesseractOcrModule.java#L228