H5 call phone front and rear camera, take pictures

Source: Internet
Author: User

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Pacam.aspx.cs" inherits= "CRM3.cam.pacam"%>

<! DOCTYPE html>

<TITLE>HTML5 Getusermedia demo</title>
<meta name= "viewport" content= "Width=device-width, initial-scale=1.0, maximum-scale=1.0 "/>

<body>
<input type=" button "title=" Turn on camera "value=" Turn on front camera " onclick= "Getmedia (0);"/><input type= "button" title= "Turn on camera" value= "Turn on rear camera" onclick= "Getmedia (1);"/><br />
<video height= "120px" autoplay= "AutoPlay" ></video> <input type= "button" Title= "Take photos" value= "Take photos" onclick= "Getphoto ();"/><br/>
<canvas id= "canvas1" height= "120px" ></ Canvas> <input type= "button" title= "video" value= "video" onclick= "Getvedio ();"/><br/>
<canvas id= "Canvas2" height= "120px" ></CANVAS>

<script type= "Text/javascript";
var Video = Document.queryselector (' video ');
Var audio, audiotype;

var canvas1 = document.getElementById (' canvas1 ');
var context1 = Canvas1.getcontext (' 2d ');

var canvas2 = document.getElementById (' Canvas2 ');
var context2 = Canvas2.getcontext (' 2d ');

Navigator.getusermedia = Navigator.getusermedia | | Navigator.webkitgetusermedia | | Navigator.mozgetusermedia | | Navigator.msgetusermedia;
Window. URL = window. URL | | Window.webkiturl | | Window.mozurl | | Window.msurl;

var exarray = []; Storage device Source ID
Mediastreamtrack.getsources (function (Sourceinfos) {
for (var i = 0; I! = Sourceinfos.length; ++i) {
var sourceinfo = sourceinfos[i];
This will traverse the audio,video, so make a distinction
if (sourceinfo.kind = = = ' Video ') {
Exarray.push (sourceinfo.id);
}
}
});

function Getmedia (a) {
if (Navigator.getusermedia) {
Navigator.getusermedia ({
' Video ': {
' Optional ': [{
' SourceID ': exarray[a]//0 for front camera, 1 for rear
}]
},
' Audio ': true
}, Successfunc, Errorfunc); Success is the callback function that gets the success
}
else {
Alert (' Native device media streaming (Getusermedia) not supported in this browser. ');
}
}

function Successfunc (stream) {
Alert (' Succeed to get media! ');
if (video.mozsrcobject!== undefined) {
In Firefox, Video.mozsrcobject is initially null, not undefined, and we can rely on this to detect Firefox support.
Video.mozsrcobject = stream;
}
else {
video.src = window. URL && window. Url.createobjecturl (stream) | | Stream
}

Video.play ();

Audio
Audio = new audio ();
Audiotype = Getaudiotype (audio);
if (Audiotype) {
AUDIO.SRC = ' Polaroid. ' + Audiotype;
Audio.play ();
}
}
function Errorfunc (e) {
Alert (' error! ' + e);
}


Draws a video frame onto a canvas object, and canvas switches frames every 60ms to create a visual visual effect
function Drawvideoatcanvas (video, context) {
Window.setinterval (function () {
Context.drawimage (video, 0, 0, 90, 120);
}, 60);
}

Get audio format
function Getaudiotype (Element) {
if (Element.canplaytype) {
if (Element.canplaytype (' Audio/mp4; codecs= "mp4a.40.5" ')!== ') {
Return (' AAC ');
} else if (Element.canplaytype (' Audio/ogg; codecs= "Vorbis" ')!== ') {
Return ("Ogg");
}
}
return false;
}

Vedio trigger when playing, draw vedio frame image to Canvas
Video.addeventlistener (' Play ', function () {
Drawvideoatcanvas (video, CONTEXT2);
}, False);

Photo
function Getphoto () {
Context1.drawimage (video, 0, 0, 90, 120); Draws the specified area of the video object to the specified area on the canvas for taking pictures.
}

Video
function Getvedio () {
Drawvideoatcanvas (video, CONTEXT2);
}

</script>
</body>

H5 call the camera before and after the phone, take pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.