1. Requirements Description
Implement a call camera to take pictures, or directly open the map library to select Photos, and then replace the image in the app function
2. Preparation
1) Install Ng-cordova
Go to the Ionic engineering catalog, install with the Bower tool,
Bower Install Ngcordova
Then add Ng-cordova.js or Ng-cordova.min.js to the Cordova.js reference in index.html
...
<script src= "Lib/ngcordova/dist/ng-cordova.js" ></script>
<script src= "lib/cordova.js></script>
...
Adding Ngcordova dependencies in angular
Angular.module (' myApp ', [' Ngcordova '])
2) Installation $cordovaCamera
Cordova Plugin Add Cordova-plugin-camera
Adding dependencies to the controller
. Controller (' AppControl ', [' $cordovaCamera ']) {
...
}
3) Install $cordovaimagepicker
Cordova Plugin Add Cordova-plugin-image-picker
Adding dependencies to the controller
. Controller (' AppControl ', [' $cordovaImagePicker ']) {
...
}
3. Code implementation
1("Deviceready",function(){2//Take Pictures3varoptions={4Quality:50, //Save image quality, range 0-100 5DestinationType:Camera.DestinationType.DATA_URL, //return value format: Data_url=0, returned as Base64 encoded string; file_url=1, returns the URL of the image ; native_rul=2, return the image native URL 6SourceType:Camera.PictureSourceType.CAMERA, //Set Picture Source: photolibrary=0, Camera =1, 7AllowEdit:true, //whether to allow editing before selecting a picture 8EncodingType:Camera.EncodingType.JPEG, //jpen = 0,png = 1 9targetwidth:100, //Zoom the width of the image (in pixels) Tentargetheight:100, //scale image height (pixels) Onepopoveroptions:camerapopoveroptions, //ios,ipad pop-up position ASavetophotoalbum:true, //whether to save to album -Correctorientation:true //Set whether the image taken by the camera is in the correct direction -}; the$cordovaCamera. Getpicture (Options). Then (function(imageData) { -$scope. imagesrc= "Data:image/jpeg;base64," +ImageData; -},function(err) { - //Error +}); -},false);
1Document.addeventlistener ("Deviceready",function(){2 varOptions = {3Maximumimagescount:10,//maximum number of pictures selected4width:800, //filter Width: If the width is 0, return all dimensions of the picture 5height:800, //filter Height: If the height is 0, return all dimensions of the picture 6Quality:80//image quality of the size, the default is7 };8$cordovaImagePicker. Getpictures (Options). Then (function(results) {9 for(vari = 0; i < results.length; i++) {TenAlert (' Image URI: ' +results[i]); One } A},function(Error) { - //Error getting photos - }); the},false);
Ng-cordova Mobile photos or select pictures from albums