/**
* Copyright 2018 Huawei Technologies Co.,Ltd.
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License.  You may obtain a copy of the
* License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations under the License.
*
* 
* sdk reference linking：https://support.huaweicloud.com/sdkreference-ocr/ocr_04_0001.html
**/

package OCRDemo;
import com.huawei.ais.demo.ocr.*;
import com.huawei.ais.demo.items.*;
import java.io.IOException;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.commons.io.IOUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class OCRDemo {
	public static void main(String[] args) throws URISyntaxException, UnsupportedOperationException, IOException{
		TokenDemo();
		//AKSKDemo();
		
	}
	public static void TokenDemo() throws URISyntaxException, UnsupportedOperationException, IOException{
		/*
		 * Token demo code
		 * */
		String username="xxx";
		String password="xxx";
		String domainName="xxx"; //If the current user is not an IAM user, the domainName is the same as the userName.
		String regionName="cn-north-4";
		String httpUri = "/v1.0/ocr/id-card";
		String imgPath = "./data/id-card-demo.jpg"; //File path or URL of the image to be recognized.
		
        // Set params except image
		String sideKey = "side";
		String sideValue = "";
		JSONObject params = new JSONObject();
		//params.put(sideKey, sideValue);
		try {
			HWOcrClientToken ocrClient= new HWOcrClientToken(domainName, username, password, regionName);
			HttpResponse response=ocrClient.RequestOcrServiceBase64(httpUri, imgPath, params);
			System.out.println(response);
			String content = IOUtils.toString(response.getEntity().getContent(), "utf-8");
			System.out.println(content); 
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void AKSKDemo() throws URISyntaxException, UnsupportedOperationException, IOException{
		/*
		 * AK/SK demo code
		 * */
		String AK="xxx"; //AK from authentication
		String SK="xxx"; //SK from authentication
		String regionName="cn-north-4"; //region name of the service
		String httpUri = "/v1.0/ocr/id-card";
		String imgPath = "./data/id-card-demo.jpg"; //File path or URL of the image to be recognized.
		
        // Set params except image
		String sideKey = "side";
		String sideValue = "";
		JSONObject params = new JSONObject();
		//params.put(sideKey, sideValue);

		try {
			HWOcrClientAKSK ocrClient=new HWOcrClientAKSK(regionName, AK, SK);
			HttpResponse response=ocrClient.RequestOcrServiceBase64(httpUri, imgPath, params);
			System.out.println(response);
			String content = IOUtils.toString(response.getEntity().getContent(), "utf-8");
			System.out.println(content);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
