人脸识别

样式html

<!--刷脸-->
<div hr-draggable modal="faceTradeModal" options="faceTradeOpts" id="faceTradeModal">
    <div class="modal-header">
        <h5>刷脸认证</h5>
    </div>
    <div class="modal-body form-horizontal margin5" style="text-align: center;">
        <video id="myVideo" width="500px" height="500px" ></video>
        <canvas id="myCanvas" width="200px" height="150px" ng-show="false"></canvas>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" ng-click="updateFace()" ng-show = "autoFace == 0">认证</button>
    </div>
</div>

javaScript

var mediaStreamTrack;
//打开人脸认证modal
$scope.openFaceTradeModel = function (){
    //获取照片
    $http.get(Path.getUri("api/patient/patient-id/" + $scope.activePatientInfo.patientId + "/photo"), {responseType: "arraybuffer"})
        .success(function (data) {
            $scope.faceTradeModal = true;
            $scope.currentFaceDate = _arrayBufferToBase64(data);
            setTimeout(function () {
                //打开摄像头
                $scope.openFace();
            },500);
        })
        .error(function () {
        });
};
//截取头像
$scope.getFace = function(){
    var canvas = $('#myCanvas')[0],
        context = canvas.getContext('2d'),
        video = $('#myVideo')[0];
    context.drawImage(video, 0, 0, 200, 150);
}
//关闭modal框清理计时器
$scope.closeFaceTradeModal = function(){
    $scope.faceTradeModal = false;
    if($scope.funHandle){
        clearInterval($scope.funHandle);
    }
    mediaStreamTrack && mediaStreamTrack.stop();
}
//打开摄像头
$scope.openFace = function() {
    // 获取媒体方法(旧方法)
    navigator.getMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMeddia || navigator.msGetUserMedia;

    var canvas = $('#myCanvas')[0],
        context = canvas.getContext('2d'),
        video = $('#myVideo')[0];
    // 获取媒体方法(新方法)
    // 使用新方法打开摄像头
    if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
        navigator.mediaDevices.getUserMedia({
            video: true,
            audio: true
        }).then(function (stream) {
            console.log(stream);
            mediaStreamTrack = typeof stream.stop === 'function' ? stream : stream.getTracks()[1];
            video.src = (window.URL || window.webkitURL).createObjectURL(stream);
            video.play();
        }).catch(function (err) {
            console.log(err);
        })
    }
    // 使用旧方法打开摄像头
    else if (navigator.getMedia) {
        navigator.getMedia({
            video: true
        }, function (stream) {
            mediaStreamTrack = stream.getTracks()[0];
            video.src = (window.URL || window.webkitURL).createObjectURL(stream);
            video.play();
        }, function (err) {
            console.log(err);
        });
    }
    //$scope.autoFace自动人脸识别参数控制
    if($scope.autoFace == 1){
        //$scope.funHandle计时器返回一个计时器
        if($scope.funHandle){//如果计时器存在释放计时器
            clearInterval($scope.funHandle);
        }
        //为了防止正在人脸识别中再次发起人脸识别
        $scope.facingFlag = false;
        $scope.funHandle = setInterval(function(){
            if($scope.faceTradeModal == true && $scope.facingFlag == false){
                $scope.facingFlag = true;
                $scope.updateFace();
            }
        },$scope.autoFaceSpace);
        //$scope.autoFaceSpace多少秒自动识别一次
    }
}
//认证
$scope.updateFace = function(){
    var canvas = $('#myCanvas')[0],
        context = canvas.getContext('2d'),
        video = $('#myVideo')[0];
    $scope.getFace();
    var dataUrl = canvas.toDataURL('image/png');
    var param = {
        "patientId":$scope.activePatientInfo.patientId,
        "photoBybase641":$scope.currentFaceDate,
        "photoBybase642":dataUrl
    }
    $http.post(Path.getUri("api/drug/face-recognition/check"), param)
        .success(function (data) {
            if($scope.jumpFlag == false) {
                if (data >= $scope.percentLike) {
                    $scope.faceAuthFlag = true;
                    $http.get(Path.getUri("api/patient-list/save-face-auth?flag=1&visitNo=" + $scope.activePatientInfo.visitNo + "&encounterNo=" + ($scope.faceAuthSource == 3 ? $scope.activePatientInfo.encounterNo : "")))
                        .success(function (data) {
                        });
                        $scope.closeFaceTradeModal();
                } else {
                    $scope.facingFlag = false;
                    if($scope.autoFace == 0) {
                        hrDialog.dialog(hrDialog.typeEnum.WARN, {message: "认证失败,不是同一个患者!"})
                            .close(function (result) {
                            });
                    }
                }
            }
            console.log(data);
        })
        .error(function () {
            $scope.facingFlag = false;
            if($scope.autoFace == 0) {
                hrDialog.dialog(hrDialog.typeEnum.WARN, {message: "头像数据对比失败!"})
                    .close(function (result) {
                        hrProgress.close();
                    });
            }
        });
}

 

posted @ 2019-05-28 18:17  发福大叔  阅读(190)  评论(0编辑  收藏  举报