Cordova浏览器平台相机无法正常工作

时间:2015-01-02 22:32:11

标签: cordova cordova-plugins

我有一个启用了平台浏览器的Cordova应用程序。我想在Chrome中使用相机,但是调用相机根本不会产生任何反馈。它就像我的Android设备上的魅力一样。

我通过这个命令启动: cordova run browser

Chrome按照预期开启了一个完美的控制台日志,告诉我Cordova设置了相机代理:

 The key "target-densitydpi" is not supported.
 cordova.js:851 adding proxy for Camera
 index.js:47 Received Event: deviceready

我正在使用https://github.com/apache/cordova-plugin-camera/blob/master/doc/index.md中的示例。

当我在以下示例中单击我的链接时,除了日志行“capture”之外没有任何其他操作。没有错误,没有任何错误。它甚至没有要求我允许访问我的相机。我在HTML5页面上测试了我的相机,Chrome可以很好地访问它。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </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>
            <img id="myImage"/>
            <a href="javascript: capture()">Camera example</a>
        </div>
        <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>

    <script>
        function capture()
        {
            console.log("capture")
        navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
            destinationType: Camera.DestinationType.DATA_URL
        });
        }

        function onSuccess(imageData) {
            var image = document.getElementById('myImage');
            image.src = "data:image/jpeg;base64," + imageData;
        }

        function onFail(message) {
            alert('Failed because: ' + message);
        }
    </script>
    </body>
</html>

index.js文件是Cordova生成的文件。

cordova -version =&gt; 4.0.0

cordova plugins =&gt; org.apache.cordova.camera 0.3.4“相机”

如果有人可以指导我完成这项工作,我会非常感激。

提前致谢。

/马丁

1 个答案:

答案 0 :(得分:3)

  • 这是本地文件网络Chrome的限制,您无法访问相机/麦克风。解决方案是使用本地http服务器,然后从localhost://而不是file://

  • 运行
  • 无论如何,您最好的解决方案是使用标志启动chrome: --allow存取从-文件

或者你可以尝试mozilla,它的工作原理:)

问候!