Search code examples
javascriptcameranativescript

NativeScript core CameraPlus with MLKit doesn't works if saveToGallery is false


NativeScript core CameraPlus with MLKit doesn't works if saveToGallery is false. Are thera any method to do this without saving the photo in galery? It works with camera basic plugin.

Here is my code:

const HomeViewModel = require("./home-view-model");
const firebase = require("nativescript-plugin-firebase");

exports.onNavigatingTo = function (args) {
    page = args.object;
    mv = page.bindingContext = new HomeViewModel();

    page.getViewById("camPlus")
        .addEventListener("photoCapturedEvent", photoCapturedEvent);
};


exports.onCapture = function() {
    camera = page.getViewById("camPlus");
    //Must be false
    camera.takePicture({ saveToGallery: false});
};

function photoCapturedEvent(args) {
    const source = new imageSourceModule.ImageSource();
    source.fromAsset(args.data).then((imageSource) => {
            getTextFromPhoto(imageSource);
        }).catch(function (err) {
            console.log("Error -> " + err.message);
        });
}

function getTextFromPhoto(imageSource) {

    firebase.mlkit.textrecognition.recognizeTextOnDevice({
        image: imageSource
    }).then(function (result) {

        mv.idContainer = getDataFromCameraText(result.text);
        if (mv.idContainer == "") {
            getTextFromPhotoOnline(imageSource);
        } else {
            containerDataIsValid(true);
        }

    }).catch(function (errorMessage) {
        return console.log("ML Kit error: " + errorMessage);
    });
}

The method "photoCapturedEvent" gives me an error of undefined:

JS: Error -> undefined

JS: Asset '/storage/emulated/0/Android/data/org.nativescript.gScanContainer/files/IMG_1543942676583.jpg' cannot be found.

how could I get te image without saving it?


Solution

  • I checked the source code of the plugin and it seems to be a bug. As the error says, they never save the image data in the path they pass on to the photo captured event.

    So the only option for you would be, always enable saveToGallery and delete the file once you are done with getTextFromPhoto.