首先要有平台环境,我这里用的是安卓的环境,安装相机插件,在控制台运行
cordova plugin add cordova-plugin-camera
安装完成之后,在index.html插入如下代码:
<button id="imageCapture" style="width:200px;height:40px;background: orange;">take photo</button>
<img src="" alt="" id="myImage" style="width:200px;height:200px;"/>
在js文件下的index.js里处理调用相机事件:
onDeviceReady函数里加入这个:
document.getElementById('imageCapture').addEventListener("click", imageCapture);
在js文件的最后写这个函数:
//捕捉图片
function imageCapture() {
navigator.camera.getPicture(onSuccess, onFail, {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
};
完成之后,在控制台执行 cordova build android --release
找到打包好的apk,发送到手机,手机上的运行效果如图:
如此就可以调用原生的相机了。