oss濾網圖片音視頻過濾(1)內容檢測

圖片音視頻過濾有好多方法,我這裏就不一一介紹了,這篇文章只是簡單介紹一下我在項目中使用阿里雲oss濾網過濾的步驟html

1.所遇問題:java

1.圖片視頻鑑別時要設置textScanRequest.setUriPattern(uri);不然會報msg=[scene] is not support
2.內容安全內容檢測API服務接入地址爲:http://green.cn-shanghai.aliyuncs.com(上海)或http://green.cn-hangzhou.aliyuncs.com(杭州)
3.通訊協議:內容安全內容檢測API服務僅支持使用HTTP協議發送請求
4.全部接口僅支持HTTP POST方法發送請求
2.所需jar包

aliyun-java-sdk-core-3.0.7.jar
aliyun-java-sdk-green-3.0.0.jar
aliyun-sdk-oss-2.6.0-javadoc.jar
aliyun-sdk-oss-2.6.0-sources.jar
aliyun-sdk-oss-2.6.0.jarapi

以上jar包請前往https://help.aliyun.com/knowledge_detail/50170.html下載安全

2.設置公共請求參數
域名目前只支持"cn-hangzhou"和"cn-hangzhou"
// 請替換成你本身的accessKeyId、accessKeySecret
IClientProfile profile = DefaultProfile.getProfile(域名, accessKeyId,accessKeySecret);
// 內容安全內容檢測API服務接入地址爲:http://green.cn-hangzhou.aliyuncs.com
DefaultProfile.addEndpoint(須要檢測的文件名, 域名, "Green", "green.cn-hangzhou.aliyuncs.com");
 
IAcsClient client = new DefaultAcsClient(profile);
 
TextScanRequest textScanRequest = new TextScanRequest();
textScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
textScanRequest.setContentType(FormatType.JSON);
textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 全部接口僅支持HTTP
// POST方法發送請求,這種方式下請求參數須要包含在請求的URL中
textScanRequest.setEncoding("UTF-8");// 請求及返回結果都使用UTF-8字符集進行編碼
textScanRequest.setRegionId(域名);
 
3.方法調用

 

參考文檔:https://help.aliyun.com/document_detail/53423.html?spm=5176.doc28440.6.564.NWZQ2pdom

List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
Map<String, Object> task1 = new LinkedHashMap<String, Object>();
task1.put("dataId", UUID.randomUUID().toString());
task1.put("content", text);
tasks.add(task1);
JSONObject data = new JSONObject();
data.put("scenes", Arrays.asList("antispam"));
data.put("tasks", tasks);阿里雲

textScanRequest.setContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);編碼

try {
HttpResponse httpResponse = client.doAction(textScanRequest);spa

if (httpResponse.isSuccess()) {
JSONObject jo = JSON.parseObject(new String(httpResponse.getContent(), "UTF-8"));
Gson gson = new Gson();
TextBean fromJson = gson.fromJson(jo.toString(), TextBean.class);
List<TextBean.DataBean> data2 = fromJson.getData();
for (int i = 0; i < data2.size(); i++) {
TextBean.DataBean dataBean = data2.get(i);
List<TextBean.DataBean.ResultsBean> results = dataBean.getResults();
for (int j = 0; j < results.size();) {
TextBean.DataBean.ResultsBean resultsBean = results.get(j);
String suggestion = resultsBean.getSuggestion();
if (suggestion.equals("pass")) {
return true;
} else {
return false;
}
}
}orm

} else {
return false;
}視頻

} catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } return true; }

相關文章
相關標籤/搜索