0

我正在使用 Worklight v5.0.6 并从 Apache Cordova 站点下载了一个示例应用程序,用于访问设备摄像头。

我在 AndroidManifest.xml 中提供了所有适当的权限,但是当我在模拟器中运行它时,按钮不起作用,即没有调用函数并且我无法访问相机。

<!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>
4

2 回答 2

2

这与 Worklight 无关,但与 Cordova 有关。

如果您使用的是 Cordova 示例应用程序(而不是 Worklight 示例应用程序),则无法使用 Worklight 运行它,因为它不是 Worklight 项目(!)。

如果您想使用 Worklight 应用程序访问设备功能(例如相机),那么:

  1. 创建新的 Worklight 项目和应用程序
  2. 添加环境(以安卓为例)
  3. 关注Cordova 相机 API

您还应该阅读以下培训模块以了解 Cordova 和 Worklight 如何协同工作:使用 Apache Cordova 向混合应用程序添加本机功能

于 2013-05-27T08:29:27.380 回答
0

虽然回答了一个较旧的问题,但它会帮助其他人。

非常容易在 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
    });

是一个示例代码,可能也会有所帮助。

于 2014-11-11T18:56:53.597 回答