Help Center> Image Recognition> SDK Reference> Using Java SDK> Demo Project of Image Tagging

Demo Project of Image Tagging

Two authentication methods, token authentication and AK/SK authentication, are available. This section uses AK/SK authentication as an example.

This demo project corresponds to the POST /v1.0/image/tagging URI. Replace the AK/SK information with the actual AK/SK to run the demo.

  1. Configure the AK/SK in the ImageTaggingDemo.java file. The sample code is as follows:

    // 1. Configure the basic information for accessing Image Tagging and generate a client connection object.
    AisAccess service = ServiceAccessBuilder.builder()		
            .ak("######")                       // your ak
            .sk("######")                       // your sk
            .region("cn-north-1")               // Configuration of Image Recognition in the CN North-Beijing1 region
            .connectionTimeout(5000)            // Timeout limit for connecting to the target URL
            .connectionRequestTimeout(1000)     // Timeout limit for obtaining available connections from the connection pool
            .socketTimeout(20000)               // Timeout limit for obtaining server response data
            .build();

  2. Select a local image or use the default image of the sample project, and modify the image path (data/image-tagging-demo-1.jpg) in the ImageTaggingDemo.java file.

    //
    // 2. Construct the parameters required for accessing Image Tagging.
    //
    String uri = "/v1.0/image/tagging";
    byte[] fileData = FileUtils.readFileToByteArray(new File("data/image-tagging-demo-1.jpg"));
    String fileBase64Str = Base64.encodeBase64String(fileData);

  3. Execute the ImageTaggingDemo.java file. If 200 is displayed on the console, the program is successfully executed. The recognization result is shown in #image_04_0008/d0e652.

    Figure 1 Execution result