Javascript 使用Phonegap拍摄照片

Javascript 使用Phonegap拍摄照片,javascript,cordova,camera,Javascript,Cordova,Camera,我是Phonegap的新手。我想用Phonegap拍摄照片。我从文档中尝试了以下代码,但相机没有启动。请告诉我我做错了什么?我的代码是 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="tex

我是Phonegap的新手。我想用Phonegap拍摄照片。我从文档中尝试了以下代码,但相机没有启动。请告诉我我做错了什么?我的代码是

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="js/phonegap.js"></script>
<script src="js/jquery.js"></script>
<script type="text/javascript">
var pictureSource; // picture source
var destinationType; // sets the format of returned value 
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    alert("OnDeviceReady...");
    pictureSource = navigator.camera.PictureSourceType;
    destinationType = navigator.camera.DestinationType;
}

function onPhotoDataSuccess(imageData) {
    var smallImage = document.getElementById('smallImage');

    smallImage.style.display = 'block';

    smallImage.src = "data:image/jpeg;base64," + imageData;
}

function onPhotoURISuccess(imageURI) {
    var largeImage = document.getElementById('largeImage');

    largeImage.style.display = 'block';

    largeImage.src = imageURI;
}

function capturePhoto() {
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
        quality : 50,
        destinationType : destinationType.DATA_URL
    });
}

function capturePhotoEdit() {
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
        quality : 20,
        allowEdit : true,
        destinationType : destinationType.DATA_URL
    });
}

function getPhoto(source) {
    navigator.camera.getPicture(onPhotoURISuccess, onFail, {
        quality : 50,
        destinationType : destinationType.FILE_URI,
        sourceType : source
    });
}

function onFail(message) {
    alert('Failed because: ' + message);
}
window.onload = onDeviceReady;
</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>

在此处插入标题
var pictureSource;//图像源
var destinationType;//设置返回值的格式
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
函数ondevicerady(){
警报(“OnDeviceReady…”);
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.destinationType;
}
函数onPhotoDataSuccess(imageData){
var smallImage=document.getElementById('smallImage');
smallImage.style.display='block';
smallImage.src=“数据:图像/jpeg;base64,”+imageData;
}
函数onPhotoURISuccess(imageURI){
var largeImage=document.getElementById('largeImage');
largeImage.style.display='block';
largeImage.src=imageURI;
}
函数capturePhoto(){
navigator.camera.getPicture(onPhotoDataSuccess、onFail、{
质量:50,
destinationType:destinationType.DATA\u URL
});
}
函数capturePhotoEdit(){
navigator.camera.getPicture(onPhotoDataSuccess、onFail、{
质量:20,
允许:是的,
destinationType:destinationType.DATA\u URL
});
}
函数getPhoto(源代码){
navigator.camera.getPicture(onPhotoURISuccess、onFail、{
质量:50,
destinationType:destinationType.FILE\u URI,
sourceType:source
});
}
函数onFail(消息){
警报('失败原因:'+消息);
}
window.onload=ondevicerady;
拍摄照片

捕获可编辑照片
从…起 照片库
从…起 相册
请告诉我哪里出错了

这是我的相机插件

<plugins>
    <plugin
        name="Storage"
        value="org.apache.cordova.Storage" />
    <plugin
        name="Camera"
        value="com.foregroundcameraplugin.ForegroundCameraLauncher" />
</plugins>


提前谢谢。

尝试使用Phonegap自己的摄像头插件,而不是定制的

您可以在此处查看文档:

另外,在引用phonegap.js时,您不必放置js/phonegap.js。你可以这样说 src=“phonegap.js”

如果您使用的是Phonegap构建,这是插入插件的正确方法:

<gap:plugin name="org.apache.cordova.camera"/>


您是否检查了错误控制台?CLI使用的console.log文件或带有Safari或Chrome的远程调试都会告诉您出了什么问题。另外,您是否记得添加摄像头插件?我在控制台中没有得到任何信息。请检查相机插件的编辑代码。哇-为了将来的参考-当你说你正在使用相机而你没有使用默认的相机插件时,你真的应该弄清楚这一点。该插件的URL是什么,我们可以查看文档?Hello@RaymondCamden。。因为我是phonegap的初学者,所以我没有遇到这个问题。你能给我照相机的工作代码吗。?这样我才能解决我的问题。谢谢。嗨,谢谢你的回答。我正在使用Eclipse工具。我应该在哪里添加插件行?您需要设置应用程序并通过Phonegap CLI(命令行界面)添加插件。您可以在此处阅读有关CLI的更多信息:谢谢。但现在我得到了一个错误:“在使用CLI添加平台时列出Android目标时出错”。我也跟着做了,但我还是遇到了同样的问题。你在我上面发布的链接上都做了吗?由于我正在使用Phonegap构建,我将无法帮助您解决此问题,抱歉。您好…我的系统出现问题。它需要权限才能在该特定目录上执行任何任务。更改目录后,问题得到解决。非常感谢您的回复。