Take picture from webcam or mobile camera in web applications

后端 未结 2 2088
庸人自扰
庸人自扰 2021-02-07 14:11

I\'m developing a web application which browse and take pictures from local and also I want to capture images through the camera. Im using the following code and i can capture d

相关标签:
2条回答
  • 2021-02-07 14:22

    You can do it like this:

    $('#cameraInput').on('change', function(e){
     $data = e.originalEvent.target.files[0];
      $reader = new FileReader();
      reader.onload = function(evt){
      $('#your_img_id').attr('src',evt.target.result);
      reader.readAsDataUrl($data);
    }});
    
    0 讨论(0)
  • 2021-02-07 14:26

    Miles Erickson and Henock Bongi, you need to take reader.readAsDataUrl($data); out of the onload function in order that the onload fire.

    If you don't want to use jQuery see below:

    function readFile(file) {                                                       
        var reader = new FileReader();
        reader.onload = readSuccess;                                            
        function readSuccess(evt) {     
            document.getElementById("your_img_id").src = evt.target.result                   
        };
        reader.readAsDataURL(file);                                              
    } 
    
    document.getElementById('cameraInput').onchange = function(e) {
        readFile(e.srcElement.files[0]);
    };

    0 讨论(0)
提交回复
热议问题