package cn.hmsoft.ai.idcard;

import java.io.File;

import cn.hmsoft.ai.idcard.baidu.BaiduIdCardOcr;
import cn.hmsoft.ai.util.AiParamUtil;

/***************************
 * 身份证识别
 * 
 * @author zxq
 * @create 2019-08-22 22:33:53
 * @version 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public abstract class IdCardOcr {

	private static IdCardOcr ocr = null;

	public static IdCardOcr instance() {
		if (ocr == null) {
			synchronized (IdCardOcr.class) {
				try {
					ocr = (IdCardOcr) Class.forName(AiParamUtil.IdCardOcrClass).newInstance();
				} catch (Exception e) {
					try {
						ocr = BaiduIdCardOcr.class.newInstance();
					} catch (Exception ex) {

					}
				}
			}
		}
		return ocr;
	}

	public IdCard ocrIdCard(String idcard_image_path) {
		IdCard card = null;
		try {
			card = this.ocrIdCardByFile(new File(idcard_image_path));
		} catch (Exception e) {

		}
		if (card == null) {
			card = new IdCard();
			card.setError_msg("unknow error");
		}
		return card;
	}

	public IdCard ocrIdCard(File idcard_image_file) {
		return ocrIdCard(idcard_image_file.getPath());
	}

	protected abstract IdCard ocrIdCardByFile(File file);
	
	public IdCardBack ocrIdCardBack(String idcard_image_path) {
		IdCardBack card = null;
		try {
			card = this.ocrIdCardBackByFile(new File(idcard_image_path));
		} catch (Exception e) {

		}
		if (card == null) {
			card = new IdCardBack();
			card.setError_msg("unknow error");
		}
		return card;
	}
	
	public IdCardBack ocrIdCardBack(File idcard_image_file) {
		return ocrIdCardBack(idcard_image_file.getPath());
	}

	
	protected abstract IdCardBack ocrIdCardBackByFile(File file);
}
