自最近2天以来,我试图弄清楚出了什么问题?我正在使用Microsoft认知服务来制作Cordova Android应用程序,以供面部识别。为了拍摄图像,我使用了Cordova摄像头插件和执行操作(检测面,识别等),我正在使用JS。我将在这篇文章中逐步解释代码。这是我的内容安全策略:
<meta http-equiv="Content-Security-Policy" content="media-src * blob:; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src 'self' 'unsafe-inline' *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
之后,标准的HTML代码用于显示捕获图片的显示按钮
<button id="take-picture-button">Take Picture</button>
现在,让我们进入.js文件代码,因为它是Cordova摄像头插件,所以我使用了一些预定义的事件:
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('pause', this.onPause, false);
document.addEventListener('resume', this.onResume, false);
},
onDeviceReady: function () {
document.getElementById("take-picture-button").addEventListener("click", function () {
appState.takingPicture = true;
navigator.camera.getPicture(cameraSuccessCallback, cameraFailureCallback,
{
sourceType: Camera.PictureSourceType.CAMERA,
destinationType: Camera.DestinationType.FILE_URI,
targetWidth: 500,
targetHeight: 500
}); });
},
和之后onpause:function(){}&amp;onResume:function(){}以下是我通过使用MS-认知服务面对api进行面部检测的代码:(从faceapi文档中,我知道我可以在POST方法中发送二进制数据或blob或文件,因此我需要转换图像中的二进制数据)我将一起发布图像转换代码和AJAX代码,以便您可以理解。
var img = new Image();
img.src = imageUri; // System Path (eg: file:///storage/android/.......)
var canvas = document.createElement("canvas");
canvas.width = $(window).width();
canvas.height = $(window).height();
var ctx = canvas.getContext("2d");
img.onload = function () {
ctx.drawImage(img, 0, 0);
}
var dataURL = canvas.toDataURL("image/jpeg");
var data = dataURL.split(',')[1];
var mimeType = dataURL.split(';')[0].slice(5)
var bytes = window.atob(data);
var buf = new ArrayBuffer(bytes.length);
var byteArr = new Uint8Array(buf);
for (var i = 0; i < bytes.length; i++) {
byteArr[i] = bytes.charCodeAt(i);
}
var params = {
"returnFaceId": "true",
"returnFaceLandmarks": "false",
"returnFaceAttributes": "age",
};
var faceIds = new Array();
$.ajax({
url: "https://australiaeast.api.cognitive.microsoft.com/face/v1.0/detect?" + $.param(params),
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", "API_KEY");
},
type: "POST",
data: byteArr,
processData: false,
})
.done(function (data) {
for (var i = 0; i < data.length; i++) {
faceIds.push(data.faceId);
alert("FaceID at index"+ i+" is " + JSON.stringify(data.faceId[i]));
}
})
.fail(function (jqXHR, textStatus, errorThrown) {
alert("Failed in Face Detect, Details: Status:: " + jqXHR.status + " ResponseText:: " + jqXHR.statusText + "");
});
现在,上述代码的输出是"脸部检测失败,详细信息:status :: 400 ResponseText ::不良请求我不明白我需要在哪里进行更改,或者我错过了什么?请帮忙。谢谢
响应中应该有一条消息,以告诉您出了什么问题。有几个可能的原因,例如无效,无效,无效。和消息。