-
Notifications
You must be signed in to change notification settings - Fork 17
/
imageAsyncResultDemo.js
90 lines (89 loc) · 3.38 KB
/
imageAsyncResultDemo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var utils=require("./utils");
//产品密钥ID,产品标识
var secretId="your_secret_id";
// 产品私有密钥,服务端生成签名信息使用,请严格保管,避免泄露
var secretKey="your_secret_key";
// 业务ID,易盾根据产品业务特点分配
var businessId="your_business_id";
// 易盾反垃圾云服务图片离线检测结果获取接口地址
var apiurl="http://as.dun.163.com/v4/image/asyncResult";
//请求参数
var post_data = {
// 1.设置公有有参数
secretId:secretId,
businessId:businessId,
version:"v4",
timestamp:new Date().getTime(),
nonce:utils.noncer(),
signatureMethod:"MD5", // MD5, SM3, SHA1, SHA256
};
// 2. 设置私有参数
var taskIds=["3898f9e189404ea98fb20e77d11b69e3","3f343b8947a24a6987cba8ef5ea6534f"];
post_data.taskIds=JSON.stringify(taskIds);
var signature=utils.genSignature(secretKey,post_data);
post_data.signature=signature;
//http请求结果
var responseCallback=function(responseData){
var data = JSON.parse(responseData);
var code=data.code;
var msg=data.msg;
if(code==200){
// 反垃圾检测结果
var antispamArray=data.antispam;
for(var i=0;i<antispamArray.length;i++){
var obj=antispamArray[i];
var name=obj.name;
var taskId=obj.taskId;
var status=obj.status;
var labelsArray=obj.labels;
if(status==0){
var action=obj.action;
console.log("taskId="+taskId+",status="+status+",name="+name+",action="+action);
// 产品需根据自身需求,自行解析处理,本示例只是简单判断分类级别
for(var k=0;k<labelsArray.length;k++){
var labelObj=labelsArray[k];
var label=labelObj.label;
var level=labelObj.level;
var rate=labelObj.rate;
// subLabels这二级分类数组,根据需要解析
var subLabels=labelObj.subLabels;
console.log("lable:"+label+",level:"+level+",rate:"+rate);
}
switch (action) {
case 0:
console.log("#图片机器检测结果:最高等级为\"正常\"\n");
break;
case 1:
console.log("#图片机器检测结果:最高等级为\"嫌疑\"\n");
break;
case 2:
console.log("#图片机器检测结果:最高等级为\"确定\"\n");
break;
default:
break;
}
}else{
console.log("检测失败");
}
}
// ocr结果
var ocrArray=data.ocr;
for(var i=0;i<ocrArray.length;i++){
var obj=ocrArray[i];
var name=obj.name;
var taskId=obj.taskId;
var detailsArray=obj.details;
console.log("taskId="+taskId+",name="+name);
for(var k=0;k<detailsArray.length;k++){
var detail=detailsArray[k];
var content=detail.content;
// lineContents为ocr片段及坐标信息,根据需要解析
var lineContents=detail.lineContents;
console.log("识别ocr文本内容:"+content);
}
}
}else{
console.log('ERROR:code=' + code+',msg='+msg);
}
}
utils.sendHttpRequest(apiurl,"POST",post_data,responseCallback);