Demo Project of Defog

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/vision/defog URI. Replace the AK/SK information with the actual AK/SK to run the demo.

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

    // 1. Configure the basic information for accessing Defog 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 original image path (data/defog-demo-1.png) and the path for saving the image generated after processing (data/defog-demo-1.cooked.png) in the DefogDemo.java file.

    //
    // 2. Construct the parameters required for accessing Defog.
    //
    String uri = "/v1.0/vision/defog";
    byte[] fileData = FileUtils.readFileToByteArray(new File("data/defog-demo-1.png"));
    String fileBase64Str = Base64.encodeBase64String(fileData);
    			
    JSONObject json = new JSONObject();
    json.put("image", fileBase64Str);
    json.put("gamma", 1.5);
    json.put("natural_look", true);
    // 3. Pass the URI and required parameters of Defog.
    // Pass the parameters in JSON objects and call the service using POST.
    HttpResponse response = service.post(uri, json.toJSONString());
    // 4. Check whether the API call is successful. If 200 is returned, the API call succeeds. Otherwise, it fails.
    ResponseProcessUtils.processResponseStatus(response);
    // 5. Process the character stream returned by the service and create the image file processed by Defog.
    ResponseProcessUtils.processResponseWithImage(response, "data/defog-demo-1.cooked.png");

  3. Execute the DefogDemo.java file. The recognition result is output. If 200 is displayed on the console, the program is successfully executed. Figure 1 shows the original image and defogged image.

    Figure 1 Comparison between the original image and defogged image