文档

图片OCR识别

更新时间:

本文介绍了如何使用Java SDK图片OCR接口,识别图片中的文字或卡证信息。

功能描述

通用OCR除了能够识别普通图片中的文字,还能识别结构化的卡证上的文字。关于参数的详细说明,请参见图片OCR检测API文档

前提条件

  • 安装Java依赖。关于安装Java依赖的具体操作,请参见安装Java依赖

    说明

    请一定按照安装Java依赖页面中的版本安装,否则会导致调用失败。

  • 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类

提交图片同步检测任务

接口

描述

支持的Region

ImageSyncScanRequest

提交图片OCR同步识别任务,对图片中的文字进行识别(scene=ocr)。

  • cn-shanghai

  • cn-beijing

  • cn-shenzhen

  • ap-southeast-1

示例代码

  • 传图片URL进行检测

    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 {
            /**
             * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
             * 常见获取环境变量方式:
             * 方式一:
             *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             * 方式二:
             *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
            IAcsClient client = new DefaultAcsClient(profile);
    
            ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
            // 指定返回格式。
            imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
            // 指定请求方法。
            imageSyncScanRequest.setMethod(MethodType.POST);
            imageSyncScanRequest.setEncoding("utf-8");
            // 支持HTTP和HTTPS。
            imageSyncScanRequest.setProtocol(ProtocolType.HTTP);
    
            JSONObject httpBody = new JSONObject();
            /**
             * 设置要检测的场景。
             * ocr:表示OCR图文识别和OCR卡证识别。
             */
            httpBody.put("scenes", Arrays.asList("ocr"));
    
            /**
             * 设置待检测的图片,一张图片对应一个检测任务。
             * 多张图片同时检测时,处理时间由最后一张处理完的图片决定。
             * 通常情况下批量检测的平均响应时间比单任务检测长,一次批量提交的图片数越多,响应时间被拉长的概率越高。
             * 代码中以单张图片检测作为示例,如果需要批量检测多张图片,请自行构建多个任务。
             */
            JSONObject task = new JSONObject();
            task.put("dataId", UUID.randomUUID().toString());
    
            // 设置图片链接。
            task.put("url", "https://example.com/xxx.jpg");
            task.put("time", new Date());
            httpBody.put("tasks", Arrays.asList(task));
    
            // 需要OCR卡证识别时,设置卡证类型。
            JSONObject cardExtras = new JSONObject();
            // 身份证正面识别。
            cardExtras.put("card", "id-card-front");
            // 身份证反面。
            //cardExtras.put("card", "id-card-back");
            httpBody.put("extras", cardExtras);
            imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);
            /**
             * 请设置超时时间,服务端全链路处理超时时间为10秒,请据此做相应设置。
             * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout异常。
             */
            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();
            }
    
            // 服务端接收到请求,并完成处理返回的结果。
            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");
                // 每一张图片的检测结果。
                JSONArray taskResults = scrResponse.getJSONArray("data");
                if (200 == requestCode) {
                    for (Object taskResult : taskResults) {
                        // 单张图片的处理结果。
                        int taskCode = ((JSONObject)taskResult).getIntValue("code");
                        // 对应检测场景下图片的处理结果。如果是多个场景,则会有每个场景的结果。
                        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
                                // 识别出来的身份证信息。
                                if("review" .equals(suggestion) && "ocr".equals(scene)){
                                    JSONObject idCardInfo =  ((JSONObject) sceneResult).getJSONObject("idCardInfo");
                                    System.out.println(idCardInfo.toJSONString());
                                }
                            }
                        }else{
                            // 单张图片处理失败,原因视具体的情况详细分析。
                            System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                         }
                    }
                } else {
                    /**
                     * 表明请求整体处理失败,原因视具体的情况详细分析。
                     */
                    System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
                 }
            }
        }
    }
  • 传本地图片文件进行检测

    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.extension.uploader.ClientUploader;
    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 {
    /**
             * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
             * 常见获取环境变量方式:
             * 方式一:
             *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             * 方式二:
             *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
            IAcsClient client = new DefaultAcsClient(profile);
    
            ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
            // 指定返回格式。
            imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
            // 指定请求方法。
            imageSyncScanRequest.setMethod(MethodType.POST);
            imageSyncScanRequest.setEncoding("utf-8");
            // 支持HTTP和HTTPS。
            imageSyncScanRequest.setProtocol(ProtocolType.HTTP);
    
            JSONObject httpBody = new JSONObject();
            /**
             * 设置要检测的场景。
             * ocr:表示OCR图文识别和OCR卡证识别。
             */
            httpBody.put("scenes", Arrays.asList("ocr"));
    
            /**
             * 设置待检测的图片,一张图片对应一个检测任务。
             * 多张图片同时检测时,处理时间由最后一张处理完的图片决定。
             * 通常情况下批量检测的平均响应时间比单任务检测长,一次批量提交的图片数越多,响应时间被拉长的概率越高。
             * 代码中以单张图片检测作为示例,如果需要批量检测多张图片,请自行构建多个任务。
             * 本地图片相对于互联网图片链接,多了一个上传步骤,上传后取返回的链接进行检测。
             */
            ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false);
            String url = null;
            try{
                url = clientUploader.uploadFile("/Users/test/Pictures/idfront-data-1536747400633.jpg");
            }catch (Exception e){
                System.out.println("upload file to server fail."); 
            }
            JSONObject task = new JSONObject();
            task.put("dataId", UUID.randomUUID().toString());
            task.put("url", url);
            task.put("time", new Date());
            httpBody.put("tasks", Arrays.asList(task));
    
            // 使用OCR卡证识别时,设置要识别的卡证类型。
            JSONObject cardExtras = new JSONObject();
            // 身份证正面。
            cardExtras.put("card", "id-card-front");
            // 身份证反面。
            //cardExtras.put("card", "id-card-back");
            httpBody.put("extras", cardExtras);
    
            imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);
            /**
             * 请设置超时时间。服务端全链路处理超时时间为10秒,请据此做相应设置。
             * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout异常。
             */
            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();
            }
    
            // 服务端接收到请求,并完成处理返回的结果。
            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");
                // 每一张图片的检测结果。
                JSONArray taskResults = scrResponse.getJSONArray("data");
                if (200 == requestCode) {
                    for (Object taskResult : taskResults) {
                        // 单张图片的处理结果。
                        int taskCode = ((JSONObject)taskResult).getIntValue("code");
                        // 对应检测场景下,图片的处理结果。如果是多个场景,则会有每个场景的结果。
                        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
                                // 识别出来的卡证信息。
                                if("review" .equals(suggestion) && "ocr".equals(scene)){
                                    JSONObject idCardInfo =  ((JSONObject) sceneResult).getJSONObject("idCardInfo");
                                    System.out.println(idCardInfo.toJSONString());
                                }
                            }
                        }else{
                            // 单张图片处理失败,原因视具体的情况详细分析。
                            System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                        }
                    }
                } else {
                    /**
                     * 表明请求整体处理失败,原因视具体的情况详细分析。
                     */
                    System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
                }
            }
        }
    }
  • 传图片二进制字节组数据进行检测

    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.extension.uploader.ClientUploader;
    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 org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
           /**
             * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
             * 常见获取环境变量方式:
             * 方式一:
             *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             * 方式二:
             *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
             *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
             */
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-shanghai",
                    "建议从环境变量中获取RAM用户AccessKey ID",
                    "建议从环境变量中获取RAM用户AccessKey Secret");
            DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
            IAcsClient client = new DefaultAcsClient(profile);
    
            ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
            // 指定返回格式。
            imageSyncScanRequest.setAcceptFormat(FormatType.JSON);
            // 指定请求方法。
            imageSyncScanRequest.setMethod(MethodType.POST);
            imageSyncScanRequest.setEncoding("utf-8");
            // 支持HTTP和HTTPS。
            imageSyncScanRequest.setProtocol(ProtocolType.HTTP);
    
            JSONObject httpBody = new JSONObject();
            /**
             * 设置要检测的场景。
             * ocr:表示OCR图文识别和OCR卡证识别。
             */
            httpBody.put("scenes", Arrays.asList("ocr"));
            /**
             * 设置待检测的图片,一张图片对应一个检测任务。
             * 多张图片同时检测时,处理时间由最后一张处理完的图片决定。
             * 通常情况下批量检测的平均响应时间比单任务检测长,一次批量提交的图片数越多,响应时间被拉长的概率越高。
             * 代码中以单张图片检测作为示例,如果需要批量检测多张图片,请自行构建多个任务。
             * 图片二进制数据检测相对于互联网图片链接,多了一个上传步骤,上传后取返回的链接进行检测。
             */
            ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false);
            byte[] imageBytes = null;
            String url = null;
            try{
                // 读取本地文件作为二进制数据,做为输入示例。实际使用中请直接替换成您的图片二进制数据。
                imageBytes = FileUtils.readFileToByteArray(new File("/Users/01fb4ab6420b5f34623e13b82b51ef87.jpg"));
                // 上传到服务端。
                url = clientUploader.uploadBytes(imageBytes);
            }catch (Exception e){
                System.out.println(("upload file to server fail.");
            }
    
            JSONObject task = new JSONObject();
            task.put("dataId", UUID.randomUUID().toString());
            task.put("url", url);
            task.put("time", new Date());
            httpBody.put("tasks", Arrays.asList(task));
    
            // 使用OCR卡证识别时,设置要识别的卡证类型。
            JSONObject cardExtras = new JSONObject();
            // 身份证正面。
            cardExtras.put("card", "id-card-front");
            // 身份证反面。
            //cardExtras.put("card", "id-card-back");
            httpBody.put("extras", cardExtras);
            imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);
            /**
             * 请设置超时时间。服务端全链路处理超时时间为10秒,请据此做相应设置。
             * 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout异常。
             */
            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();
            }
    
            // 服务端接收到请求,并完成处理返回的结果。
            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");
                // 每一张图片的检测结果。
                JSONArray taskResults = scrResponse.getJSONArray("data");
                if (200 == requestCode) {
                    for (Object taskResult : taskResults) {
                        // 单张图片的处理结果。
                        int taskCode = ((JSONObject)taskResult).getIntValue("code");
                        // 对应检测场景下,图片的处理结果。如果是多个场景,则会有每个场景的结果。
                        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
                                // 识别出来的卡证信息。
                                if("review" .equals(suggestion) && "ocr".equals(scene)){
                                    JSONObject idCardInfo =  ((JSONObject) sceneResult).getJSONObject("idCardInfo");
                                    System.out.println(idCardInfo.toJSONString());
                                }
                            }
                        }else{
                            // 单张图片处理失败,原因视具体的情况详细分析。
                            System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));
                        }
                    }
                } else {
                    /**
                     * 表明请求整体处理失败,原因视具体的情况详细分析。
                     */
                    System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));
                }
            }
        }
    }

  • 本页导读 (0)
文档反馈