How to integrate Cordoba Camera plugin in Ionic 1 project

随声附和 提交于 2019-12-12 04:31:59

问题


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:

  1. Install ngCordova via bower
  2. Add js reference to your index.html
  3. Add the ngCordova module as a dependency to your app.js
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!