2016-04-29 50 views
1

Iam在我的JavaScript項目中使用cordova在攝像頭功能上工作。我在我的項目中使用了cordova.js和camera.js文件。但是當我調用camera.js的getPicture()函數時,它在我的設備中不起作用。你可以在我的html中找到下面的代碼。請幫我做到這一點。提前致謝。攝像頭功能與cordova.js/camera.js不兼容

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    
 
    <script src="cordova.js" type="text/javascript" ></script> 
 
    <script src="Camera.js" type="text/javascript" ></script> 
 
    <script language="javascript"> 
 

 
    \t \t var pictureSource; 
 
    \t \t var destinationType; 
 

 
\t   document.addEventListener("deviceready",onDeviceReady,false); 
 

 
\t \t function onDeviceReady() { 
 
\t \t \t pictureSource=navigator.camera.PictureSourceType; 
 
\t \t \t destinationType=navigator.camera.DestinationType; 
 
\t \t } 
 

 
\t \t function onPhotoDataSuccess(imageData) { 
 
\t \t \t var smallImage = document.getElementById('smallImage'); 
 
\t \t \t smallImage.style.display = 'block'; 
 
\t \t \t smallImage.src = "data:image/jpeg;base64," + imageData; 
 
\t \t } 
 

 
\t \t function capturePhoto() { 
 
\t \t \t navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, 
 
\t \t \t \t destinationType: destinationType.DATA_URL }); 
 
\t \t } 
 

 

 
\t \t function getPhoto(source) { 
 
\t \t \t navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
 
\t \t \t \t destinationType: destinationType.FILE_URI, 
 
\t \t \t \t sourceType: source }); 
 
\t \t } 
 
\t \t function onFail(message) { 
 
\t \t \t alert('Failed because: ' + message); 
 
\t \t } 
 
    
 
    
 
    document.getElementById('cam1').innerHTML='<a href="javascript:void(0)" onclick="capturePhoto()"><img src="../images/camera_up.gif" alt="img" width="23" height="20" border="0" id="smallImage" /></a>'; 
 
    </script>

+0

什麼Log Cat顯示?任何消息? –

+1

也許'DATA_URL'類型消耗了太多的內存,操作系統會中斷你的腳本?嘗試使用'Camera.DestinationType.FILE_URI'類型來代替,看看它是否工作... – Uzbekjon

+0

@jaydroider我得到這個錯誤:未捕獲TypeError:無法讀取屬性'getPicture'未定義在日誌貓 – User

回答

1

嘗試通過命令行控制檯安裝插件,找到你的項目,然後使用這個命令。

  • 科爾多瓦版本5.0 +

cordova plugin add cordova-plugin-camera

  • 舊版本

cordova plugin add org.apache.cordova.camera

此命令在config.xml中自動添加引用

刪除該參考文獻:

<script src="Camera.js" type="text/javascript" ></script> 
+0

也確保'cordova_plugins.js'正確加載。我昨天遇到了一個問題。 –