繁体   English   中英

Cordova 带有使用摄像头的托管 Web 应用程序

[英]Cordova with hosted web app using the camera

我正在尝试使用cordova 构建一个应用程序,但不是在本地托管Web 应用程序,而是将其托管在服务器上。

所以这部分与白名单和允许导航配合得很好。

当我尝试使用相机时出现问题。 通过导航器访问相机似乎需要一些时间。

所以这是我的简单网络应用程序,只是为了了解它是如何工作的。

<!DOCTYPE html>
<head>
    <title>test</title>
    <script src="scripts/cordova.js"></script>
</head>

<body>
<script>

    (function() {

        // camera will be undefined
        var camera = navigator.camera;

    })();


</script>
</body>
</html>

但是当我把整个东西放在 setTimeout 中时,它是可以访问的。

<!DOCTYPE html>
<head>
    <title>test</title>
    <script src="scripts/cordova.js"></script>
</head>

<body>

<script>

    (function() {
        setTimeout(function() {

            // I can now use it. 
            var camera = navigator.camera;

        }, 10000);
    })();


</script>

</body>

</html>

这是我的 index.js 文件,它将重定向到我们服务器上托管的 Web 应用程序。 所以它在重定向之前等待设备准备好。

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.receivedEvent('deviceready');
        window.location.replace("http://10.0.0.36:8080/");
    },

    // Update DOM on a Received Event
    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);
    }
};

app.initialize();

我试图理解为什么?

谢谢,

在调用任何插件之前,您需要等待设备准备就绪。

document.addEventListener("deviceready", function(){
  var camera = navigator.camera;
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM