Cordova 摄像头插件ibm worklight不工作
我正在使用Worklight v5.0.6,并从Apache Cordova站点下载了一个示例应用程序,用于访问设备摄像头 我在AndroidManifest.xml中提供了所有适当的权限,但当我在模拟器中运行它时,按钮不起作用,即没有调用函数,也没有访问相机Cordova 摄像头插件ibm worklight不工作,cordova,ibm-mobilefirst,Cordova,Ibm Mobilefirst,我正在使用Worklight v5.0.6,并从Apache Cordova站点下载了一个示例应用程序,用于访问设备摄像头 我在AndroidManifest.xml中提供了所有适当的权限,但当我在模拟器中运行它时,按钮不起作用,即没有调用函数,也没有访问相机 <!DOCTYPE html> <html> <head> <title>Capture Photo</title> <script type="text/javascr
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.6.0.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for Cordova to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// Cordova is ready to be used!
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64 encoded image data
// console.log(imageData);
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
largeImage.src = imageURI;
}
// A button will call this function
//
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function capturePhotoEdit() {
// Take picture using device camera, allow edit, and retrieve image string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20,allowEdit:true,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From PhotoAlbum</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
拍摄照片
var pictureSource;//图像源
var destinationType;//设置返回值的格式
//等待Cordova连接到设备
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//科尔多瓦已经准备好使用了!
//
函数ondevicerady(){
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.destinationType;
}
//成功检索照片时调用
//
函数onPhotoDataSuccess(imageData){
//取消注释以查看base64编码的图像数据
//控制台日志(imageData);
//获取图像句柄
//
var smallImage=document.getElementById('smallImage');
//取消隐藏图像元素
//
smallImage.style.display='block';
//显示捕获的照片
//内联CSS规则用于调整图像大小
//
smallImage.src=“数据:图像/jpeg;base64,”+imageData;
}
//成功检索照片时调用
//
函数onPhotoURISuccess(imageURI){
//取消注释以查看图像文件URI
//log(imageURI);
//获取图像句柄
//
var largeImage=document.getElementById('largeImage');
//取消隐藏图像元素
//
largeImage.style.display='block';
//显示捕获的照片
//内联CSS规则用于调整图像大小
//
largeImage.src=imageURI;
}
//一个按钮将调用此函数
//
函数capturePhoto(){
//使用设备摄像头拍照,并将图像作为base64编码字符串检索
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{质量:50,
destinationType:destinationType.DATA_URL});
}
//一个按钮将调用此函数
//
函数capturePhotoEdit(){
//使用设备照相机拍照,允许编辑和检索图像字符串
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:20,allowEdit:true,
destinationType:destinationType.DATA_URL});
}
//一个按钮将调用此函数
//
函数getPhoto(源代码){
//从指定源检索图像文件位置
navigator.camera.getPicture(onPhotoURISuccess,onFail,{质量:50,
destinationType:destinationType.FILE\u URI,
sourceType:source});
}
//如果有什么不好的事情发生了就打电话。
//
函数onFail(消息){
警报('失败原因:'+消息);
}
拍摄照片
捕获可编辑照片
从照片库
从相册
这与Worklight无关,但与Cordova有关
如果您正在使用Cordova示例应用程序(而不是Worklight示例应用程序),则无法使用Worklight运行它,因为它不是Worklight项目(!)
如果要使用Worklight应用程序访问设备功能(如照相机),则:
您还应该阅读以下培训模块,以了解Cordova和Worklight是如何协同工作的:虽然回答了一个较老的问题,但它会帮助其他人 在Worlight项目中调用和使用摄像头功能非常容易 比如,;打开摄像头的步骤
navigator.camera.getPicture(onSuccessCallBack, onFailCallBack, {
quality: 50,
sourceType: Camera.PictureSourceType.CAMERA,
destinationType: Camera.DestinationType.FILE_URI
});
打开图库的步骤
navigator.camera.getPicture(onSuccessCallBack, onFailCallBack, {
quality: 50,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
destinationType: Camera.DestinationType.FILE_URI
});
是一个示例代码,可能也会有所帮助。您的问题解决了吗?标记为已回答。