Call the local camera to take pictures (H5 and canvas), and the camera to take pictures of the h5 canvas

Source: Internet
Author: User

Call the local camera to take pictures (H5 and canvas), and the camera to take pictures of the h5 canvas

Implementation of calling the local camera photo function for H5 and canvas

1. Code Implementation (html part))

<Input type = "button" title = "enable camera" value = "enable camera" onclick = "getMedia ();"/> <br/>

<Video height = "120px" autoplay = "autoplay"> </video>

<Input type = "button" title = "" value = "" 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>

 

2. js part

<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. URLs | window. msURL;
Var exArray = []; // The source ID of the storage device.
MediaStreamTrack. getSources (function (sourceInfos ){
For (var I = 0; I! = SourceInfos. length; ++ I ){
Var sourceInfo = sourceInfos [I];
// Audio and video will be traversed here, so we need to differentiate them
If (sourceInfo. kind === 'video '){
ExArray. push (sourceInfo. id );
}
}
});
Function getMedia (){
If (navigator. getUserMedia ){
Navigator. getUserMedia ({
'Video ':{
'Optional ':[{
'Sourceid': exArray [1] // 0 indicates the front camera and 1 indicates the rear camera.
}]
},
'Audio': true
}, SuccessFunc, errorFunc); // success is a callback function that is successfully obtained.
}
Else {
Alert ('native device media streaming (getUserMedia) not supported in this browser .');
}
}
Function successFunc (stream ){
// Alert ('succeed to get media! ');
If (video. Audio srcobject! = Undefined ){
// In Firefox, video. javassrcobject is initially null, rather than undefined. We can use this to detect Firefox's support.
Video. Audio srcobject = 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 );
}
// Draw the video frame to the Canvas object. The Canvas switches frames every 60 ms to form a visual video effect.
Function drawVideoAtCanvas (video, context ){
Window. setInterval (function (){
Context. drawImage (video, 0, 0, 90, 120 );
}, 60 );
}
// Obtain the 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;
}
// Triggered during vedio playback. The vedio frame image is drawn to the canvas.
// Video. addEventListener ('play', function (){
// DrawVideoAtCanvas (video, context2 );
//}, False );
// Take a photo
Function getPhoto (){
Context1.drawImage (video, 0, 120,); // draws the area specified in the video object to the area specified on the canvas for photographing.
}
// Video
Function getVedio (){
DrawVideoAtCanvas (video, context2 );
}
</Script>

3. Implementation

4. browser Section

Camera method navigator is enabled for H5 photos. getUserMedia: tests are applicable to Firefox, local calls, and browser calls that meet the https protocol. The local localhost and 127.0.0.1 can be accessed, but are published to the project and accessed through other pages, the https protocol needs to be configured for tomcat, but the security is low. Therefore, it is only used for reference

 

Related Article

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.