3
votes

I have a Cordova app with platform Browser enabled. I would like to use the camera in Chrome, but calling the camera yields no feedback at all. It works like a charm on my Android devide.

I launch through this command: cordova run browser

Chrome opens as expected with a perfectly fine console log telling me that Cordova setup a proxy for the camera:

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

I'm using the example from https://github.com/apache/cordova-plugin-camera/blob/master/doc/index.md.

When I click my link in the following example, nothing happens except the log line "capture". No errors, no nothing. It don't even ask my for permission to access my camera. I did test my camera on a HTML5 page and Chrome can access it fine.

<!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>

The index.js file is the one generated by Cordova.

cordova -version => 4.0.0

cordova plugins => org.apache.cordova.camera 0.3.4 "Camera"

If anyone can guide me through this, I would really appriciate it.

Thanks in advance.

/Martin

1

1 Answers

3
votes
  • That's a limitation of Chrome for local file webs, you can't access camera/mic. The solution is to use a local http server and then run from localhost:// instead of file://

  • Anyway, your best solution is to start chrome with the flag: --allow-access-from-files

Or you can try mozilla, where it works : )

Regards!