在Cordova应用程序中使用相机拍照

3
我正在继续学习cordova和javascript(我需要它)。 我试图创建一个非常小的应用程序,使用相机。 为此,我采用了以下提供的代码: [http://cordova.apache.org/docs/en/2.5.0/cordova_camera_camera.md.html#Camera] 我正在尝试在netbeans中的cordova项目中调整此代码。
我得到了下面的代码。 我提供了我的index.html和index.js。 当我在模拟器中测试时,出现问题。 当我单击按钮时,没有任何反应,没有错误消息,显然它不会使用相机拍照。 在capturePhoto方法中似乎有一些问题,因为在netbeans中我收到了一个警告(未声明全局变量destinationType)。 你能帮我吗?
index.html
<head>     
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    <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">
    <title>Hello World</title>
    <link rel="stylesheet" type="text/css" href="css/index.css">
</head>
<body>
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
    </div>

    <button id="myBtn">Capture Photo</button> <br>

    <img style="display:none;width:80px;height:80px;" id="smallImage" src="" />
    <img style="display:none;" id="largeImage" src="" />

    <script>
        document.getElementById("myBtn").addEventListener("click", capturePhoto());
    </script> 
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
</body>   

index.js

var app = {
pictureSource: "", destinationType: "",
// 应用程序初始化 initialize: function() { this.bindEvents(); },
// 绑定启动时需要的所有事件,通常包括: // 'load'、'deviceready'、'offline' 和 'online'。 bindEvents: function() { document.addEventListener('deviceready', this.onDeviceReady, false); },
// deviceready事件处理函数
// 在事件中,'this'指向本事件。为了调用'receivedEvent'函数, // 必须显式调用'app.receivedEvent(...);' onDeviceReady: function() { app.receivedEvent('deviceready'); app.pictureSource = navigator.camera.PictureSourceType; app.destinationType = navigator.camera.DestinationType; },
// 更新DOM元素以响应接收事件 receivedEvent: function(id) { var parentElement = document.getElementById(id); var listeningElement = parentElement.querySelector('.listening'); var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;'); receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id); },
// 当图片成功获取时调用 onPhotoDataSuccess: function(imageData) { var smallImage = document.getElementById('smallImage'); smallImage.style.display = 'block'; smallImage.src = "data:image/jpeg;base64," + imageData; },
// 按钮将调用此函数 capturePhoto: function() { navigator.camera.getPicture(app.onPhotoDataSuccess, app.onFail, { quality: 100, destinationType: app.destinationType.DATA_URL }); },
// 当出现错误时调用 onFail: function(message) { alert('Failed because: ' + message); }
};
app.initialize();

感谢你的帮助。

1个回答

0
在调用navigator.camera.getPicture时,您需要使用destinationType: Camera.DestinationType.DATA_URL而不是destinationType: destinationType.DATA_URL。您还应该指定sourceType和mediaType,因此您可能想要像这样做:
function capturePhoto() {
    var options = {
        quality: 100,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.CAMERA,
        mediaType: Camera.MediaType.CAMERA,
        encodingType: Camera.EncodingType.JPEG,
        saveToPhotoAlbum: true
    };
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, options);
}

现在我收到了一个警告,它说“全局变量相机未声明”。我正在尝试修改我的代码以找到解决方案,有许多我必须理解的怪癖... - grome55
有几件事情需要注意:Cordova 不包含任何插件,因此您需要添加您想要使用的插件。您是否已经添加了相机插件?在命令行中输入 "cordova plugin add cordova-plugin-camera"。另外,在重新阅读您的帖子时,我发现您正在尝试从模拟器中使用相机。据我所知,相机(以及大多数其他设备硬件)在模拟器中不可用。 - Mike Dailor
嗨,Mike,是的,我添加了插件。我认为这样可以。如果我使用cordova文档中提供的javascript代码(没有任何更改),即使在模拟器中也可以工作。在这种情况下,模拟器会要求我使用计算机的网络摄像头,应用程序也可以正常工作。但是当我尝试将此代码(请参见上面的链接)适应于cordova netbeans项目时,问题就出现了。在这种情况下,我将我的代码放在index.js中,并进行了许多调整。我认为这更多是由于JavaScript技能不足而不是特定的cordova问题误解,但我不确定... - grome55
好的,经过两天的研究,我认为我找到了问题的源头。但是我不知道如何解决它。我提供的代码已经稍作更改,但我的问题仍然存在。我的 JavaScript 代码有问题。 当我模拟我的应用程序时,我遇到了一个错误。 Uncaught TypeError: Cannot read property 'PictureSourceType' of undefined (17:44:43:779 | error, javascript) at app.onDeviceReady (www/js/index.js:32:41) at (anonymous function) (file:///android_asset/www/cordova.js:227:34) 就像他找不到 cordova.js 一样,但是它确实在那里。相机插件已添加。 - grome55
如果有人想帮助我理解我的错误... git@github.com:jermartin/myFirstMobileApp.git 有2个分支,相同的代码... 一个是为netbeans设置的 一个是为sublimetext设置的 感谢您的帮助 - grome55

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接