2017-09-29 33 views
0

我不能让我的按钮,点击并访问我的相机和其他功能。不能得到PhoneGap的访问IOS摄像头。按钮的问题

代码的我的HTML部分。我确实安装了所有必需的插件。我可以查看我创建的按钮。但点击时没有任何反应。

<div class="row"> 
    <div class="col-sm-2"> </div> 
    <div id="imgDiv" class="col-sm-5 col-xs-11 thumbnail"> 
     <img src="#" alt="Image" id="img" /> 
     <div class="text-center"> 
     <button type="button" class="btn btn-primary" id="btPhotoLib">Photo</button> 
     <button type="button" class="btn btn-primary active" id="btCamera">Camera</button> 
     <button type="button" class="btn btn-primary" id="btUpload">Upload</button> 
     </div>       
    </div>    
</div> 

我的JS文件:

var imgURI; 
var serverURL ="ajlnfioej/upload.php"; 

$(document).ready(function(){ 
    document.addEventListener('deviceready', getCameraReady, false);  
}); 

function getCameraReady(){ 
    $('#btCamera').on('click', function(e){ 
     options = {quality:50, destinationType: Camera.Destination.FILE_URI,  sourceType: pictureSource.CAMERA}; 

     navigator.camera.getPicture(photoSuccess, photoFail,[options]);  
     }); 

    $('#btPhotoLib').on('click', function(e){ 
    options = {quality:50, destinationType: Camera.Destination.FILE_URI, sourceType: pictureSource.PHOTOLIBRARY}; 
    navigator.camera.getPicture(photoSuccess, photoFail,[options]); 
}); 

$('#btUpload').on('click', function(e){ 
    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    options.fileName = imgURI.substr(imgURI.lastIndexOf('/')+1); 
    options.mimeType = "image/jpeg"; 

    var ft = new FileTransfer(); 
    var server = encodeURI(serverURL); 
    ft.upload(imgURI, server,uploadSuccess, uploadFail,options);   
}); 
} 


function photoSuccess(uri){ 
    $("#img").attr("src", uri); 
    $("#img").css("width": "100%", "height": "100%"); 
    imgURI = uri; 
} 

function cameraError(message){ 
    navigator.notification.alert("camera usage not supported on this device"); 
} 

function uploadSuccess(result){ 
navigator.camera.cleanup(); 
navigator.notification.alert("Number of bytes is : " + results.bytesSent); 
navigator.notification.alert("Http Response is : " +results.response); 


function uploadFail(){ 
    alert("Am error has occured: Code = " + error.code); 
} 

回答

0

我已经通过您的代码了,并已完成了工作。它已经在Android和iOS上测试过,但是我无法测试上传。

  • 我已经在你的JavaScript纠正了一些语法错误(有几个缺少大括号以及其他一些小事情)。

  • 当使用jQuery添加多个CSS标签你必须给它一个对象,看起来你只是错过了大括号。

  • 添加在photoFail函数中,因为它已被使用但未定义。

  • 为错误功能添加了错误参数。

  • Camera.getPicture选项参数期望的对象,但它被赋予一个数组。

  • Camera.Destination没有定义,应该是Camera.DestinationType

  • pictureSource没有定义,应该是Camera.PictureSourceType


我得到了最本从检查cordova-plugin-camera documentation。我会建议阅读。

这里是工作代码。您可以查看所有更改here

var imgURI; 
var serverURL ="ajlnfioej/upload.php"; 

$(document).ready(function(){ 
    document.addEventListener('deviceready', getCameraReady, false);  
}); 

function getCameraReady(){ 
    $('#btCamera').on('click', function(e){ 
     var options = {quality:50, destinationType: Camera.DestinationType.FILE_URI, sourceType: Camera.PictureSourceType.CAMERA}; 

     navigator.camera.getPicture(photoSuccess, photoFail, options);  
    }); 

    $('#btPhotoLib').on('click', function(e){ 
     var options = {quality:50, destinationType: Camera.DestinationType.FILE_URI, sourceType: Camera.PictureSourceType.PHOTOLIBRARY}; 
     navigator.camera.getPicture(photoSuccess, photoFail, options); 
    }); 

    $('#btUpload').on('click', function(e){ 
     var options = new FileUploadOptions(); 
     options.fileKey = "file"; 
     options.fileName = imgURI.substr(imgURI.lastIndexOf('/')+1); 
     options.mimeType = "image/jpeg"; 

     var ft = new FileTransfer(); 
     var server = encodeURI(serverURL); 
     ft.upload(imgURI, server,uploadSuccess, uploadFail,options);   
    }); 
} 


function photoSuccess(uri){ 
    $("#img").attr("src", uri); 
    $("#img").css({"width": "100%", "height": "100%"}); 
    imgURI = uri; 
} 

function cameraError(message){ 
    navigator.notification.alert("camera usage not supported on this device"); 
} 

function uploadSuccess(result){ 
    navigator.camera.cleanup(); 
    navigator.notification.alert("Number of bytes is : " + result.bytesSent); 
    navigator.notification.alert("Http Response is : " +result.response); 
} 

function uploadFail(error){ 
    alert("Am error has occured: Code = " + error.code); 
} 

function photoFail(error){ 
    alert("Am error has occured: Code = " + error.code); 
}