七牛云鉴黄实现

maven依赖:

 
        
            com.squareup.okhttp3
            okhttp
            3.3.1
            compile
        
        
            com.google.code.gson
            gson
            2.6.2
            compile
        
        
            com.qiniu
            happy-dns-java
            0.1.4
            compile
        
        
            junit
            junit
            4.12
            test
        

        
            com.qiniu
            qiniu-java-sdk
            7.2.0
        

    

JAVA代码:

public class QiniuUtils {
    public static String ACCESS_KEY="您自己的";
    public static String SECRET_KEY="您自己的";


        public static Boolean pornography(String pis){
            Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

            String url = "http://ai.qiniuapi.com/v3/image/censor";
            String host = "ai.qiniuapi.com";

            String body = "{ \"data\": { \"uri\": \""+pis+"\" }, \"params\": { \"scenes\": [ \"pulp\", \"terror\", \"politician\" ] } }";
            String contentType = "application/json";

            String method = "POST";
            String qiniuToken = "Qiniu " + auth.signRequestV2(url, method, body.getBytes(), contentType);

            StringMap header = new StringMap();
            header.put("Host", host);
            header.put("Authorization", qiniuToken);
            header.put("Content-Type", contentType);
            Client client = new Client();
            Response response = null;
            String str ="";
            try {
                response = client.post(url, body.getBytes(), header, contentType);
                str = response.bodyString();
            } catch (QiniuException e) {
                e.printStackTrace();
            }
            if (!str.equals("")){
                JsonParser jp = new JsonParser();
                //将json字符串转成json对象
                JsonObject jo = jp.parse(str).getAsJsonObject();
                String label = jo.get("result").getAsJsonObject().get("scenes").getAsJsonObject()
                        .get("pulp").getAsJsonObject().get("details").getAsJsonArray().get(0).getAsJsonObject().get("label").getAsString();
                if (label.equals("pulp")){
                    return false;
                }
            }
            return true;
        }

    public static void main(String[] args) {
        System.out.println(pornography("http://photo.l99.com/bigger/5ea/1447034797367_auxhsd.jpg"));
    }
}

测试结果:七牛云鉴黄实现_第1张图片

书写不易:引用请注明出处

你可能感兴趣的:(七牛云鉴黄实现)