使用Phonegap拍摄照片

时间:2014-08-26 10:05:14

标签: 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="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>

请告诉我哪里弄错了。

这是我的相机插件

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

提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试使用Phonegap自带的相机插件而非自定义插件。

您可以在此处查看文档:{​​{3}}

另外,在引用phonegap.js时,您不必使用js / phonegap.js。你可以参考它 SRC =&#34; phonegap.js&#34;

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

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