This article example shares the specific code of Vue to realize the photo upload function on the PC for your reference. The specific content is as follows <!DOCTYPE html> <html> <head> <meta charset="UTF8"> </head> <body> <div id="contentHolder"> <video id="video" width="320" height="320" autoplay></video> <button id="camera">Take a photo</button> <canvas id="canvas" width="320" height="320"> </canvas> </div> <script type="text/javascript"> var video = document.getElementById('video'); var track; var Camera = document.getElementById('camera'); window.addEventListener("DOMContentLoaded", function(){ navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; if (navigator.getUserMedia) { navigator.getUserMedia({video:true}, function(stream) { track = stream.getTracks()[0]; // Use this to turn off the camera video.src = window.URL.createObjectURL(stream); video.onloadedmetadata = function(e) { video.play(); }; }, function(err) { alert(err.name); } ); } }); Camera.onclick = function(){ var canvas = document.getElementById('canvas'); var context2D = canvas.getContext("2d"); context2D.fillStyle = "#ffffff"; context2D.fillRect(0, 0, 320, 320); context2D.drawImage(video, 0, 0, 320, 320); var image_code = canvas.toDataURL("image/png"); // Base64 to be passed to the backend console.log(image_code) if (null != track) { track.stop(); //Close the camera} }; </script> </body> </html> The above code will call the camera when the web page is opened The code sent to the backend is a base64 code Below is the code I passed to the background using Vue var param = { file:image_code2 } var a = JSON.stringify(param); uploadimg(a).then((res) => { console.log(res); }); Below is my PHP background receiving code public function uploadImg($name="img",$path='img'){ $_POST = json_decode(file_get_contents('php://input'),true); $param = $_POST; $image_code = $param['file']; $img = str_replace('data:image/png;base64,', '', $image_code); //Get base64 code $img = str_replace(' ', '+', $img); $data = base64_decode($img); $name = time().".png"; $savepath = "./upload/".$name; //The location where the image is saved file_put_contents($savepath,$data); //Write the content to the file $this->ajaxReturn(array('status'=>'0','data'=>$savepath)); } If you want to click to trigger the camera to open, you can take the code from the camera and put it in a method. var Camera = document.getElementById('camera'); window.addEventListener("DOMContentLoaded", function(){ navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; }); function demo(){ if (navigator.getUserMedia) { navigator.getUserMedia({video:true}, function(stream) { track = stream.getTracks()[0]; // Use this to turn off the camera video.src = window.URL.createObjectURL(stream); video.onloadedmetadata = function(e) { video.play(); }; }, function(err) { alert(err.name); } ); } } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL uses the truncate command to quickly clear all tables in a database
>>: Alibaba Cloud Server Linux System Builds Tomcat to Deploy Web Project
About semantics Semantics is the study of the rel...
Precondition: content="width=750" <m...
Table of contents Preface: Step 1: Find the free ...
There are several ways I know of to set anchor pos...
Table of contents Starting from type judgment Str...
1. Basic grammar Copy code The code is as follows...
Table of contents Install sakila Index Scan Sort ...
The 2008.5.12 Wenchuan earthquake in Sichuan took...
This article example shares the specific code for...
Preview address: https://ovsexia.gitee.io/leftfix...
[Abstract] This article quickly builds a complete...
Result: html <canvas id="starfield"&...
1. Use frameset, frame and iframe to realize mult...
1. The effect diagram implemented in this article...
The operating environment of this tutorial: Windo...