Can't Get from Nativescript Camera Plugin to byte array


#1

I’m trying to figure out how to let the user take a picture with a camera, and then take that captured picture and upload it to a service. The service requires a byte array, but for the life of me, I can’t figure out any code to get from an imageAsset to a byte array. I’ve tried converting it to an image source, but still can’t find a path to a byte array. I’ve tried saving it locally, but that always fails silently. I m having the camera save it to the camera roll, and I see that it is being created successfully.

Any ideas to go from imageasset to byte array?


#2

hi, normally I have to convert my images to base64, but I see some documentation here in the ImageSource module about ‘loadFromData’ - "the native data (byte array) to load the image from. So you can convert from byte arrays to an image source (https://github.com/NativeScript/NativeScript/blob/5e0de0a/tns-core-modules/image-source/image-source.d.ts#L135) but I don’t see the opposite. @pete.k any guidance here?


#3

Hi, if you want to send pictures you should try this plugin:

https://github.com/NativeScript/nativescript-background-http

For byte array, have you try this?

     var file = new java.io.File("full path to file");
     var size = file.length();
     var bytes = new byte[size];
     try {
          var buf = new java.io.BufferedInputStream(new java.io.FileInputStream(file));
          buf.read(bytes, 0, bytes.length);
          //do something
          buf.close();
     } catch (ex) {
          console.log(ex);
     }

#4

Thank you for the suggestion, but part of what I am struggling with is getting the path information from the {N} camera module.


#5

do you want the full path? something like this?

var flgEnter=false;
btnFoto.on(buttonModule.Button.tapEvent, function (args) {
    flgEnter=true;
    camera.takePicture()
        .then(function (imageAsset) {
            console.log("Result is an image asset instance");
            if(imageAsset && imageAsset.android && flgEnter){
                flgEnter=false;
                console.log("full path:"+imageAsset.android);
            }
        }).catch(function (err) {
            console.log("Error -> " + err.message);
        });
});

#6

When I do that, I get:
fullpath = <PHAsset: 0x1031cb830> DE8881C3-5ED6-441E-AE9E-6E9A46C0FACE/L0/001 mediaType=1/0, sourceType=1, (2448x3264), creationDate=2017-11-17 18:30:50 +0000, location=0, hidden=0, favorite=0

Still doesn’t seem to give me path info.


#7

Hi, change the code:

console.log("full path:"+imageAsset.android);

to:

console.log("full path:"+imageAsset.android);
console.dir(imageAsset);

It’ll display all the data in json format and you’ll see the path property.


#8

I’m on ios, and when I use console.dir it just outputs {object Object]


#9

are you running on iOS?


#10

Yes. I am running on iOS.


#11

I, I don’t have a license to run in a real iOS device, the emulator doesn’t have the option to take photos. But in theory imageAsset.ios should return the path.


#12

I determine paths using a path service in my Angular app:

import {Injectable, Inject, NgZone} from '@angular/core';
import * as fs from 'file-system';

@Injectable()
export class FilesService {
  constructor(
    private ngZone: NgZone,
  ) { }


  public getFilename(path: string) {
    let parts = path.split('/');
    return parts[parts.length - 1];
  }

  public documentsPath(filename: string) {
    return `${fs.knownFolders.documents().path}/${filename}`;
  }

}


#13

But this presumes you have a path string to begin with doesn’t it? I’m not able to get the path string.


#14

If you don’t mind digging through my codebase…I take images and save them to Firebase in Giftler (https://github.com/jlooper/giftler) - in the example I mentioned above, I’m simply taking a picture, converting to base64 string, and sending to a REST API service, not saving to device