cordova - Phonegap Cordova 3.0.0 navigator.camera 未定义

标签 cordova cordova-3

我第一次尝试使用 Cordova 原生插件。我从相机和文档中提供的示例代码开始。然而,这失败了,navigator.camera未定义。

我已经包含了下面的代码。

<div data-role="page" id="CameraPage">
<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) {
  alert("Photo Data Success");
  // 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;
}

// A button will call this function
//
function capturePhoto() {
  alert("function is called"); 
  if(_.isUndefined(navigator.camera)){
    alert("Camera is not defined");
  }else{
    alert("WTF?!");
  }
  // Take picture using device camera and retrieve image as base64-encoded string
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
    destinationType: destinationType.DATA_URL });
}

// Called if something bad happens.
//
function onFail(message) {
  alert('Failed because: ' + message);
}

</script>

<button onclick="capturePhoto();">Capture Photo</button> <br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />

我根据 CLI 说明安装了相机插件
cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git

我还添加了cordova.js 文件。

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

最佳答案

你在哪里测试你的代码?

我不知道你是否还需要anwser。但我认为它可以帮助某人。我运行你的代码,它 100% 工作。
我已经在 Android 模拟器中使用模拟相机进行了测试。我认为您忘记了使用权限。

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

我在这里做了一个例子 https://github.com/paulorbpacheco/CameraTest-Cordova.git

我希望这可以帮助你...

问候

关于cordova - Phonegap Cordova 3.0.0 navigator.camera 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18690388/

相关文章:

ios - Cordova 3.1.0 : Plugin not found or is not a CDVPlugin

android - Google Play 商店发布问题

ios - Phonegap 3.4 文件传输错误 (iOS)

ios - 在 Xcode - Cordova App 中显示 console.log

ios - Phonegap 2.3.0 Xcode 编译错误

android - Cordova - 在收到通知后在后台更改 Android 设备上的徽章编号

ios - camera.getPicture 在选择图库中的 cordova ios 中滞后了太多时间

cordova - ionic 2 InAppBrowser url 未打开

javascript - 无法让 phonegap device.platform 工作

android - Cordova 与服务器的连接不成功