This topic describes how to use the Java SDK to call the optical character recognition (OCR) operation to detect text in common images and return the results in real time.


Before calling operations, make the following preparations:
  1. Create an AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information, see Create an AccessKey.
  2. Install Java dependencies. For more information, see Installation.
  3. Download and import the Extension.Uploader utility class into your project if you submit a local image or a binary image stream for image moderation.

Submit synchronous OCR tasks

Operation Feature Region Description
ImageSyncScanRequest Sends synchronous OCR requests to detect text in images.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore
  • ap-southeast-5: Indonesia (Jakarta)
This operation detects text in images after you specify ocr in the scenes parameter.
Sample code
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
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 {
        IClientProfile profile = DefaultProfile
            .getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
            .addEndpoint("cn-shanghai", "cn-shanghai", "Green", "");
        IAcsClient client = new DefaultAcsClient(profile);

        ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
        // Specify the response format of the operation.
        // Specify the request method.
        // Both HTTP and HTTPS are supported.

        JSONObject httpBody = new JSONObject();
         * Specify the moderation scenario.
         * ocr
        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 starts from when the request is initiated to when the last image is moderated.
         * Generally, 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.
         * The sample code uses a single image as an example. If you want 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://xxx.jpg");
        httpBody.put("tasks", Arrays.asList(task));

        imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);
         * You must set the connection timeout and read timeout. The timeout period for the server to complete an image moderation request is 10s.
         * If you set the read timeout to a value smaller than 10s, a read timeout error is returned.
        HttpResponse httpResponse = null;
        try {
            httpResponse = client.doAction(imageSyncScanRequest);
        } catch (ServerException e) {
        } catch (ClientException e) {
        } catch (Exception e){

        // The results 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()));
            int requestCode = scrResponse.getIntValue("code");
            // The moderation result of each image.
            JSONArray taskResults = scrResponse.getJSONArray("data");
            if (200 == requestCode) {
                for (Object taskResult : taskResults) {
                    // The moderation result of a single image.
                    int taskCode = ((JSONObject)taskResult).getIntValue("code");
                    // The moderation result of the image in a moderation scenario. If you have specified multiple moderation scenarios, 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 detected in the moderated image.
                            if("review" .equals(suggestion) && "ocr".equals(scene)){
                                JSONObject idCardInfo =  ((JSONObject) sceneResult).getJSONObject("idCardInfo");
                        // 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 {
                 * Your whole 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));