2013-05-27 33 views
0

我使用工作燈v5.0.6和下載來自Apache的科爾多瓦現場一個示例應用程序來訪問設備上的照相機。相機插件IBM工作燈不工作

我給了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> 
+0

你的問題解決了嗎?標記爲已回答。 –

回答

2

這與Worklight無關,但與Cordova無關。

如果您正在服用科爾多瓦示例應用程序(而不是一個工作燈示例應用程序),您可以使用工作燈不運行它,因爲它不是一個工作燈項目(!)。

如果你想訪問設備功能(如相機),使用工作燈的應用程序,然後:

  1. 創建一個新的工作燈項目和應用程序
  2. 添加環境(安卓爲例)
  3. 按照Cordova Camera API

你也應該閱讀下列培訓模塊,瞭解科爾多瓦和工作燈是如何協同工作:Adding native functionality to hybrid apps with Apache Cordova

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 
    }); 

Here是一個示例代碼,可以很好的幫助。