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

import java.util.HashMap;
public  class HealthCode {
    private String Name;
    private String Data;
    private String Status;

    public HealthCode() {
        Name = "";
        Data = "";
        Status = "";
    }

    public String getName() {
        return Name;
    }

    public String getData() {
        return Data;
    }

    public String getStatus() {
        return Status;
    }

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

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

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

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

    public static void main(String[] args) {
        String path = "C:/Users//16491/eclipse-workspace/OCR_baidu/src/1.jpg";
        System.out.println(Judge(path));
    }

    public static String Judge(String path) {
        HealthCode H = new HealthCode();
        H = ocr1(path);
        String StatusNameData = "S"+H.Status + "," + H.Name + "," + H.Data+"E";
        return StatusNameData;
    }


    /*class OCR {
        //设置APPID/AK/SK
        public OCR(){

        }*/
    public static HealthCode ocr1(String path) {
        final String APP_ID = "28049220";
        final String API_KEY = "qyKlUy6xSapA89DTPomExteq";
        final String SECRET_KEY = "q5EYpXULVh5gC3firLU1nGaymwIwOMBd";


        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

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

        // 可选：设置代理服务器地址, 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>());
        String str= res.toString(0);

        int flag = 0;
        int flag1 = 0;
        int flag2 = 0;
        HealthCode HC = new HealthCode();
        for (int i = 16; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '[' && flag == 0) {
                flag = 1;
            } else if (flag == 1 && c == ']') {
                break;
            } else {
                if (i == 27) {
                    String N = HC.getName();
                    for (; str.charAt(i) != '\"'; i++) {
                        N = N + str.charAt(i);

                    }
                    HC.setName(N);
                }


                if (str.charAt(i) == '-' && flag1 == 0) {
                    flag1 = 1;
                    String D = HC.getData();
                    for (i = i - 4; str.charAt(i) != '\"'; i++) {
                        D = D + str.charAt(i);
                    }
                    HC.setData(D);
                }
                if (str.charAt(i) == '码' && flag2 == 0) {
                    flag2 = 1;
                    String S = HC.getStatus();
                    S = S + str.charAt(i - 1);
                    HC.setStatus(S);
                }
            }
        }
        return HC;
    }
}