package com.hfkj.ocr;

import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.teaopenapi.models.Config;

/**
 * 阿里云识别
 * @author hurui
 */
public class AliYunOcrService {

    /**
     * 创建请求
     * @return
     * @throws Exception
     */
    private static Client createClient() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId("LTAI5tAu96cTs6b87vdfZivT")
                // 您的AccessKey Secret
                .setAccessKeySecret("67SmjGewt0zKGrhQeDKGbbk5lE3tAi");
        // 访问的域名
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new Client(config);
    }

    /**
     * 身份证识别
     * @param url 访问地址
     * @return
     */
    public static RecognizeIdcardResponseBody recognizeIdCard(String url) {
        try {
            Client client = createClient();
            RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest().setUrl(url);
            RecognizeIdcardResponse response = client.recognizeIdcard(recognizeIdcardRequest);
            return response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 银行卡识别
     * @param url 访问地址
     * @return
     */
    public static RecognizeBankCardResponseBody recognizeBankCard(String url) {
        try {
            Client client = createClient();
            RecognizeBankCardRequest recognizeBankCardRequest = new RecognizeBankCardRequest().setUrl(url);
            RecognizeBankCardResponse response = client.recognizeBankCard(recognizeBankCardRequest);
            return response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 银行开户许可证识别
     * @param url 访问地址
     * @return
     */
    public static RecognizeBankAccountLicenseResponseBody recognizeBankAccount(String url) {
        try {
            Client client = createClient();
            RecognizeBankAccountLicenseRequest recognizeBankAccountLicenseRequest = new RecognizeBankAccountLicenseRequest().setUrl(url);
            RecognizeBankAccountLicenseResponse response = client.recognizeBankAccountLicense(recognizeBankAccountLicenseRequest);
            return response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 营业执照识别
     * @param url 访问地址
     * @return
     */
    public static RecognizeBusinessLicenseResponseBody recognizeBusinessLicense(String url) {
        try {
            Client client = createClient();
            RecognizeBusinessLicenseRequest request = new RecognizeBusinessLicenseRequest().setUrl(url);
            RecognizeBusinessLicenseResponse response = client.recognizeBusinessLicense(request);
            return response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
