This article transferred from: http://blog.csdn.net/greystar/article/details/47973837
1. Make sure the environment is OK, if there is an error, Ann download the relevant version of the Android SDK, I also toss a few days on this.
2. Create a default Cordova project, and add the relevant plug-in, in the project directory Open Config. ins will open the configuration interface, you can choose the camera plug-in, optional installation.
3. Call
Note: You cannot set an event call in HTML
Open www\index.html, do a demo of the interface
<body> <p>hello, your application is ready!</p>
<!--Cordova Reference, which is added to the application when it is built. --<script src= "Cordova.js" ></script> <script src= "Scripts/platformoverrides.js" ></script >
<script src= "Scripts/index.js" ></script>//The following two lines are examples, others are the template comes with, do not need to be adjusted, can not directly control the event call < onclick= "" > &L T;input type= "button" value= "Call Camera" id= "Btntakecamera"/> &L T;/body>
Add JS call, here directly modify the Index.js
[JavaScript]View Plaincopyprint?
- Document.addeventlistener ("Deviceready", Ondeviceready, false);
- function Ondeviceready () {
- document.getElementById ("Btntakecamera"). onclick = Takecamera;
- Alert ("Device initialization is normal");
- }
- function Takecamera () {
- Navigator.camera.getPicture (onsuccess, Onfail, {
- QUALITY:50,
- DestinationType:Camera.DestinationType.DATA_URL,
- EncodingType:Camera.EncodingType.JPEG,
- TARGETWIDTH:100,
- TARGETHEIGHT:100,
- });
- }
- function onsuccess (imageData) {
- var image = document.getElementById (' takeimg ');
- IMAGE.SRC = "data:image/jpeg;base64," + imageData;
- }
- function Onfail (message) {
- Alert (' Failed because: ' + message);
- }
document.addEventListener ("deviceready", onDeviceReady, false);
function onDeviceReady () {
document.getElementById ("btntakeCamera"). onclick = takeCamera;
alert ("device initialization is normal");
}
function takeCamera () {
navigator.camera.getPicture (onSuccess, onFail, {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
});
}
function onSuccess (imageData) {
var image = document.getElementById (‘takeImg’);
image.src = "data: image / jpeg; base64," + imageData;
}
function onFail (message) {
alert (‘Failed because:‘ + message);
}
Camera specific parameters in Plugins\cordova-plugin-camera \doc\zh\index.md detailed introduction, traditional help, we should all be able to understand.
Go VS2015 Cordova Try-camera