This topic describes the operations provided by the Java SDK to manage custom image libraries. You can call these operations to customize image samples based on personalized content management requirements to intelligently detect pornography, terrorist content, and ads in images and videos.

Query custom image libraries

Use the following code to obtain the list of image libraries, including custom image libraries and feedback-based image libraries:
DescribeImageLibRequest describeImageLibRequest = new DescribeImageLibRequest();
        describeImageLibRequest.setServiceModule("open_api");

        try {
            // List all image libraries, including custom image libraries and feedback-based image libraries.
            DescribeImageLibResponse describeImageLibResponse = client.getAcsResponse(describeImageLibRequest);
            System.out.println(JSON.toJSONString(describeImageLibResponse));

            List<DescribeImageLibResponse.ImageLib> allLibs = describeImageLibResponse.getImageLibList();
            List<DescribeImageLibResponse.ImageLib> customImageLibs = new ArrayList<DescribeImageLibResponse.ImageLib>();
            for (DescribeImageLibResponse.ImageLib imageLib : allLibs) {
                String source =  imageLib.getSource();
                // List custom image libraries.
                if("MANUAL".equals(source)) {
                    customImageLibs.add(imageLib);
                }

                // List feedback-based image libraries.
                if("FEEDBACK".equals(source)) {
                    customImageLibs.add(imageLib);
                }
            }

            System.out.println(JSON.toJSONString(customImageLibs));
        } catch (ClientException e) {
            e.printStackTrace();
        }

Create a custom image library

Use the following code to create a custom image library:
Note Set parameters based on the moderation scenario.
CreateImageLibRequest  createImageLibRequest = new CreateImageLibRequest();
        createImageLibRequest.setServiceModule("open_api");
        createImageLibRequest.setName("Pornography detection blacklist");
        createImageLibRequest.setScene("PORN");
        createImageLibRequest.setCategory("BLACK");

        try {
            CreateImageLibResponse createImageLibResponse = client.getAcsResponse(createImageLibRequest);
            System.out.println(JSON.toJSONString(createImageLibResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }

Modify a custom image library

Use the following code to modify the Name and BizTypes parameters of a custom image library:
UpdateImageLibRequest  updateImageLibRequest = new UpdateImageLibRequest();
        // Enter the ID of the custom image library.
        updateImageLibRequest.setId(12345);
        updateImageLibRequest.setName("New name of the custom image library");
        updateImageLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList("comment")));
        updateImageLibRequest.setCategory("WHITE");
        updateImageLibRequest.setScene("PORN");
        try {
            UpdateImageLibResponse updateImageLibResponse = client.getAcsResponse(updateImageLibRequest);
            System.out.println(JSON.toJSONString(updateImageLibResponse));
        } catch (ClientException e) {
            e.printStackTrace();

Delete a custom image library

Use the following code to delete a custom image library:
Note After you delete a custom image library, all images in the library are also deleted.
DeleteImageLibRequest  deleteImageLibRequest = new DeleteImageLibRequest();
        // Enter the ID of the custom image library.
        deleteImageLibRequest.setId(12345);
        try {
            DeleteImageLibResponse deleteImageLibResponse = client.getAcsResponse(deleteImageLibRequest);
            System.out.println(JSON.toJSONString(deleteImageLibResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }

Query images in a custom image library

Use the following code to obtain the list of all images in a custom image library:
DescribeImageFromLibRequest describeImageFromLibRequest = new DescribeImageFromLibRequest();
        describeImageFromLibRequest.setImageLibId(1519);
        describeImageFromLibRequest.setPageSize(20);
        describeImageFromLibRequest.setCurrentPage(1);
        try {
            DescribeImageFromLibResponse describeImageFromLibResponse = client.getAcsResponse(describeImageFromLibRequest);
            System.out.println(JSON.toJSONString(describeImageFromLibResponse));
        } catch (ClientException e) {
            e.printStackTrace();
        }

Add images to a custom image library

Use the following code to add images to a custom image library. Follow these steps to add images:
  1. Obtain the image upload credential.
  2. Upload images to the custom image library.
  3. Submit the information about the library to which images are uploaded and the image path information to the server.
// Obtain the image upload credential.
        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();
        }

        // Upload images.
        CustomLibUploader customLibUploader = new CustomLibUploader();
        String object = null;
        try {
             object = customLibUploader.uploadFile(describeUploadInfoResponse.getHost(), describeUploadInfoResponse.getFolder(), describeUploadInfoResponse.getAccessid(),
                     describeUploadInfoResponse.getPolicy(), describeUploadInfoResponse.getSignature(),
                     "/Users/liuhai.lh/Desktop/a.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(org.apache.commons.lang.StringUtils.isNotBlank(object)){
            UploadImageToLibRequest imageToLibRequest = new UploadImageToLibRequest();
            imageToLibRequest.setImageLibId(1519);
            imageToLibRequest.setImages(JSON.toJSONString(Arrays.asList(object)));

            try {
                UploadImageToLibResponse uploadImageToLibResponse = client.getAcsResponse(imageToLibRequest);
                System.out.println(JSON.toJSONString(uploadImageToLibResponse));
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }

Delete custom images from a custom image library

Use the following code to delete multiple custom images from a custom image library:
DeleteImageFromLibRequest deleteImageFromLibRequest = new DeleteImageFromLibRequest();
deleteImageFromLibRequest.setIds(JSON.toJSONString(Arrays.asList(669310)));
try {
    DeleteImageFromLibResponse deleteImageFromLibResponse = client.getAcsResponse(deleteImageFromLibRequest);
    System.out.println(JSON.toJSONString(deleteImageFromLibResponse));
} catch (ClientException e) {
    e.printStackTrace();
}