2012-04-16 142 views
1

我想在Android手機上使用phonegap捕捉圖像。我嘗試在Phonegap頁面上發佈的代碼,但未出現相機。任何幫助表示讚賞。非常感謝。如何使用手機在Android手機上顯示相機

這是顯示我的手機

It is the display on my phone, there is no camera

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Capture Photo</title> 

    <script type="text/javascript" charset="utf-8" src="cordova-1.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.DATA_URL }); 
    } 

    // A button will call this function 
    // 
    function capturePhotoEdit() { 
     // Take picture using device camera, allow edit, and retrieve image as base64-encoded string 
     navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true, 
     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 Photo Album</button><br> 
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" /> 
    <img style="display:none;" id="largeImage" src="" /> 
    </body> 
</html> 
+0

你怎麼在「亞行logcat」看到當你點擊相機按鈕中的一個? – 2012-04-16 18:57:01

+0

謝謝,我解決了這個問題。我嘗試Phonegap1.0.0,它的工作原理。我不知道爲什麼。 Phonegap1.6有什麼問題嗎? – 2012-04-17 09:13:22

+0

這將是很好,找出什麼問題在1.6。你可以做logcat步驟嗎? – 2012-04-17 14:23:56

回答

0

對你添加的權限和相機功能在您AndroidMenifest文件? 如果你沒有添加,然後再試一次。

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 
6

無論您capturePhoto()capturePhotoEdit()功能需要有destinationType更新。

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

應該是這樣的,

navigator.camera.getPicture(
    onPhotoDataSuccess, 
    onFail, 
    { 
     quality: 50, 
     destinationType: destinationType.DATA_URL 
    } 
); 
0

在你的代碼改變科爾多瓦版本。 現在你有「cordova-1.6.0.js」 你應該把它改爲「cordova-2.5.0.js」 我認爲它會在那之後起作用。 這樣

<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script> 
0

我一直有同樣的問題,我已經得到了getPhoto()函數現在的工作,但仍無法獲得cameraPhoto()或cameraPhotoEdit()函數工作。我已經做了幾乎所有的事情在這篇文章中多聽。

哦,你可以嘗試這裏列出的一些解決方案:

Phonegap Camera API Cannot Read Property data url of undefined