html5 call camera

1. page code

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<div id="contentHolder">
		<video id="video" width="320" height="320" autoplay="autoplay"></video>
		<button id="snap" style="display: none"></button>
		<canvas style="display: none" id="canvas" width="320" height="320"></canvas>
	</div>
</body>
</html>

2.js Code

window.addEventListener('DOMContentLoaded', function () {
        var canvas = document.getElementById('canvas'),
			context = canvas.getContext('2d'),
			video = document.getElementById('video'),
			videoObj = { video : true },
			errBack = function (err) {
				console.log('Video capture error : ' , err.code );
			};
        navigator.getUserMedia = navigator.getUserMedia ||  navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
        navigator.getUserMedia(videoObj, function (stream) {
			video.src = window.webkitURL.createObjectURL(stream);
			video.play();
		}, errBack);
	});

 

3. The device hasA case where a plurality of camerasHow to open other camera

var audios = [], videos=[]

function gotSources(sourceInfos) {
  for (var i = 0; i != sourceInfos.length; ++i) {
    var sourceInfo = sourceInfos[i];
     if (sourceInfo.kind === 'audio') {
         audios.push(sourceInfo.id);
    } else if (sourceInfo.kind === 'video') {
         videos.push(sourceInfo.id);
    } else {
       console.log('some other kind of source: ', sourceInfo);
    }   
  }
}

if (typeof MediaStreamTrack === 'undefined'){
  alert('This browser does not support MediaStreamTrack.\n\nTry Chrome Canary.');
} else {
  MediaStreamTrack.getSources(gotSources);
}

function successCallback(stream) {
  window.stream = stream; // make stream available to console
  videoElement.src = window.URL.createObjectURL(stream);
  videoElement.play();
}

function errorCallback(error){
  console.log("navigator.getUserMedia error: ", error);
}

function start(){
  if (!!window.stream) {
    videoElement.src = null;
    window.stream.stop();
  }
  var constraints = {
    audio: {
      optional: [{sourceId: audios[0]}]
    },
    video: {
      optional: [{sourceId: videos[0]}]
    }
  };
  navigator.getUserMedia(constraints, successCallback, errorCallback);
}

start();

 

 

Intelligent Recommendation

HTML5 call camera photo, video, recording

HTML5 call camera photo, video, recording Call the phone camera Call mobile phone camera video Call mobile phone recording...

Using the HTML5 API interface to call the camera camera function

I am thinking about some ways to call the camera under HTML5. Looked at the html5 documentation, mainly using html5getUserMedia()API, write an example to record the specific usage. Achieved the basicH...

Html5 call using the local camera to take pictures to upload pictures

Only a PC can test, the phone does not work  ...

html5 call phone camera (picture may limit multiple choice pc)

html5 own input file = "", html5 pure, and does not involve the js, can be achieved. code show as below:   capture, said the system can capture the default device, such as: camera- came...

Use html5 drawing technical matters call camera to take pictures;

Call cell phone camera to take pictures in mui framework can be used directly soundtrack of HTML5; The following is the HTML code The following is the JavaScript code   ...

More Recommendation

html5 webapp call system camera to take pictures in question

html5 webapp call system camera to take pictures in question I. Introduction: The reason why the webapp is now very popular, is now the main phone hardware configuration is a surplus, the gap with a w...

Use HTML5 + call cell phone camera and photo albums

Preface: Use HTML5 front-end time to do a WEB terminal APP, which uses a mobile phone to call the page H5 camera function, was also spent a lot of time to study. Ultimately adopted HTML5plus (HTML5 +)...

How to call the camera and photo album functions on the mobile browser of HTML5 page

Recently, I was working on a company's insurance information processing system project. We developed an HTML5 page accessed by a WeChat browser. There is a <input id = "input" type = &quo...

Use html5 to call the camera to take a picture. Can be used for face recognition

Not much to say, look at the code, some browsers may have adaptation problems, Google and Firefox have successfully run  ...

HTML5 uses the input control to directly call the system camera

Use the input: file tag to call the system's default camera, camera, and recording functions. In fact, there is a capture attribute that directly explains what function needs to be called: Capture mea...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top