package org.jeecg.modules.system.util;

import com.huaweicloud.sdk.core.auth.ICredential;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Base64;

import javax.imageio.stream.FileImageInputStream;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.ocr.v1.region.OcrRegion;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import com.huaweicloud.sdk.ocr.v1.*;
import com.huaweicloud.sdk.ocr.v1.model.*;

/**
 * 华为文字识别
 * 
 * @author w
 *
 */
public class RecognizeGeneralTextSolution {

	public static final String CLOUD_SDK_AK = "X4MNF4GUFECVS2JKQS8L";
	public static final String CLOUD_SDK_SK = "7hMUh5lgdpPMUQQSRc6tM6MNsMPx1Cgsnrv1zE67";

	/**
	 * 识别文字
	 * 
	 * @param imgUrl
	 * @return
	 */
	public static String recognizeGeneralTextSolution(String imgUrl,String classifierId) {

		ICredential auth = new BasicCredentials()
                .withAk(CLOUD_SDK_AK)
                .withSk(CLOUD_SDK_SK)
                .withProjectId("7c9b49b201734164a4f1efef5e1e46e0");

        OcrClient client = OcrClient.newBuilder()
                .withCredential(auth)
                .withRegion(OcrRegion.valueOf("cn-north-4"))
                .build();
		
        RecognizeCustomTemplateRequest request = new RecognizeCustomTemplateRequest();
        CustomTemplateRequestBody body = new CustomTemplateRequestBody();
        body.setClassifierId(classifierId);
		body.setImage(readImageFile(imgUrl));
		request.withBody(body);
		try {
			RecognizeCustomTemplateResponse response = client.recognizeCustomTemplate(request);
			return response.toString();
		} catch (ConnectionException e) {
			e.printStackTrace();
		} catch (RequestTimeoutException e) {
			e.printStackTrace();
		} catch (ServiceResponseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String args[]) throws Exception {

		recognizeGeneralTextSolution("D:\\Download\\178171512_1099943536_259_194.jpg","b9b1de0e-6601-11ee-88fb-0255ac100240");
	}

	public static String readImageFile(String path) {
		String base64 = null;
		try {
			File imageFile = new File(path);
            FileInputStream fileInputStream = new FileInputStream(imageFile);
            byte[] imageBytes = new byte[(int) imageFile.length()];
            fileInputStream.read(imageBytes);
            fileInputStream.close();
            base64 = Base64.getEncoder().encodeToString(imageBytes);
		} catch (FileNotFoundException ex1) {
			ex1.printStackTrace();
		} catch (IOException ex1) {
			ex1.printStackTrace();
		}
		return base64;
	}
}