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

import java.util.HashMap;
public class TripCode {
    private String Status;
    private String Telephone;
    private String Time;
    private String District;

    public TripCode() {
        Status = "";
        Telephone = "";
        Time = "";
        District = "";
    }

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

    public void setTelephone(String telephone) {
        Telephone = telephone;
    }

    public void setTime(String time) {
        Time = time;
    }

    public void setDistrict(String district) {
        District = district;
    }

    public String getTelephone() {
        return Telephone;
    }

    public String getStatus() {
        return Status;
    }

    public String getDistrict() {
        return District;
    }

    public String getTime() {
        return Time;
    }

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

    public static void main(String[] args) {
        String path = "C:\\Users\\16491\\Desktop\\1.jpg";
        System.out.println(Judge(path));
    }

    /*};
    class OCR2 {
        //设置APPID/AK/SK*/
    public static String Judge(String path) {
        TripCode T = new TripCode();
        T = ocr2(path);
        String InfM = T.Status + "," + T.Telephone + "," + T.Time + "," + T.District;
        return InfM;
    }

    public static TripCode ocr2(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(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>());
        //System.out.println(res);
        String str = res.toString(0);
        //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);
        TripCode TC = new TripCode();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '的' && s.charAt(i + 1) == '动' && s.charAt(i + 2) == '态' && s.charAt(i + 3) == '行' && s.charAt(i + 4) == '程' && s.charAt(i + 5) == '卡') {
                String tel = TC.getTelephone();
                for(;s.charAt(i)!=',';i--);
                i++;
                for (; s.charAt(i) != ','; i++) {
                    tel += s.charAt(i);
                }
                TC.setTelephone(tel);
            }
            if (s.charAt(i) == '请' && s.charAt(i + 1) == '收' && s.charAt(i + 2) == '下') {
                String St = TC.getStatus();
                i = i + 3;
                for (; s.charAt(i) != ','; i++) {
                    St += s.charAt(i);
                }
                TC.setStatus(St);
            }
            if (s.charAt(i) == '更' && s.charAt(i + 1) == '新' && s.charAt(i + 2) == '于') {
                String Data = TC.getTime();
                i = i + 3;
                for (; s.charAt(i) != ','; i++) {
                    Data += s.charAt(i);
                }
                TC.setTime(Data);
            }
            if (s.charAt(i) == '到' && s.charAt(i + 1) == '达' && s.charAt(i + 2) == '或' && s.charAt(i + 3) == '途' && s.charAt(i + 4) == '经') {
                String Dis = TC.getDistrict();
                i = i + 5;
                for (; s.charAt(i) != ']'; i++) {
                    Dis += s.charAt(i);
                }
                TC.setDistrict(Dis);
            }
        }
        return TC;
    }
}
