2013-04-03 49 views
3

我知道這裏有很多關於Phonegap和使用攝像頭功能的問題。 我試圖解決這個問題,但仍然沒有解決方案。Phonegap攝像頭功能

設備是Nexus 7,相機不會在構建應用程序後啓動。 下面是代碼:

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

var pictureSource; // picture source 
var destinationType; // sets the format of returned value 

// Wait for PhoneGap to connect with the device 
// 
document.addEventListener("deviceready",onDeviceReady,false); 

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

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

// 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> 

與三星Galaxy S2嘗試了......再一次什麼都沒有發生! 我不知道這個問題!? 這裏是代碼:

androidmainfest.xml權限:

<uses-sdk android:minSdkVersion="14" /> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.VIBRATE" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.RECEIVE_SMS" /> 
<uses-permission android:name="android.permission.RECORD_AUDIO" /> 
<uses-permission android:name="android.permission.RECORD_VIDEO"/> 
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 
<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.BROADCAST_STICKY" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

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

這裏從HTML文件中的代碼:

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

var pictureSource; // picture source 
var destinationType; // sets the format of returned value 

// Wait for PhoneGap to connect with the device 
// 
document.addEventListener("deviceready",onDeviceReady,false); 

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

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

// 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

您需要按照提到的,提供的選項文檔。例如:

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

Rereference: http://docs.phonegap.com/en/2.5.0/cordova_camera_camera.md.html#camera.getPicture

+0

我添加了這一行,但沒有奏效。那麼我需要在權限的情況下編輯manifest.xml嗎? – DehMotth

+0

您需要write_external_storage和攝像頭權限。 – wmfairuz

+0

但是當用在線服務構建應用程序時,沒有機會編輯mainfest.xml文件? – DehMotth

0

現在,它的工作原理。不知道這個問題,但它工作。 Here's代碼:

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

    <link rel="stylesheet" href="css/style.css" /> 

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

function capturePhoto(){ 
    navigator.camera.getPicture(uploadPhoto,null,{sourceType:1,quality:60}); 
} 

function uploadPhoto(data){ 
    // this is where you would send the image file to server 
    //output image to screen 
     cameraPic.src = "data:image/jpeg;base64," + data; 
    } 

navigator.notification.alert(message, alertCallback, [title], [buttonName]); 

navigator.notification.alert(
     'Your Photo has been uploaded', // message 
     okay,       // callback 
     'Photo Uploaded',    // title 
     'OK'       // buttonName 
    ); 

function uploadPhoto(data){ 
    // this is where you would send the image file to server 
     cameraPic.src = "data:image/jpeg;base64," + data; 
     // Successful upload to the server 
     navigator.notification.alert(
      'Your Photo has been uploaded', // message 
      okay,       // callback 
      'Photo Uploaded',    // title 
      'OK'       // buttonName 
     ); 
     // upload has failed Fail 
     /* 
     if (failedToUpload){ 
     navigator.notification.alert(
      'Your Photo has failed to upload', 
      failedDismissed, 
      'Photo Not Uploaded', 
      'OK' 
      ); 
     } 
     */ 
    } 
    function okay(){ 
     // Do something 
    } 

    </script> 
    </head> 
    <body> 
<div id="camera"> 
    <button class="camera-control" onclick="capturePhoto();">Capture Photo</button> 
    <div style="text-align:center;margin:20px;"> 
     <img id="cameraPic" src="" style="width:auto;height:120px;"></img> 
    </div> 
</div> 
    </body> 
</html> 
0

可能是你的手機內存不夠,這個問題其實不是關於PhoneGap的。這也是原生android應用程序的一個常見問題。它的出現是因爲當相機被觸發時,android活動會變爲背景(onStop狀態),等待相機拍攝照片。然後,GC會在相機動作結束前殺死活動以釋放記憶,當相機完成時,您的活動已經死亡。這就是爲什麼該應用程序重新啓動。

這裏是我的建議

1 .Replace克拉默插件,避免使用自定義插件啓動垃圾收集,(http://code.google.com/p/foreground-camera-plugin/http://code.google.com/p/foreground-gallery-插件/) 2.檢測內存不足,如果殺其他進程,主動釋放內存3.提高自己的生存能力,儘量避免在所選系統中釋放內存。

private void clearMemory(boolean killAll) 
{ 
    mklog("當前系統可用內存大小是:" + getAvailMemory(getApplicationContext())); 
    ActivityManager activityManger = (ActivityManager) this 
      .getSystemService(ACTIVITY_SERVICE); 
    List<ActivityManager.RunningAppProcessInfo> list = activityManger 
      .getRunningAppProcesses(); 
    if (list != null) 
     for (int i = 0; i < list.size(); i++) 
     { 
      ActivityManager.RunningAppProcessInfo apinfo = list.get(i); 

      System.out.println("pid   " + apinfo.pid); 
      System.out.println("processName    " 
        + apinfo.processName); 
      System.out 
        .println("importance   " + apinfo.importance); 
      String[] pkgList = apinfo.pkgList; 

      if (apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND 
        || (killAll && apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE)) 
      { 
       // Process.killProcess(apinfo.pid); 
       for (int j = 0; j < pkgList.length; j++) 
       { 
        activityManger.killBackgroundProcesses(pkgList[j]); 
        mklog("準備殺死進程:" + pkgList[j]); 
       } 
      } 

     } 

    mklog("清理之後 當前系統可用內存大小是:" + getAvailMemory(getApplicationContext())); 
} 

private long getAvailMemory(Context context) 
{ 
    ActivityManager am = (ActivityManager) context 
      .getSystemService(Context.ACTIVITY_SERVICE); 
    MemoryInfo mi = new MemoryInfo(); 
    am.getMemoryInfo(mi); 
    return mi.availMem/(1024 * 1024); 
} 
     public void mklog(String contentString) 
{ 
    Log.i("Web Console", contentString); 
}