Click on Image to Upload Picture Html 5

  • Updated date Jul 31, 2020
  • 52k
  • 16

In this commodity, you will acquire how to capture Epitome from Webcam Video using HTML5 using JavaScript and Upload to Server C#.

Today I am going to evidence yous how to capture images from the webcam using JavaScript and HTML5 and upload on the server using a simple Ajax call using C#.

For your reference I have fastened zero file of the MVC projection with this article.

Beneath are the steps to perform this requirment.

  1. Access Webcam past taking permission from user
  2. One time you accept the success permission from user, then capture the image on Canvass
  3. Brand Ajax telephone call to send Canvas Image to Server with the help of Base64 data
  4. And upload Base64 data as an epitome on the server folder

Here one question can arrise, equally to why we use C# code to upload image, and why not JavaScript. Then the answer is JavaScript does not have permission to access File System for security reasons.

So permit's get started.

HTML

  1. <div course = "jumbotron"  way= "margin-pinnacle:20px;padding:20px;" >
  2.     <p><span id="errorMsg" ></span></p>
  3.     <divclass = "row" >
  4.         <divclass = "col-lg-half-dozen" >
  5.             <!-- Here we streaming video from webcam -->
  6.             <h4>
  7.                 Video coming from Webcam  <buttongrade = "btn btn-primary"  id= "btnCapture" >Capture to Sheet >></push>
  8.             </h4>
  9.             <video id="video"  playsinline autoplay></video>
  10.         </div>
  11.         <divclass = "col-lg-half dozen" >
  12.             <h4>
  13.                 Captured image from Webcam <input type="button" class = "btn btn-master"  id= "btnSave"  name= "btnSave"  value= "Salve the canvas(paradigm) to server"  />
  14.             </h4>
  15.             <!-- Webcam video snapshot -->
  16.             <canvas manner="border:solid 1px #ddd;background-color:white;"  id= "canvas"  width= "475"  peak= "475" ></canvas>
  17.         </div>
  18.     </div>
  19. </div>

Scripts

  1. <script type= "text/javascript" >
  2. var  video = document.querySelector( "#video" );
  3. const  constraints = {
  4.         audio:false ,
  5.         video: {
  6.             width: 475, height: 475
  7.         }
  8.     };
  9. if  (navigator.mediaDevices.getUserMedia) {
  10.         navigator.mediaDevices.getUserMedia(constraints)
  11.             .then(part  (stream) {
  12.                 video.srcObject = stream;
  13.             })
  14.             .take hold of ( function  (err0r) {
  15.                 console.log("Something went incorrect!" );
  16.             });
  17.     }
  18. function  stop(due east) {
  19. var  stream = video.srcObject;
  20. var  tracks = stream.getTracks();
  21. for  ( var  i = 0; i < tracks.length; i++) {
  22. var  rail = tracks[i];
  23.             track.terminate();
  24.         }
  25.         video.srcObject =zippo ;
  26.     }
  27. </script>
  28. <script type="text/javascript" >
  29.     $("#btnCapture" ).click( function  () {
  30. var  canvas = certificate.getElementById( 'canvas' );
  31. var  context = canvas.getContext( '2d' );
  32.         context.drawImage(video, 0, 0);
  33.     });
  34.     $("#btnSave" ).click( part  () {
  35. var  destinationCanvas = document.createElement( "canvas" );
  36. var  destCtx = destinationCanvas.getContext( '2d' );
  37.         destinationCanvas.height = 500;
  38.         destinationCanvas.width = 500;
  39.         destCtx.interpret(video.videoWidth, 0);
  40.         destCtx.scale(-1, ane);
  41.         destCtx.drawImage(document.getElementById("canvas" ), 0, 0);
  42. var  imagebase64data = destinationCanvas.toDataURL( "image/png" );
  43.         imagebase64data = imagebase64data.supervene upon('data:image/png;base64,' , '' );
  44.         $.ajax({
  45.             blazon:'Mail' ,
  46.             url:'/Home/UploadWebCamImage' ,
  47.             data:'{ "imageData" : "'  + imagebase64data + '" }' ,
  48.             contentType:'application/json; charset=utf-8' ,
  49.             dataType:'text' ,
  50.             success:function  (out) {
  51.                 alert('Image uploaded successfully..' );
  52.             }
  53.         });
  54.     });
  55. </script>

Below is the condition which really asks the user for the Webcam permission.

  1. if  (navigator.mediaDevices.getUserMedia)

CSS

Note

Nosotros need to add below CSS to flip the video and canvas every bit webcam is not showing mirror video.

  1. <fashion type= "text/css" >
  2.     video {
  3.         -webkit-transform: scaleX(-i );
  4.         transform: scaleX(-1 );
  5. margin-tiptop : 5px ;
  6.     }
  7. #canvas  {
  8.         -moz-transform: scaleX(-1 );
  9.         -o-transform: scaleX(-i );
  10.         -webkit-transform: scaleX(-1 );
  11.         transform: scaleX(-i );
  12.         filter: FlipH;
  13.         -ms-filter:"FlipH" ;
  14.     }
  15. </style>

C# Code

  1. public cord  UploadWebCamImage( cord  imageData)
  2. {
  3. string  filename = Server.MapPath( "~/UploadWebcamImages/webcam_" ) + DateTime.Now.ToString().Supervene upon( "/" , "-" ).Supersede( " " , "_" ).Replace( ":" , "" ) + ".png" ;
  4. using  (FileStream fs = new  FileStream(filename, FileMode.Create))
  5.             {
  6. using  (BinaryWriter bw = new  BinaryWriter(fs))
  7.                 {
  8. byte [] data = Convert.FromBase64String(imageData);
  9.                     bw.Write(data);
  10.                     bw.Close();
  11.                 }
  12.             }
  13. return "success" ;
  14. }

And then once all the setup is washed and we run the projection, first nosotros have to give permission southward for the Webcam on the  browser on which the awarding is running as below.

Chrome

Firefox

Let'south have a look at the running project

HTML subsequently giving permission to access webcam,

HTML After clicking the capture push button.

HTML subsequently clicking the save the prototype to Server,

And here nosotros can meet the uploaded prototype on the project folder.

Delight download the source coude fastened with this Commodity and change it according to your requirement.

Notes

Due to the large size I have not added below packages in the nil foder as they were created by defauly the Visual Studio 2019.

  • Microsoft.AspNet.Mvc.five.2.7
  • Microsoft.AspNet.Razor.iii.2.7
  • Microsoft.AspNet.WebPages.3.2.vii
  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0
  • Microsoft.Web.Infrastructure.1.0.0.0

Happy Coding!!

hallsupothis.blogspot.com

Source: https://www.c-sharpcorner.com/article/capture-image-from-webcam-video-usning-html5-using-javascript-and-upload-to-serv/

0 Response to "Click on Image to Upload Picture Html 5"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel