package com.examples;

import com.google.gson.Gson;
import com.qiniu.http.Client;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import java.util.HashMap;
import java.util.Map;

public class ResourcesCensor {
    //设置好账号的ACCESS_KEY和SECRET_KEY
    private static final String ACCESS_KEY = "填写你们自己的ak";
    private static final String SECRET_KEY = "填写你们自己的sk";
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    public static void main(String args[]) {
        String result;
        //result = ImageCensor();
        result = VideoCensor();
        System.out.printf(result);
    }

    //参考api文档 https://developer.qiniu.com/dora/manual/4252/image-review
    //图片审核
    public static String ImageCensor() {
        // 构造post请求body
        Gson gson = new Gson();

        Map<String, Object> uri = new HashMap<>();
        uri.put("uri", "http://oayjpradp.bkt.clouddn.com/Audrey_Hepburn.jpg");

        Map<String, Object> scenes = new HashMap<>();
        //pulp 黄  terror 恐  politician 敏感人物
        String[] types = {"pulp", "terror", "politician", "ads"};
        scenes.put("scenes", types);

        Map params = new HashMap();
        params.put("data", uri);
        params.put("params", scenes);

        String paraR = gson.toJson(params);
        byte[] bodyByte = paraR.getBytes();

        // 接口请求地址
        String url = "http://ai.qiniuapi.com/v3/image/censor";

        return post(url, bodyByte);
    }

    //参考api文档 https://developer.qiniu.com/dora/manual/4258/video-pulp
    //视频审核
    public static String VideoCensor() {
        // 构造post请求body
        Gson gson = new Gson();

        Map bodyData = new HashMap();

        Map<String, Object> uri = new HashMap<>();
        uri.put("uri", "https://mars-assets.qnssl.com/scene.mp4");

        Map<String, Object> params = new HashMap<>();

        Map<String, Object> scenes = new HashMap<>();
        //pulp 黄  terror 恐  politician 敏感人物
        String[] types = {"pulp", "terror", "politician"};

        Map<String, Object> cut_param = new HashMap<>();
        cut_param.put("interval_msecs", 500);

        params.put("scenes", types);
        params.put("cut_param", cut_param);

        bodyData.put("data", uri);
        bodyData.put("params", params);
        String paraR = gson.toJson(bodyData);
        byte[] bodyByte = paraR.getBytes();

        // 接口请求地址
        String url = "http://ai.qiniuapi.com/v3/video/censor";
        return post(url, bodyByte);
    }

    private static String post(String url, byte[] body) {
        String accessToken = (String) auth.authorizationV2(url, "POST", body, "application/json")
                .get("Authorization");

        Client client = new Client();
        StringMap headers = new StringMap();
        headers.put("Authorization", accessToken);
        try {
            com.qiniu.http.Response resp = client.post(url, body, headers, Client.JsonMime);
            return resp.bodyString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

@@ -45,16 +45,56 @@
 n.Gson;%0A
+import com.qiniu.common.QiniuException;%0A
 import c
@@ -415,31 +415,24 @@
     private 
-static 
 final Auth a
@@ -473,16 +473,64 @@
 ET_KEY);
+%0A    private final Client client = new Client();
 %0A%0A    pu
@@ -580,42 +580,139 @@
     
-String result;%0A        //result = 
+ResourcesCensor resourcesCensor = new ResourcesCensor();%0A        String result;%0A        try %7B%0A            result = resourcesCensor.
 Imag
@@ -722,32 +722,38 @@
 nsor();%0A        
+    //
 result = VideoCe
@@ -745,16 +745,32 @@
 esult = 
+resourcesCensor.
 VideoCen
@@ -784,16 +784,20 @@
         
+   
+ 
 System.o
@@ -815,16 +815,96 @@
 esult);%0A
+        %7D catch (QiniuException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A 
@@ -992,31 +992,24 @@
 %0A    public 
-static 
 String Image
@@ -1008,32 +1008,54 @@
 ng ImageCensor()
+ throws QiniuException
  %7B%0A        // %E6%9E%84%E9%80%A0
@@ -1845,23 +1845,16 @@
  public 
-static 
 String V
@@ -1865,16 +1865,38 @@
 Censor()
+ throws QiniuException
  %7B%0A     
@@ -2806,23 +2806,16 @@
 private 
-static 
 String p
@@ -2842,16 +2842,38 @@
 %5B%5D body)
+ throws QiniuException
  %7B%0A     
@@ -3009,46 +3009,8 @@
 );%0A%0A
-        Client client = new Client();%0A
     
@@ -3105,34 +3105,25 @@
 n);%0A        
-try %7B%0A    
+%0A
         com.
@@ -3203,28 +3203,24 @@
 e);%0A        
-    
 return resp.
@@ -3237,104 +3237,8 @@
 ();%0A
-        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return null;%0A
     

Update ResourcesCensor.java