CVO
CVO

Reputation: 742

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?

Upvotes: 0

Views: 129

Answers (2)

Swissdude
Swissdude

Reputation: 3556

That's an older thread but since this hasn't been fixed in the plugin, for everybody who's running into the same problem, here's a solution:

Instead of always saving the image to the gallery and then tediously fetching it from there, storing it somewhere else and delete the image from the gallery, you can simply add this to the index.ios.js:

On line 490 ff you'll find the method MySwifty.prototype.savePhoto

On iOS, this._photoToSave is a UIImage and ImageAsset doesn't seem to like that. Hence the asset is empty.

So I added this to the method:

if (isIOS) {

  const imageFolder = knownFolders.documents(); 
  const iosImage = UIImageJPEGRepresentation(this._photoToSave, 0.7);

  const result1 = NSFileManager.defaultManager.createFileAtPathContentsAttributes(imageFolder.path + "/cam_capture.jpg", iosImage, null);

  const asset = new ImageAsset(imageFolder.path + "/cam_capture.jpg");

   _this._owner.get().sendEvent(CameraPlus.photoCapturedEvent, asset);
   _this.resetPreview();

}

This saves the image and returns an image asset to the photoCapturedEvent.

You can of course change the image name to something more generic and change the image-path, too.

If you want to save the image as PNG instead of JPG, then you'd use

const iosImage = UIImagePNGRepresentation(this._photoToSave);

Upvotes: 0

Manoj
Manoj

Reputation: 21908

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.

Upvotes: 1

Related Questions