本示例介绍如何使用Alibaba Cloud SDK for Java自定义智能鉴黄、暴恐涉政识别、图片或视频广告的图片样本,满足个性化内容管控需求。

前提条件

在使用本教程之前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-green -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-green</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-oss -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>2.8.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>
    </dependencies>
  • 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类

代码示例

  • 初始化请求公共参数
    public static IAcsClient createClient() {
        IClientProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }
  • 获取自定义图库列表
    private static void describeImageLib(IAcsClient client) {
        DescribeImageLibRequest request = new DescribeImageLibRequest();
        // open_api:用于内容检测API功能的图库
        request.setServiceModule("open_api");
        try {
            DescribeImageLibResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
  • 创建图库
    private static void createImageLib(IAcsClient client) {
        CreateImageLibRequest request = new CreateImageLibRequest();
        // 服务模块名称,取值
        // open_api:用于内容检测API功能的词库
        // website:用于站点检测功能的词库
        request.setServiceModule("open_api");
        // 图库名称。
        request.setName("鉴黄黑库");
        // 图库使用场景,取值:
        // PORN:智能鉴黄
        // AD:广告识别
        // ILLEGAL:暴恐涉政识别
        request.setScene("PORN");
        // 图库类型,取值:
        // BLACK:黑名单
        // WHITE:白名单
        request.setCategory("BLACK");
    
        try {
            CreateImageLibResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
  • 修改图库信息
    private static void updateImageLib(IAcsClient client) {
        UpdateImageLibRequest request = new UpdateImageLibRequest();
        // 主键ID。
        request.setId(12345);
        // 图库名称。
        request.setName("鉴黄黑库改名");
        //
        List<String> BizTypes = new ArrayList<String>();
        BizTypes.add("comment");
        request.setBizTypes(JSON.toJSONString(BizTypes));
        // 词库类型,取值:
        // BLACK:黑名单
        // WHITE:白名单
        request.setCategory("WHITE");
        // 图库使用场景,取值:
        // PORN:智能鉴黄
        // AD:广告识别
        // ILLEGAL:暴恐涉政识别
        request.setScene("PORN");
    
        try {
            UpdateImageLibResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
  • 删除自定义图库
    private static void deleteImageLib(IAcsClient client) {
        DeleteImageLibRequest request = new DeleteImageLibRequest();
        // 图库主键ID
        request.setId(12345);
        try {
            DeleteImageLibResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
  • 查询图库中图片
    private static void describeImageFromLib(IAcsClient client) {
        DescribeImageFromLibRequest request = new DescribeImageFromLibRequest();
        // 图库主键ID
        request.setImageLibId(1519);
        // 返回的分页大小,即每个分页显示的图片数量
        request.setPageSize(20);
        // 返回的当前分页
        request.setCurrentPage(1);
        try {
            DescribeImageFromLibResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
  • 向自定义图库中添加图片
    private static void uploadImageToLib(IAcsClient client) {
        //获取上传凭证
        DescribeUploadInfoRequest describeUploadInfoRequest = new DescribeUploadInfoRequest();
        describeUploadInfoRequest.setBiz("customImageLib");
        DescribeUploadInfoResponse describeUploadInfoResponse = null;
        try {
            describeUploadInfoResponse = client.getAcsResponse(describeUploadInfoRequest);
            System.out.println(JSON.toJSONString(describeUploadInfoResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }
    
        //上传图片
        CustomLibUploader customLibUploader = new CustomLibUploader();
        String object = null;
        try {
            object = customLibUploader.uploadFile(describeUploadInfoResponse.getHost(), describeUploadInfoResponse.getFolder(), describeUploadInfoResponse.getAccessid(),
                    describeUploadInfoResponse.getPolicy(), describeUploadInfoResponse.getSignature(),
                    "C:/头像.png");
            System.out.println("------------------------------------------------");
            System.out.println(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        if(object != null){
            UploadImageToLibRequest imageToLibRequest = new UploadImageToLibRequest();
            imageToLibRequest.setImageLibId(7146);
            imageToLibRequest.setImages(JSON.toJSONString(Arrays.asList(object)));
    
            try {
                UploadImageToLibResponse uploadImageToLibResponse = client.getAcsResponse(imageToLibRequest);
                System.out.println(JSON.toJSONString(uploadImageToLibResponse));
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
    }
  • 删除自定义图片
    private static void deleteImageFromLib(IAcsClient client) {
        DeleteImageFromLibRequest request = new DeleteImageFromLibRequest();
        List<String> Ids = new ArrayList<String>();
        Ids.add("669310");
        request.setIds(JSON.toJSONString(Ids));
        try {
            DeleteImageFromLibResponse response = client.getAcsResponse(request);
            System.out.println(JSON.toJSONString(response, true));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }