站长微信:Name-Tornado 大家一起学习 共同进步 每赚网-稳定更新互联网项目 永久地址 mzw.ljf666.com 项目免费送 收藏不迷路

阿里云身份证图片识别文字

557次阅读
没有评论

About Me

欢迎各位读者访问,大家一起学习。

优秀是一种习惯♡♡♡

做更好的自己!

本人见识有限,写到博客难免有错误或疏忽的地方,还望各位大佬多多指点,在此表示感激不尽。♡♡♡


阿里云身份证图片识别文字

需求: 身份证识别文字[包括地址,姓名,年龄等........]

1. 阿里云购买服务:

阿里云身份证识别服务0.01可以购买500次,个人测试还是够用了!

身份证识别图片服务地址:https://market.aliyun.com/products/57124001/cmapi010401.html?spm=a2c4g.11186623.0.0.189960cdzLRF4S#sku=yuncode440100000

阿里云身份证图片识别文字

2. java代码实现:

第一步中链接有阿里云详细的请求路径可以查看,这边只放java实现相关代码:

package com.alibaba.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;

public class ocr_idcard {
    public static String img_base64(String path) {
        /**
         *  对path进行判断,如果是本地文件就二进制读取并base64编码,如果是url,则返回
         */
        String imgBase64="";
        if (path.startsWith("http")){
            imgBase64 = path;
        }else {
            try {
                File file = new File(path);
                byte[] content = new byte[(int) file.length()];
                FileInputStream finputstream = new FileInputStream(file);
                finputstream.read(content);
                finputstream.close();
                imgBase64 = new String(Base64.encodeBase64(content));
            } catch (IOException e) {
                e.printStackTrace();
                return imgBase64;
            }
        }

        return imgBase64;
    }

    public static void main(String[] args) {
        String host = "http://dm-51.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_idcard.json";
        String appcode = "你的appcode";
        String imgFile = "本地图片路径或者图片的url";
        String method = "POST";

        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
      //根据API的要求,定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");

        Map<String, String> querys = new HashMap<String, String>();
        // 对图像进行base64编码
        String imgBase64 = img_base64(imgFile);   

        //configure配置
        JSONObject configObj = new JSONObject();
        configObj.put("side", "face");

        String config_str = configObj.toString();

        // 拼装请求body的json字符串
        JSONObject requestObj = new JSONObject();
        requestObj.put("image", imgBase64);
        if(configObj.size() > 0) {
            requestObj.put("configure", config_str);
        }
        String bodys = requestObj.toString();

        try {
            /**
                     * 重要提示如下:
              * HttpUtils请从
              * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
                     * 下载
             *
                      * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            int stat = response.getStatusLine().getStatusCode();
            if(stat != 200){
                System.out.println("Http code: " + stat);
                System.out.println("http header error msg: "+ response.getFirstHeader("X-Ca-Error-Message"));
                System.out.println("Http body error msg:" + EntityUtils.toString(response.getEntity()));
                return;
            }

            String res = EntityUtils.toString(response.getEntity());
            JSONObject res_obj = JSON.parseObject(res);

            System.out.println(res_obj.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

阿里云身份证图片识别文字

3. 效果测试:

阿里云身份证图片识别文字

                                                      不要在最能吃苦的年纪选择了安逸!!!        --- Tornado♥

正文完
 
Tornado
版权声明:本站原创文章,由 Tornado 2022-03-17发表,共计3142字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码