问题
I'm trying to implement a camera to my Ionic 1 project.
But I can't find any reliable examples of how to do that. I found: https://www.thepolyglotdeveloper.com/2014/09/use-android-ios-camera-ionic-framework/ and https://github.com/apache/cordova-plugin-camera and some older Stack Overflow entries.
Still, I haven't got it running myself.
回答1:
You're on the right track already! What you found is the most popular camera plugin for Cordova:
https://github.com/apache/cordova-plugin-camera
This is a pure Cordova plugin though, meaning that it's not adjusted in any way for Ionic. This means, you just add it to your project and can use it as soon as Ionic is ready:
ionic.Platform.ready( function() {
navigator.camera.getPicture(onSuccess, onFail, options);
});
But passing callbacks as params is indeed not the angular way to do this. So on top of the basic Cordova camera plugin you can add ngCordova to enhance the handling.
To install and add ngCordova to your project follow these instructions:
http://ngcordova.com/docs/install/
To wrap it up:
- Install ngCordova via bower
- Add js reference to your index.html
- Add the ngCordova module as a dependency to your app.js
- In case you've added everything correctly, inject $cordovaCamera in your controller, directive or service to use it.
This allows you to access the camera the angular way, more about it you can find here:
http://ngcordova.com/docs/plugins/camera/
/**
* taken from the docs linked above
* you can now make use of promises here!
*/
$cordovaCamera.getPicture(options).then(function(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}, function(err) {
// error
});
Hope this helps integrating the camera successfully in your project. ;)
来源:https://stackoverflow.com/questions/40513646/how-to-integrate-cordoba-camera-plugin-in-ionic-1-project