package com.ag.baiduapi.util;

import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONUtil;
import com.ag.baiduapi.pojo.ocr.invoice.OcrInvoiceRootData;
import com.ag.baiduapi.pojo.ocr.normal.OcrNormalData;
import com.ag.baiduapi.service.OcrServicce;
import com.ag.util.PropertyUtil;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.HashMap;

public class AipOcrUtil {


    //设置APPID/AK/SK 实际项目使用需替换为真实值
    public static final String APP_ID = PropertyUtil.getProperty("BAIDU_API_APP_ID");
    public static final String API_KEY = PropertyUtil.getProperty("BAIDU_API_API_KEY");
    public static final String SECRET_KEY = PropertyUtil.getProperty("BAIDU_API_SECRET_KEY");

    /**
     * 类级的内部类，也就是静态的成员式内部类，该内部类的实例与外部类的实例
     * 没有绑定关系，而且只有被调用到才会装载，从而实现了延迟加载
     */
    private static class AipOcrHolder{
        /**
         * 静态初始化器，由JVM来保证线程安全
         */
        private static AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
    }
    /**
     * 私有化构造方法
     */
    private void AipOcr(){

    }

    public static  AipOcr getInstance(){
        AipOcr aipOcr= AipOcrHolder.client;
        aipOcr.setConnectionTimeoutInMillis(2000);
        aipOcr.setSocketTimeoutInMillis(60000);

        return aipOcr;
    }

    /**
     * 识别图片（普通识别）
     * @param url 图片网络地址
     * @return
     */
    public static OcrNormalData ocrNormalUrlImage(String url){
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");//是否检测图像朝向
        options.put("detect_language", "true");//	是否检测语言
        options.put("probability", "true");//是否返回识别结果中每一行的置信度

        AipOcr aipOcr=AipOcrUtil.getInstance();
        JSONObject res =aipOcr.basicGeneralUrl(url, options);

        OcrNormalData ocrData= JSONUtil.toBean(res.toString(), OcrNormalData.class);

        return ocrData;

    }


    /**
     * 识别图片（高精度）
     * @param inputStream 流
     * @return
     */
    public static OcrNormalData ocrNormalStreamImage (InputStream inputStream){
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");//是否检测图像朝向
        options.put("detect_language", "true");//	是否检测语言
        options.put("probability", "false");//是否返回识别结果中每一行的置信度

        //输入流转为byte[]
        byte[] test = IoUtil.readBytes(inputStream);

        AipOcr aipOcr= AipOcrUtil.getInstance();
        JSONObject res = aipOcr.basicAccurateGeneral(test, options);

        OcrNormalData ocrData= JSONUtil.toBean(res.toString(), OcrNormalData.class);

        return ocrData;
    }


    /**
     * 本地发票PDF识别
     * @param pdfPath
     * @return
     */
    public static OcrInvoiceRootData ocrLocalInvoicePdf(String pdfPath){
        String result=new OcrServicce().ocrLocalInvoicePdf(pdfPath);
        OcrInvoiceRootData ocrData= JSONUtil.toBean(result, OcrInvoiceRootData.class);
        return  ocrData;
    }


    /**
     * 本地发票PDF识别返回Json
     * @param pdfPath
     * @return
     */
    public static String ocrLocalInvoicePdfStr(String pdfPath){
        String result=new OcrServicce().ocrLocalInvoicePdf(pdfPath);
        return  result;
    }


    /**
     * 本地发票图片识别
     * @param imagePath
     * @return
     */
    public static OcrInvoiceRootData ocrLocalInvoiceImage(String imagePath){
        String result=new OcrServicce().ocrInvoiceImage(imagePath,"LOC");
        OcrInvoiceRootData ocrData= JSONUtil.toBean(result, OcrInvoiceRootData.class);
        return  ocrData;
    }


    /**
     * 网络发票图片识别
     * @param imagePath
     * @return
     */
    public static OcrInvoiceRootData ocrUrlInvoiceImage(String imagePath){
        String result=new OcrServicce().ocrInvoiceImage(imagePath,"URL");
        OcrInvoiceRootData ocrData= JSONUtil.toBean(result, OcrInvoiceRootData.class);
        return  ocrData;
    }




    /**
     * 本地发票图片识别返回JSON
     * @param imagePath
     * @return
     */
    public static String ocrLocalInvoiceImageStr(String imagePath){
        String result=new OcrServicce().ocrInvoiceImage(imagePath,"LOC");
        return  result;
    }


    /**
     * 网络发票图片识别返回JSON
     * @param imagePath
     * @return
     */
    public static String ocrUrlInvoiceImageStr(String imagePath){
        String result=new OcrServicce().ocrInvoiceImage(imagePath,"URL");
        return  result;
    }


    /**
     * 正面身份证本地图片识别
     * @param imagePath
     * @return
     */
    public static String oclLocalIdCardImageFrontStr(String imagePath){
        String result=new OcrServicce().ocrLocalIdCardImage("F",imagePath);
        return  result;
    }

    /**
     * 反面身份证本地图片识别
     * @param imagePath
     * @return
     */
    public static String oclLocalIdCardImageBackStr(String imagePath){
        String result=new OcrServicce().ocrLocalIdCardImage("B",imagePath);
        return  result;
    }


    public static  void main(String[] args) {
        OcrInvoiceRootData data=AipOcrUtil.ocrLocalInvoicePdf("C:\\Users\\jean\\Downloads\\47329012.pdf");
        System.out.println("=========="+JSONUtil.toJsonStr(data));
    }

}
