Ionic 2 (typescript) use camera


#1

Does anyone know how can i communicate with the device’s camera?

upload image
take picture

Can’t find any tutorial on this subject…

please help :slight_smile:


#2
import {Camera} from 'ionic-native';

#3

Just to elaborate a bit more, you can use the ionic-native package which is a set wrappers for cordova plugins.

You can use

npm install ionic-native --save

Then import the package

import {Camera} from 'ionic-native';

This will give you the autocompletion and make sure everything works fine.

Don’t forget to install the native plugin as well!

http://ionicframework.com/docs/v2/native/Camera/


#4

Thanks for quick reply , but i still got an error


#5

Hi @mhartington ,

The plugin seems to be working fine, but I am getting URI and File path while using Camera.PictureSourceType as CAMERA and PHOTOLIBRARY.

###following is the sample values returned for each options:

PHOTOLIBRARY : content://media/external/images/media/51519
CAMERA : file:///storage/emulated/0/Android/data/io.ionic.starter/cache/1460012636978.jpg

How to convert the PHOTLIBRARY uri vallue to file location url?
And file location value returned from the CAMERA option is not setting up the image in image element.


#6

You could use FileReader.readAsDataURL()
google it


#7

Thanks. I have implemented with DATA_URL as the Camera.DestinationType and used BLOB field in database.

With the awareness of the warning:

Return base64 encoded string. DATA_URL can be very memory intensive and cause app crashes or out of memory errors. Use FILE_URI or NATIVE_URI if possible