All Products
Search
Document Center

Content Moderation:OCR

Last Updated:Aug 03, 2023

This topic describes how to use Content Moderation SDK for Java to perform optical character recognition (OCR). This way, you can recognize text in images.

Prerequisites

  • Java dependencies are installed. For more information, see Installation.

    Note

    You must use the Java version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

  • The Extension.Uploader utility class is downloaded and imported into your project if you submit a local image or a binary image stream for image moderation.

Submit synchronous OCR tasks

Operation

Description

Supported region

ImageSyncScanRequest

Submits synchronous OCR tasks with the scenes parameter set to ocr to recognize text in images.

  • cn-shanghai

  • cn-beijing

  • cn-shenzhen

  • ap-southeast-1

Sample code

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.*;

public class Main {

  public static void main(String[] args) throws Exception {
   /**
     * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M.  
     * Common ways to obtain environment variables:
     * Method 1:
     *     Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     *     Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     * Method 2:
     *     Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
     *     Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    DefaultProfile profile = DefaultProfile.getProfile(
        "cn-shanghai",
        "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
        "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
    DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    IAcsClient client = new DefaultAcsClient(profile);

    ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
    // Specify the response format of the operation. 
    imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
    // Specify the request method. 
    imageSyncScanRequest.setMethod(MethodType.POST);
    imageSyncScanRequest.setEncoding("utf-8");
    // Both HTTP and HTTPS are supported. 
    imageSyncScanRequest.setProtocol(ProtocolType.HTTP);

    JSONObject httpBody = new JSONObject();
    /**
    * Specify the moderation scenario. 
    * The value ocr of the scenes parameter indicates that the server uses OCR to moderate text in images. 
    */
    httpBody.put("scenes", Arrays.asList("ocr"));

        /**
         * Create one task for each image to be moderated. 
         * If you moderate multiple images in a request, the total response time that the server spends processing the request begins when the request is initiated and ends upon moderation of the last image. 
         * The average response time of moderating multiple images in a request is longer than that of moderating a single image. The more images you submit at a time, the higher the probability that the average response time will be extended. 
         * In this example, a single image is moderated. If you need to moderate multiple images at a time, create one task for each image to be moderated. 
         */
    JSONObject task = new JSONObject();
    task.put("dataId", UUID.randomUUID().toString());

    // Specify the image URL. 
    task.put("url", "https://example.com/xxx.jpg");
    httpBody.put("tasks", Arrays.asList(task));

    imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);
    /**
    * Set the connection timeout period and the read timeout period. The timeout period for the server to complete an image moderation request is 10s. Specify the timeout based on this period. 
    * If you set the read timeout to a period shorter than 10s, the server may generate a read timeout error during request processing. 
    */
    imageSyncScanRequest.setConnectTimeout(3000);
    imageSyncScanRequest.setReadTimeout(10000);
    HttpResponse httpResponse = null;
    try {
      httpResponse = client.doAction(imageSyncScanRequest);
    } catch (ServerException e) {
      e.printStackTrace();
    } catch (ClientException e) {
      e.printStackTrace();
    } catch (Exception e){
      e.printStackTrace();
    }

    // The results that are returned after the server receives and processes your request. 
    if(httpResponse != null && httpResponse.isSuccess()){
      JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));
      System.out.println(JSON.toJSONString(scrResponse));
      int requestCode = scrResponse.getIntValue("code");
      // The moderation results of all images. 
      JSONArray taskResults = scrResponse.getJSONArray("data");
      if (200 == requestCode) {
        for (Object taskResult : taskResults) {
          // The moderation results of a single image. 
          int taskCode = ((JSONObject)taskResult).getIntValue("code");
          // The moderation results of the image in the corresponding moderation scenario. If you specified multiple moderation scenarios in the request, the moderation results of the image in each scenario are returned. 
          JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
          if(200 == taskCode){
            for (Object sceneResult : sceneResults) {
              String scene = ((JSONObject)sceneResult).getString("scene");
              String suggestion = ((JSONObject)sceneResult).getString("suggestion");
              //do something
              // The text in the moderated image. 
              if("review" .equals(suggestion) && "ocr".equals(scene)){
                JSONObject idCardInfo = ((JSONObject) sceneResult).getJSONObject("idCardInfo");
                System.out.println(idCardInfo.toJSONString());
              }
            }
          }else{
            // A single image failed to be moderated. Analyze the failure based on the actual situation. 
            System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
          }
        }
      } else {
        /**
        * The request failed to be processed. Analyze the failure based on the actual situation. 
        */
        System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
      }
    }
  }
}