import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.util.HashMap;

public class CovidTestRep {
    private String Name;
    private String Time;
    private String Status;
    public CovidTestRep() {
        Name = "";
        Time = "";
        Status = "";
    }

    public String getName() {
        return Name;
    }

    public String getData() {
        return Time;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }

    public void setName(String name) {
        Name = name;
    }

    public void setData(String data) {
        Time = data;
    }

    public void Print() {
        System.out.println("Name:" + this.Name);
        System.out.println("Data:" + this.Time);
        System.out.println("Status:" + this.Status);
    }


    public static void main(String[] args) {
        String path = "C:/Users//16491/eclipse-workspace/OCR_baidu/src/3.jpg";// 初始化一个AipOcr
        ocr3(path);
    }
    public static String Judge(String path) {
        CovidTestRep Ctr=new CovidTestRep();
        Ctr=ocr3(path);
        String StatusNameTime=Ctr.Status+","+Ctr.Name+","+Ctr.Time;
        return StatusNameTime;
    }
    public static CovidTestRep ocr3(String path) {
        final String APP_ID = "28049220";
        final String API_KEY = "qyKlUy6xSapA89DTPomExteq";
        final String SECRET_KEY = "q5EYpXULVh5gC3firLU1nGaymwIwOMBd";
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(10000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        // client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        // client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
        /*Map<String,Object> m=res.toMap();
        Set s=m.entrySet();*//*

        for (Object str : s) {
            System.out.println(str);
        }
        //System.out.println(s);*/
        String str= res.toString(0);
        /*for(int i=0;i<str.length();i++)
        {char c=str.charAt(i);
            System.out.println(i);
        System.out.println(c);
        }*/
        //System.out.println(str);
        String s="";
        for(int i=0;i<str.length();i++){
            char c=str.charAt(i);
            if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='{'||c=='"'||c==':'||c=='}'||c=='：'||c=='_'){
                continue;
            }
            else
                s+=c;
            if(c==']')break;
        }
        //System.out.println(s);
        CovidTestRep CTR=new CovidTestRep();
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)=='姓'&&s.charAt(i+1)=='名')
            {
                String Na= CTR.getName();
                i=i+3;
                for(;s.charAt(i)!=',';i++)
                {
                    Na+=s.charAt(i);
                }
                CTR.setName(Na);
            }
            if(s.charAt(i)=='采'&&s.charAt(i+1)=='样'&&s.charAt(i+2)=='时'&&s.charAt(i+3)=='间')
            {
                String Da= CTR.getData();
                i=i+5;
                for(;s.charAt(i)!=',';i++)
                {
                    Da+=s.charAt(i);
                }
                CTR.setData(Da);
            }
            if(s.charAt(i)=='检'&&s.charAt(i+1)=='测'&&s.charAt(i+2)=='结'&&s.charAt(i+3)=='果')
            {
                String St= CTR.getStatus();
                i=i+5;
                for(;s.charAt(i)!=']';i++)
                {
                    St+=s.charAt(i);
                }
                CTR.setStatus(St);
            }
        }
        return CTR;
    }
}
