Ionic Cordova相机无法正常工作

时间:2015-10-12 12:42:24

标签: javascript cordova

我正在尝试使用离子科尔多瓦相机。

我有以下代码

HomePage.html

<ion-view view-title="Example">
  <ion-content>
    <img ng-show="imgURI !== undefined" ng-src="{{imgURI}}">
    <img ng-show="imgURI === undefined" ng-src="http://placehold.it/300x300">
    <button class="button" ng-click="takePicture()">Take Picture</button>
  </ion-content>
</ion-view>

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
  </head>
  <body ng-app="starter">

 <ion-nav-view></ion-nav-view>

</body>
</html>

controllers.js

angular.module('starter.controllers', [])

.controller('HomePageCtrl', function ($scope, $state, $location, $cordovaCamera) {

    $scope.data = {};


    $scope.takePicture = function () {
        var options = {
            quality: 75,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.CAMERA,
            allowEdit: true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function (imageData) {
            $scope.imgURI = "data:image/jpeg;base64," + imageData;
        }, function (err) {
            // An error occured. Show a message to the user
        });
    }
})

app.js

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])

.run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {

        if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);

        }
        if (window.StatusBar) {
            // org.apache.cordova.statusbar required
            StatusBar.styleLightContent();
        }
    });
})

.config(function ($stateProvider, $urlRouterProvider) {

    $stateProvider

         .state('HomePage', {
             url: '/HomePage',
             templateUrl: 'templates/HomePage.html',
             controller: 'HomePageCtrl'
         })
         .state('login', {
             url: '/login',
             templateUrl: 'templates/Login.html',
             controller: 'LoginCtrl'
         });

    $urlRouterProvider.otherwise('/login');  
});

问题:

当我点击按钮以使用相机

我得到以下异常:

 Module 'ngCordova' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

我错过了上方

任何帮助将不胜感激。

感谢。

3 个答案:

答案 0 :(得分:2)

采取弗罗姆here

  

在index.html文件中包含ng-cordova.js或ng-cordova.min.js   在cordova.js之前和你的AngularJS / Ionic文件之后(从那以后)   ngCordova依赖于AngularJS)。

简而言之:index.html中缺少ngCordova。

答案 1 :(得分:1)

当您将Image发送到数据库时,图像来自base64 string中的摄像头。因此,您自动发送到数据库的该字符串。     在项目中安装 cordova插件添加org.apache.cordova.camera

 +------+------+------+------+------+--------------------------+
 | NDEF Message= 3(0x03) (1 byte)                              |        
 +-------------------------------------------------------------+
 | LEGTH=69 (0x45)(1 byte)                                     |
 +------+------+------+------+------+--------------------------+
 | MB=1 | ME=0 | CF=0 | SR=1 | IL=0 | TNF=2 (MIME type record) |(0x92)
 +------+------+------+------+------+--------------------------+
 | TYPE LENGTH=09 (0x90)                                       |
 +-------------------------------------------------------------+
 | PAYLOAD LENGTH=17 (0x11)(1 byte)                            |
 +-------------------------------------------------------------+
 | TYPE="co/info"  (9 byte)                                    |
 +-------------------------------------------------------------+
 | PAYLOAD="bd://aa:bb:cc:dd:ee:ff" (55 byte)                  |
 +------+------+------+------+------+--------------------------+
 | MB=0 | ME=1 | CF=0 | SR=1 | IL=0 | TNF=4 (External type)    |
 +------+------+------+------+------+--------------------------+
 | TYPE LENGTH=15 (0x0F)                                       |
 +-------------------------------------------------------------+
 | PAYLOAD LENGTH=22 (0x16)                                    |
 +-------------------------------------------------------------+
 | TYPE="android.com:pkg"                                      |
 +-------------------------------------------------------------+
 | PAYLOAD="com.your.app.package"                              |
 +-------------------------------------------------------------+

答案 2 :(得分:1)

首先你需要定义ngCordova脚本(beetween ionic.bundle.js脚本和cordova.js脚本)

<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>

你也可能需要安装白名单插件并添加meta到索引,因为在某些移动设备中它不会因为中心安全策略权限违规而无法使用

<meta http-equiv="Content-Security-Policy"
content="default-src 'self' https://*/ http://*/ data: gap: content: 
'unsafe-eval' 'unsafe-inline';
               script-src 'self' 'unsafe-inline' 'unsafe-eval' *;
               style-src  'self' 'unsafe-inline' *">

这样你也可以使用pictureSelection