Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the specific camera on mobile phone through html5

When I use html5 'getUserMedia' API to access acamera on the android(4.0) phone, it comes out "front camera", but I want to open "back camera". Sample code:

<!DOCTYPE html>
<html>
 <head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width">
    <title>Html5 Mobile Carema</title>
    <script src="js/jquery.js"></script>
    <script>
       $(document).ready(init);

       function init() {
       try {
        window.URL = window.URL || window.webkitURL || window.msURL
                || window.oURL;
        navigator.getUserMedia = navigator.getUserMedia
                || navigator.webkitGetUserMedia
                || navigator.mozGetUserMedia ||         navigator.msGetUserMedia;


        navigator.getUserMedia({
            video : true
        }, successsCallback, errorCallback);
    } catch (err) {
        // Tries it with old spec of string syntax
        navigator.getUserMedia('video', successsCallback, errorCallback);
    }
    $(":button").click(function() {
        slap();
    });
}
function slap() {
    var video = $("#myVideo")[0];
    var canvas = capture(video);
    $("#result").empty();
    $("#result").append(canvas);
    //alert();
    var imgData = canvas.toDataURL('image/png;base64,');
    //var imgData = canvas.toDataURL("image/png");
    var imgData = imgData.substring(22);
    //blb = dataURItoBlob(imgData);
    //sendMsg(blb);
}
function errorCallback(err) {

}
function successsCallback(stream) {
    $("#myVideo").attr("src", window.webkitURL.createObjectURL(stream));
}
function capture(video) {
    var canvas = document.createElement('canvas');
    var width = video.videoWidth;
    var height = video.videoHeight;
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;
    var context = canvas.getContext('2d');
    context.drawImage(video, 0, 0, 160, 120);
    return canvas;
}

  </script>
</head>
<body>
    <video id="myVideo" autoplay="autoplay"></video>
    <br> <input type="button" value="capture" />
<br><div id="result" style="width: 145px"></div>
<div>
<p id="resultMsg" style="color: red"></p>
<p id="decodeTime" style="color: green"></p>
</div>

</body>
</html>

I don't know how to access specific camera on android phone, anyone who knows? thanks

like image 984
user2412803 Avatar asked Dec 13 '25 14:12

user2412803


1 Answers

There is now the ability to specify a camera in the latest specification with the facingMode property: http://www.w3.org/TR/mediacapture-streams/#idl-def-VideoFacingModeEnum

This property is an optional part of the MediaStreamConstraints object that is the first argument of the getUserMedia method.

Here's a simplified example from the spec:

var supports = navigator.mediaDevices.getSupportedConstraints();
if (!supports["facingMode"]) {
  // Handle lack of browser support if necessary
}
var gotten = navigator.mediaDevices.getUserMedia({
  video: {
    facingMode: {exact: "environment"}
  }
});

The value environment means the back camera of the device. Other values are user, left and right.

Note that support for this varies depending on the browser/browser version.

like image 124
tagawa Avatar answered Dec 15 '25 05:12

tagawa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!