2012-10-01 42 views
0

我在煎茶觸摸2.開發一個應用程序,我想在這 頁面有一個默認的圖像,並低於一個按鈕這樣的方式來設計的頁面。通過點擊該按鈕,相機應該打開(設備主要是iPad和iPhone),捕捉圖像後可以看到它存儲在設備中名爲「捕獲」的文件夾中。然後捕獲的圖​​像應該替換該默認圖像。如何使用phonegap在sencha觸摸應用程序中啓用相機功能?

我想使用PhoneGap的強制。我已經看到了PhoneGap API的相機,但我不明白如何正確使用它。我使用Mac和Xcode進行開發。

+0

嗨!你有沒有嘗試從在線教程/文檔的例子? http://docs.phonegap.com/en/1.5.0/phonegap_camera_camera.md.html#Camera(初完整的例子) – Littm

+0

嘿我試圖該代碼。但它在html按鈕中工作。但在我的應用程序中,我使用模型視圖控制器方法使用js文件創建按鈕。這是一個sencha觸摸應用程序。請建議我如何實施? 感謝 – Fabre

+0

您可以加入您使用在您的文章創建按鈕的js代碼? – Littm

回答

0

在我的應用程序煎茶觸摸2和PhoneGap的1.4 在控制器 在起飛照片按鈕處理

onTakePhotoButton: function(){ 
     // Retrieve image file location from specified source 
     navigator.camera.getPicture(uploadPhoto, function (message) { 
      alert('Get picture failed'); 
     }, { 
      quality: 50, 
      destinationType: navigator.camera.DestinationType.FILE_URI, 
      sourceType: navigator.camera.PictureSourceType.CAMERA //or PHOTOLIBRARY 
     } 
     ); 
     function uploadPhoto(imageURI) { 
      var options = new FileUploadOptions(); 
      options.fileKey = "file"; 
      var imagefilename = Number(new Date()) + ".jpg"; 
      options.fileName = imagefilename; 
      options.mimeType = "image/jpeg"; 
      options.chunkedMode = false; 
      var params = new Object(); 
      params.image = imagefilename; 
      options.params = params; 
      var ft = new FileTransfer(); 
      ft.upload(imageURI,your_request_upload_url_on_server, win, fail, options); 
     } 

     function win(r) { 
      console.log("Code = " + r.responseCode); 
      console.log("Response = " + r.response); 
      console.log("Sent = " + r.bytesSent); 
      var json_obj = Ext.decode(r.response);//remote server funciton upload return json type 
      if(json_obj!=null && json_obj.response.image_name!=null){ 
       console.log(json_obj.response.image_name); 
       imageDisplay.setSrc(your_image_root_tmp+json_obj.response.image_name+'?dc='+Number(new Date())); 
      }else{ 
       Ext.Msg.alert('Errors', "The server response failure!"); 
      }    

     } 

     function fail(error) { 
      alert("An error has occurred: Code = " + error.code); 
     } 
} 

你可以參考這裏更詳細http://zacvineyard.com/blog/2011/03/upload-a-file-to-a-remote-server-with-phonegap

+0

我試過你的代碼,但它不工作。它顯示空白頁面。我認爲問題是在同一個js頁面中聲明幾個函數。 「navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:100});」函數工作正常,但「onPhotoDataSuccess」函數未被調用。同樣的事情發生在你的代碼中也提出了一些替代方案.. 謝謝.... fabre – Fabre

+0

嘗試getPicture函數,並帶有回調uploadPhoto函數。帶回調獲取功能和失敗功能的uploadPhoto功能。 –

0

capturePhoto:函數(){

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

     function onPhotoDataSuccess(imageData) { 
     // Uncomment to view the base64 encoded image data 
     // console.log(imageData); 

     // Get image handle 
     // 
     var smallImage = document.getElementById('userLogo'); 

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

PLZ驗證...

相關問題