package com.ag.baiduapi.service;

import com.ag.baiduapi.util.AipOcrUtil;
import com.baidu.aip.contentcensor.EImgType;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Author jean
 * @Date 2022/4/19 10:33
 * @Version 1.0
 */
@Service
public class OcrServicce {

    /**
     * 识别本地发票PDF
     * @param pdfPath 本地pdf地址
     * @return
     */
    public String ocrLocalInvoicePdf(String pdfPath){
        HashMap<String, String> options = new HashMap<String, String>();
        AipOcr aipOcr= AipOcrUtil.getInstance();
        JSONObject res = aipOcr.vatInvoice(pdfPath, EImgType.PDF, options);
        return res.toString();
    }


    /**
     * 识别发票图片
     * @param imagePath 图片地址
     * @param imageType LOC本地图片  URL网络图片
     * @return
     */
    public String ocrInvoiceImage(String imagePath,String imageType){
        HashMap<String, String> options = new HashMap<String, String>();
        AipOcr aipOcr= AipOcrUtil.getInstance();
        JSONObject res = aipOcr.vatInvoice(imagePath,"URL".equalsIgnoreCase(imageType)?EImgType.URL:EImgType.FILE, options);
        return res.toString();
    }

    /**
     * 识别身份证图片
     * @param idCardSite 身份证正反 F正 B反
     * @param imagePath 本地图片地址
     * @return
     */
    public String ocrLocalIdCardImage(String idCardSite,String imagePath){
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("detect_risk", "false");
        AipOcr aipOcr= AipOcrUtil.getInstance();
        String idCardSide ="F".equalsIgnoreCase(idCardSite)?"front":"back";
        JSONObject res =aipOcr.idcard(imagePath, idCardSide, options);
        return res.toString();
    }
}
