Cordova PhoneGap:捕获未定义navigator.device的插件
我是PhoneGap的新手,正在尝试了解如何使用捕获插件。我用的是Cordova 3.5.0。我已成功运行以下命令:Cordova PhoneGap:捕获未定义navigator.device的插件,cordova,phonegap-plugins,cordova-plugins,Cordova,Phonegap Plugins,Cordova Plugins,我是PhoneGap的新手,正在尝试了解如何使用捕获插件。我用的是Cordova 3.5.0。我已成功运行以下命令: cordova plugin add org.apache.cordova.media-capture 我读过一些示例,其中HTML页面中包含cordova.js或phonegap.js。但是,Cordova创建的项目层次结构中不存在任何文件,因此我不知道如何包含它。我还了解到,这个文件是由Cordova在构建时自动注入的。至于包含任何JavaScript文件,我只包含我自己的
cordova plugin add org.apache.cordova.media-capture
我读过一些示例,其中HTML页面中包含cordova.js或phonegap.js。但是,Cordova创建的项目层次结构中不存在任何文件,因此我不知道如何包含它。我还了解到,这个文件是由Cordova在构建时自动注入的。至于包含任何JavaScript文件,我只包含我自己的JavaScript文件。在该JavaScript文件中,我有代码用于测试:
alert(navigator.device);
navigator.device.capture.captureImage(function(files) {
alert(files);
}, function(error) {
alert(error);
});
第一个警报显示navigator.device未定义。我正在android模拟器上运行此应用程序。要运行应用程序,我正在执行以下操作:
cordova emulate android
我想我需要包括或设置一些东西才能让它正常工作。非常感谢您的帮助。我想我需要在我的HTML页面中添加以下内容:
<script type="text/javascript" src="cordova.js"></script>
尽管该文件在项目层次结构中不存在,但它是在您构建应用程序时生成的。该文件不会自动注入HTML页面,但仍需要在需要时手动将其包括在内。我发现我需要在HTML页面中添加以下内容:
<script type="text/javascript" src="cordova.js"></script>
尽管该文件在项目层次结构中不存在,但它是在您构建应用程序时生成的。该文件不会自动注入HTML页面,但仍需要手动将其包含在需要的位置。欢迎使用Cordova 首先,我建议你检查一下,你是否用正确的方法安装了npm等。查看此处的教程-> cd~/桌面 cordova create media.example.com media cd媒体 cordova平台添加android cordova插件添加org.apache.cordova.camera而非媒体捕获 cordova插件add org.apache.cordova.console不使用警报,使用控制台更容易:- 科尔多瓦大厦 现在打开你的文件夹。应该有一个类似media/platforms/android/assets/www的目录,在这个目录中,您应该可以找到您的cordova.js和cordova_plugins.js 下一个问题:你的相机 在您按照我所说的安装完所有内容并在terminal等中检查$PATH变量echo$PATH之后,您可以在index.html中尝试以下代码来检查摄像头插件是否正常工作:
<!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 in-line 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 in-line 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>
欢迎来到科尔多瓦 首先,我建议你检查一下,你是否用正确的方法安装了npm等。查看此处的教程-> cd~/桌面 cordova create media.example.com media cd媒体 cordova平台添加android cordova插件添加org.apache.cordova.camera而非媒体捕获 cordova插件add org.apache.cordova.console不使用警报,使用控制台更容易:- 科尔多瓦大厦 现在打开你的文件夹。应该有一个类似media/platforms/android/assets/www的目录,在这个目录中,您应该可以找到您的cordova.js和cordova_plugins.js 下一个问题:你的相机 在您按照我所说的安装完所有内容并在terminal等中检查$PATH变量echo$PATH之后,您可以在index.html中尝试以下代码来检查摄像头插件是否正常工作:
<!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 in-line 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 in-line 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>
不,通常情况下,每次构建项目时,Cordova.JS都会自动包含在index.html中。我将编辑上面的答案,并在构建项目后立即添加标准索引的代码:-不,通常情况下,每次构建项目时,Cordova.JS都会自动包含在index.html中。我将编辑上面的答案,并在构建项目后立即添加标准索引的代码:-我感谢你的回答和解释。另外,我正在使用捕获插件而不是相机插件,这样我也可以捕获视频。根据我读到的内容,我可能会混合使用相机插件捕捉图片和捕捉视频。我感谢您的回复和解释。另外,我正在使用捕获插件而不是相机插件,这样我也可以捕获视频。根据我读到的内容,我可能会混合搭配使用相机插件捕捉图片和捕捉视频。