活体检测代码提交

This commit is contained in:
袁帅
2022-10-24 17:06:53 +08:00
parent 4965600636
commit 38fd61e948
8 changed files with 753 additions and 25 deletions

View File

@ -6,6 +6,8 @@
<!-- <link href="cs.css" rel="stylesheet" type="text/css"> -->
<script src="jquery.js"></script>
<script src="jquery.webcam.min.js"></script>
<script src="jsrsasign-latest-all-min.js"></script>
<script src="living.min.js"></script>
<!-- <script src="excanvas.js"></script> -->
</head>
<body>
@ -14,6 +16,9 @@
<img id="base64image" src='' width="450" height="320" style="float: left; clear: both;"/> -->
<script type="text/javascript">
var pos = 0, ctx = null, saveCB,w = 380,h= 200, image = new Array();
var lv = null;
var action = 1,timer = 1000;
var liveDetectPhoto = null;
var canvas = document.createElement("canvas");//创建画布指定宽度和高度
canvas.setAttribute('width', 320);
canvas.setAttribute('height', 240);
@ -46,7 +51,8 @@
if (pos == 4 * 320 * 240) {
//把图像放到画布上,输出为png格式
ctx.putImageData(img, 0, 0);
window.parent.postMessage({"image": canvas.toDataURL("image/png")}, '*');
liveDetectPhoto = base64ToBlob(canvas.toDataURL("image/png"));
//window.parent.postMessage({"image": canvas.toDataURL("image/png")}, '*');
image = new Array();
pos = 0;
}
@ -59,7 +65,6 @@
// pos = 0;
// }
},
onCapture: function(data) {
webcam.save();
// Show a flash for example
@ -79,17 +84,102 @@
// $('#snapBtn').on('click', function() {
// webcam.capture();
// });
window.addEventListener('message', receiveMessageFromParent, false);
function receiveMessageFromParent ( event ) {
if(event.data == 'releaseCamera'){
location.reload();
}else if(event.data = 'capture'){
// liveDetectStart();
webcam.capture();
console.log(1,liveDetectPhoto);
}
//$('#base64image').attr('src', 'data:image/jpg;base64,' + event.data.data);
};
window.addEventListener('message', receiveMessageFromParent, false);
function setLiveDetectToken() {
let appKey= 'nkYy3g1yT0alE8pF6a1UTC4I';
let appSecrect = 'L30zHpTyAtTlY7tTCpbzdxxKCQgwWIQL';
// Header
var header = {
typ: "JWT", // 声明类型
alg: "HS256" // 声明加密的算法 通常直接使用 HMAC SHA256
};
var payload = {
iss: appKey,
iat: moment().unix(),
exp: moment()
.add(29, "minutes")
.unix()
};
var sHeader = JSON.stringify(header);
var sPayload = JSON.stringify(payload);
var sJWT = KJUR.jws.JWS.sign("HS256", sHeader, sPayload, {
utf8: appSecrect,
});
return sJWT;
}
function liveDetectStart () {
liveDetectStop();
let _lv = new Living(null, {
timer: 10000,
action : [1,3,2],
token: setLiveDetectToken(),
proxy:'/living/api',
getFacePicture:function(){
webcam.capture();
return liveDetectPhoto;
},
//开始活体检测此时token已鉴权成功
onDetectStart:function(){
//setSubmitLoading(true);
},
//每轮检测开始
onDetectActionStart:function(action){
action = action
},
//每轮检测计时
onDetectActionProgress:function(action,timer){
timer = timer;
window.parent.postMessage({"actionAndTimer": {action:action,timer:timer}}, 'detectStatus');
},
//每轮检测结束
onDetectActionFinish:function(action,data){
// this.clearCanvas()
timer = 10000;
console.log(data)
},
//活体检测完成
onDetectFinish:function(data){
window.parent.postMessage({"detectResult": data}, 'detectFinish');
}
});
//开始检测
_lv.start();
lv = _lv;
}
/**
* 停止活体检测
*/
function liveDetectStop (){
lv && lv.stop();
}
/**
* base64Url转blob
*/
function base64ToBlob(base64) {
var parts = base64.split(";base64,");
var contentType = parts[0].split(":")[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
for (let i = 0; i < rawLength; i += 1) {
uInt8Array[i] = raw.charCodeAt(i);
}
return new Blob([uInt8Array], { type: contentType });
};
});
</script>
</body>