Javascript 爱奥尼亚科尔多瓦相机坏了
我正在尝试使用爱奥尼亚科尔多瓦相机 我有下面的代码 HomePage.htmlJavascript 爱奥尼亚科尔多瓦相机坏了,javascript,cordova,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">
<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>
<!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>
app.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
});
}
})
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.
我在上面错过的地方
任何帮助都将不胜感激
谢谢。摘自:
在index.html文件中包括ng-cordova.js或ng-cordova.min.js
在cordova.js之前和AngularJS/Ionic文件之后(自
ngCordova取决于AngularJS)
简而言之:您的index.html中缺少ngCordova。是
当您将图像发送到数据库时,图像来自摄像头的base64字符串。因此,您会自动将该字符串发送到数据库。
在您的项目中安装cordova插件add org.apache.cordova.camera
var options = {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false,
correctOrientation:true
};
$cordovaCamera.getPicture(options).then(function(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
$scope.Base64string="data:image/jpeg;base64," + imageData;
}, function(err) {
// error
});
write this in your controller and base64 string came in $scope.Base64string but when you send this string one problem came that is you have to store this amount of data in the middle **spaces** came so to neglect the one you will write in services
srting Image="data:image/jpeg;base64,/9j/sfgdhbygcecechghchchvfhcghfvhv57egdhbgh.........";
string imagerty = Image.Replace(" ", "+");
Or
in Angularjs
var str = 'data:image/jpeg;base64,/9j/sfgdhbygcecechghchchvfhcghfvhv57egdhbgh.........';
var replaced = str.split(' ').join('+');
首先,您需要定义ngCordova脚本(beetween-ionic.bundle.js脚本和cordova.js脚本)
此外,您可能需要安装白名单插件并将元数据添加到索引中,因为在某些移动设备中,由于违反centent安全策略权限,该插件无法工作
<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
<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' *">