h5手機上調用攝像頭人像比對 與FileReader文件上傳

//顯示拍照
function showVideo(){
    canvas.width = video.offsetWidth * ratio;
    canvas.height = 200;
    context = canvas.getContext("2d");
    if(navigator.getUserMedia){
        navigator.mediaDevices.getUserMedia(myConstraints).then((stream) => {
            video.srcObject = stream;
        buffer = stream;
        video.play()
    }, (error) => {
            //ios11 上傳圖片或者拍照 在低版本蘋果上調用攝像頭失敗 就讓用戶點擊選擇拍照或者選擇自拍照
            $(".unDerIos11").show();
            $(".others").hide();
            console.log(error.name || error)
        })
    }else{
        $(".unDerIos11").show();
        $(".others").hide();
    }
}
  • 拍照完畢后要進行一個是顯示提示顯示照片 關閉了攝像頭
//方法關閉攝像頭
function closeCamera() {
    buffer && buffer.getTracks()[0].stop();
}
  • 拍照后圖片上傳到后端進行人像的對比
//拍照按鈕的單擊事件
$('#capture').click(function () {
    //繪制畫面
    context.drawImage(video,0,0,video.offsetWidth * 1 ,video.offsetHeight * 1 );
    switch ($(this).find('a').text()){
        case '識別':
            $('#canvas').show();
            $('#video').hide();
            $(this).addClass("reRec").find('a').html('重新識別');
            //上傳圖片的Base64
            uploadImg((canvas.toDataURL('image/png')),2);
            break;
        case '重新識別':
            $('#video').show();
            $('#canvas').hide();
            faceImg = 0;
            $('#video').show();
            $(".reRec-load").hide();
            $(this).removeClass("reRec").find('a').html('識別');
            break;
    }
});

以下是html5身份證上傳的js部分 使用FileReader

 <input id="upload"  class="weui-uploader__input" type="file" accept="image/*" multiple="">
//身份證上傳
var reader = new FileReader();
$('.card-face_wrap').on('change','#upload',function(){//圖片上傳
    var that =$(this).parents(".card-face_wrap");
    imgType = that.attr('val');
    //var AllowImgFileSize = 2100000; //上傳圖片最大值(單位字節)( 2 M = 2097152 B )超過2M上傳失敗
    var rd=new FileReader();  //創建文件讀取對象
    var file=$(this)[0].files[0];
    //var imgSize = file.size;
    rd.readAsDataURL(file);  //讀取類型為base64
    $(this).clone().replaceAll(file=this);
    rd.onload=function (ev) {
        //圖片顯示且上傳
        var str = '<input id="upload"  class="weui-uploader__input" type="file" accept="image/*" multiple=""><img id="imgDetail" src='+this.result+' />';
        that.html(str);
        if(imgType==1){
            curImg = this.result;
        }else if(imgType==2){
            curImg = this.result;
        }
        uploadImg(curImg,imgType);
    }

});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。