0

我正在使用 phonegap 版本 3。

我安装了 cordova 和 phonegap,但由于某种原因,我的控制台显示 phonegap.js 和 cordova.js 无法加载。它们也不在我的 js 文件夹中,所以我觉得这很奇怪,但是 phonegap 一直对我有用,直到今天我尝试使用他们的相机 API 功能。我为相机运行插件安装命令并将其添加到我的 config.xml 文件中:

<feature name="Camera">
    <param name="ios-package" value="CDVCamera" />
</feature>

但是相机在 iOS 模拟器中不起作用,在网络浏览器上也不起作用。在 Web 控制台中,我收到了这些错误。

Failed to load resource: The requested URL was not found on this server: file:///Users/thomas/dev/myapp/www/cordova.js

当我单击任何按钮时,我都会收到错误消息

TypeError: 'undefined' is not an object (evaluating 'navigator.camera.getPicture')

这是我的代码:(直接从 phonegap 网站复制)

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

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

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

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready",onDeviceReady,false);

    // device APIs are available
    //
    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: 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: 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>
4

3 回答 3

0

我认为它缺少 www 文件夹正确的结构。现在我不在办公室检查姓名,但是:

<root_folder_name> / wwww
               index.html
                     /camera
                             index.html 
                  /plugins
                         /<camera plugin name>
                                               /test
                  /platform
                       /ios
                           /<projectname>.xcodeproj

或您应该拥有的类似结构。无论如何,从: 将和文件夹与另一个文件夹plugins / <camerapluginmane> /test/ 复制到您的根文件夹。index.htmlcameraindex.htmlwww

还需要 amain.cssmain.jsacordova-incl.js所有文件都在 test 文件夹中。

只需仔细检查 /test/index.html 源并在 www 根文件夹中提供该结构和文件。

明天或星期一我可以在办公室编辑这个答案并更正名字。

您的错误:“TypeError: 'undefined' is not an object (evalating 'navigator.camera.getPicture'” 它位于 web 部件 (www 结构) 并且代码调用尚未转到 ios 文件 (.h .m) !

我希望它有帮助!

于 2013-08-23T17:29:28.807 回答
0

我知道现在回答这个问题为时已晚,但一个可行的例子还不算晚。下面的代码对我有用。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

  <title>Camera Demo</title>
  <link rel="stylesheet" type="text/css" href="css/style.css" />
  <script src="js/jquery-1.11.3.min.js"></script>

  <script type="text/javascript">

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value
    // Wait for Cordova to load
    document.addEventListener('deviceready', onDeviceReady, false);

    // Cordova is ready
    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("IMAGE PATH: "+imageURI);
      alert("Image Url : "+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: 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: 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>  
<header class="header">
<div class="header-div">
  <div class="header-single">
    <span id="title">Details </span>
  </div>
</div>
</header>
<div class="clear">
</div>
<content class="content-style">
 <h2> Camera Demo</h2>

  <button class="demo-btn-style" onclick="capturePhoto();">Capture Photo</button> <br>
  <button class="demo-btn-style" onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
  <button class="demo-btn-style" onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
  <button class="demo-btn-style" onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
  Small image: <br>
  <img style="display:none;width:100px;height:100px;" id="smallImage" src="" /> <br>
  Large image: <br>
  <img style="display:none;" id="largeImage" src="" /> <br>

</content>

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>

</boyd>
</html>
于 2016-03-03T07:02:54.887 回答
0

在终端中,在我的应用程序的主文件夹中,我运行了cordova plugin add cordova-plugin-camera. 我已经使用 PhoneGap 将它安装在我的浏览器上,但显然我需要再次执行此操作才能为 XCode 创建的 iOS 版本安装它。

Plugin "cordova-plugin-camera" already installed on browser. Installing "cordova-plugin-camera" for ios

这会将 .h 和 .m 文件添加到我的平台->ios->APPNAME->plugins 文件夹中。

现在一切正常!

于 2016-09-29T02:40:05.217 回答