二、实现思路:利用canvas绘制图像的功能,绘制图像某一帧,这里绘制了第一帧,很简单就实现了。
三、代码:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>capture screen</title> <style type="text/css"> video,#container{width: 300px;height: 200px;} #container>img{width: 100%;} </style> </head> <body> <video id="video" controls="controls"> <source src="video/video_test.mp4"> </video> <div id="container"></div> <script type="text/javascript"> (function() { var video, container; var scale = 0.8; var initialize = function() { container = document.getelementbyid("container"); video = document.getelementbyid("video"); video.addeventlistener('loadeddata', captureimage); }; var captureimage = function() { var canvas = document.createelement("canvas"); canvas.width = video.videowidth * scale; canvas.height = video.videoheight * scale; canvas.getcontext('2d').drawimage(video, 0, 0, canvas.width, canvas.height); var img = document.createelement("img"); img.src = canvas.todataurl("image/png");//转换成base64图片,地址拿出来就可以直接使用 container.appendchild(img); }; initialize(); })(); </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。