阿里云OCR:(二)银行卡号识别

印刷文字识别-银行卡号识别

阿里云文档地址

阿里云提供的银行卡接口,API文档详细,方便快捷。现在阿里云提供有测试套餐,0元500次进行体验。此API只能拿到银行卡号。

银行卡号识别Demo

package com.rzg.dgztc.util;

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 static org.apache.commons.codec.binary.Base64.encodeBase64;

/**
 * Description: 阿里云orc--银行卡信息识别
 *
 * @author: banjingwei
 * @email: [email protected]
 * @date: 2019/5/10 18:35
 * @Copyright: 2019-2018 dgztc Inc. All rights reserved.
 */
public class BankcardDemo {

    public static void main(String[] args) {
        String host = "https://yhk.market.alicloudapi.com";
        String path = "/rest/160601/ocr/ocr_bank_card.json";
        String method = "POST";
        //购买后分配的code
        String appcode = "d5a9608a55a4430481a6ee2ec34c9dd8";
        //图片地址
        String imgFile = "C:\\Users\\banjw\\Desktop\\1557485211681.jpg";
        Map headers = new HashMap();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求,定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map querys = new HashMap<>();
        // 对图像进行base64编码
        String imgBase64;
        try {
            File file = new File(imgFile);
            byte[] content = new byte[(int) file.length()];
            FileInputStream finputstream = new FileInputStream(file);
            finputstream.read(content);
            finputstream.close();
            imgBase64 = new String(encodeBase64(content));
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        // 拼装请求body的json字符串
        JSONObject requestObj = new JSONObject();
        System.out.println("图片:"+imgBase64);
        requestObj.put("image", imgBase64);
        String bodys = requestObj.toString();
        System.out.println("请求体:"+bodys);
        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);
            String res = EntityUtils.toString(response.getEntity());
            JSONObject res_obj = JSON.parseObject(res);
            System.out.println(res_obj.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

HttpUtils工具类可以根据注释提示进行下载,也可以查看上篇身份证识别博文。

你可能感兴趣的:(Java)