Youtu.java 30.3 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
package com.youtu;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import com.youtu.sign.Base64Util;
import com.youtu.sign.YoutuSign;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


/**
 * 
 * @author tyronetao
 */
public class Youtu {
	
	private static class TrustAnyTrustManager implements X509TrustManager {
		
		public void checkClientTrusted(X509Certificate[] chain, String authType)
		throws CertificateException {
		}
		
		public void checkServerTrusted(X509Certificate[] chain, String authType)
		throws CertificateException {
		}
		
		public X509Certificate[] getAcceptedIssuers() {
			return new X509Certificate[] {};
		}
	}
	
	private static class TrustAnyHostnameVerifier implements HostnameVerifier {
		public boolean verify(String hostname, SSLSession session) {
			return true;
		}
	}

	public final static String API_YOUTU_END_POINT = "http://api.youtu.qq.com/youtu/";
	public final static String API_TENCENTYUN_END_POINT = "https://youtu.api.qcloud.com/youtu/";

	public final static String API_VIP_END_POINT = "https://vip-api.youtu.qq.com/youtu/";

	// 30 days
	private static int EXPIRED_SECONDS = 2592000;
	private String m_appid;
	private String m_secret_id;
	private String m_secret_key;
	private String m_end_point;
	private boolean m_use_https;
	
	/**
	 * Youtu 构造方法
	 * 
	 * @param appid
	 *            授权appid
	 * @param secret_id
	 *            授权secret_id
	 * @param secret_key
	 *            授权secret_key
	 */
	public Youtu(String appid, String secret_id, String secret_key,String end_point) {
		m_appid = appid;
		m_secret_id = secret_id;
		m_secret_key = secret_key;
		m_end_point= end_point;
		m_use_https = end_point.startsWith("https");
	}
	
	public String StatusText(int status) {
		
		String statusText = "UNKOWN";

        switch (status)
        {
        	case 0:
                statusText = "CONNECT_FAIL";
                break;
            case 200:
                statusText = "HTTP OK";
                break;
            case 400:
                statusText = "BAD_REQUEST";
                break;
            case 401:
                statusText = "UNAUTHORIZED";
                break;
            case 403:
                statusText = "FORBIDDEN";
                break;
            case 404:
                statusText = "NOTFOUND";
                break;
            case 411:
                statusText = "REQ_NOLENGTH";
                break;
            case 423:
                statusText = "SERVER_NOTFOUND";
                break;
            case 424:
                statusText = "METHOD_NOTFOUND";
                break;
            case 425:
                statusText = "REQUEST_OVERFLOW";
                break;
            case 500:
                statusText = "INTERNAL_SERVER_ERROR";
                break;
            case 503:
                statusText = "SERVICE_UNAVAILABLE";
                break;
            case 504:
                statusText = "GATEWAY_TIME_OUT";
                break;
        }
        return statusText;		
	}
	

//	private void GetBase64FromFile(String filePath, StringBuffer base64)
//	throws IOException {
//		File imageFile = new File(filePath);
//		if (imageFile.exists()) {
//			InputStream in = new FileInputStream(imageFile);
//			byte data[] = new byte[(int) imageFile.length()]; // 创建合适文件大小的数组
//			in.read(data); // 读取文件中的内容到b[]数组
//			in.close();
//			base64.append(Base64Util.encode(data));
//
//		} else {
//			throw new FileNotFoundException(filePath + " not exist");
//		}
//
//	}
	private void GetBase64FromInputStream(InputStream is, StringBuffer base64) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(is));
		String line = "";
		StringBuffer data = new StringBuffer("");
		while ((line = in.readLine()) != null) {
			data.append(line);
		}
		base64.append(Base64Util.encode(data.toString().getBytes()));
	}

	/**
	 * bitmap转为base64
	 * @param bitmap
	 * @return
	 */
	public static String bitmapToBase64(Bitmap bitmap) throws IOException {

		String result = null;
		ByteArrayOutputStream baos = null;
		try {
			if (bitmap != null) {
				baos = new ByteArrayOutputStream();
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

				baos.flush();
				baos.close();

				byte[] bitmapBytes = baos.toByteArray();
				result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
			}
		} catch (IOException e) {
			throw e;
		} finally {
			try {
				if (baos != null) {
					baos.flush();
					baos.close();
				}
			} catch (IOException e) {
				throw e;
			}
		}
		return result;
	}

	/**
	 * base64转为bitmap
	 * @param base64Data
	 * @return
	 */
	public static Bitmap base64ToBitmap(String base64Data) {
		byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
		return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
	}

	
	private JSONObject SendHttpRequest(JSONObject postData, String mothod)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		StringBuffer mySign = new StringBuffer("");
		YoutuSign.appSign(m_appid, m_secret_id, m_secret_key,
			System.currentTimeMillis() / 1000 + EXPIRED_SECONDS,
			"", mySign);

		System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
		System.setProperty("sun.net.client.defaultReadTimeout", "30000");
		URL url = new URL(m_end_point + mothod);

		HttpURLConnection connection = (HttpURLConnection) url.openConnection();

		// set header
		connection.setRequestMethod("POST");
		connection.setRequestProperty("accept", "*/*");
//		connection.setRequestProperty("Host", "api.youtu.qq.com");
		connection.setRequestProperty("user-agent", "youtu-android-sdk");
		connection.setRequestProperty("Authorization", mySign.toString());

		connection.setConnectTimeout(10000);
		connection.setReadTimeout(15000);
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestProperty("Content-Type", "text/json");
		connection.connect();

		// POST请求
		DataOutputStream out = new DataOutputStream(
			connection.getOutputStream());

		postData.put("app_id", m_appid);
		out.write(postData.toString().getBytes("utf-8"));
		//out.writeBytes(postData.toString());
		out.flush();
		out.close();
		// 读取响应
		InputStream isss = connection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				isss));
		String lines;
		StringBuffer resposeBuffer = new StringBuffer("");
		while ((lines = reader.readLine()) != null) {
			lines = new String(lines.getBytes(), "utf-8");
			resposeBuffer.append(lines);
		}
		// System.out.println(resposeBuffer+"\n");
		reader.close();
		// 断开连接
		connection.disconnect();

		JSONObject respose = new JSONObject(resposeBuffer.toString());

		return respose;

	}
	

	private JSONObject SendHttpsRequest(JSONObject postData,String mothod)
	throws NoSuchAlgorithmException, KeyManagementException,
			IOException, JSONException {
		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
			new java.security.SecureRandom());
		
		StringBuffer mySign = new StringBuffer("");
		YoutuSign.appSign(m_appid, m_secret_id, m_secret_key,
			System.currentTimeMillis() / 1000 + EXPIRED_SECONDS,
			"", mySign);

		System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
		System.setProperty("sun.net.client.defaultReadTimeout", "30000");
		
		URL url = new URL(m_end_point + mothod);
		HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
		connection.setSSLSocketFactory(sc.getSocketFactory());
		connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
     // set header
		connection.setRequestMethod("POST");
		connection.setRequestProperty("accept", "*/*");
//		connection.setRequestProperty("Host", "youtu.api.qcloud.com");
		connection.setRequestProperty("user-agent", "youtu-android-sdk");
		connection.setRequestProperty("Authorization", mySign.toString());

		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setUseCaches(false);
		connection.setInstanceFollowRedirects(true);
		connection.setRequestProperty("Content-Type", "text/json");
		connection.connect();

		OutputStream outaa = connection.getOutputStream();
    	// POST请求
		DataOutputStream out = new DataOutputStream(connection.getOutputStream());

		postData.put("app_id", m_appid);
		out.write(postData.toString().getBytes("utf-8"));
		// 刷新、关闭
		out.flush();
		out.close();
		
		// 读取响应
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		String lines;
		StringBuffer resposeBuffer = new StringBuffer("");
		while ((lines = reader.readLine()) != null) {
			lines = new String(lines.getBytes(), "utf-8");
			resposeBuffer.append(lines);
		}
     	// System.out.println(resposeBuffer+"\n");
		reader.close();
     	// 断开连接
		connection.disconnect();

		JSONObject respose = new JSONObject(resposeBuffer.toString());

		return respose;
	}

	private JSONObject SendRequest(JSONObject postData, String method)
			throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {
		return m_use_https ? SendHttpsRequest(postData, method) : SendHttpRequest(postData, method);
	}
	/*!
	 * 人脸属性分析 检测给定图片(Image)中的所有人脸(Face)的位置和相应的面部属性。位置包括(x, y, w, h),
	 * 面部属性包括性别(gender), 年龄(age), 表情(expression), 眼镜(glass)和姿态(pitch,roll,yaw).
	 *
	 * @param bitmap 人脸图片
	 * @param mode 检测模式 0/1 正常/大脸模式
	 * @return 请求json结果
	*/
	public JSONObject DetectFace(Bitmap bitmap,int mode) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);
		data.put("mode", mode);

		JSONObject respose = SendRequest(data, "api/detectface");

		return respose;
	}


	/*!
	 * 人脸属性分析 检测给定图片(Image)中的所有人脸(Face)的位置和相应的面部属性。位置包括(x, y, w, h),
	 * 面部属性包括性别(gender), 年龄(age), 表情(expression), 眼镜(glass)和姿态(pitch,roll,yaw).
	 *
	 * @param url 人脸图片url
	 * @param mode 检测模式 0/1 正常/大脸模式
	 * @return 请求json结果
	*/
	public JSONObject DetectFaceUrl(String url, int mode)
	throws IOException, JSONException, KeyManagementException,
			NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		data.put("url", url);
		data.put("mode", mode);
		JSONObject respose = SendRequest(data, "api/detectface");

		return respose;
	}


	/*!
	 * 五官定位
	 *
	 * @param image
	 *            人脸图片
	 */
	public JSONObject FaceShape(Bitmap bitmap,int mode) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();
		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);
		data.put("mode", mode);
		JSONObject respose = SendRequest(data, "api/faceshape");

		return respose;
	}
	/*!
	 * 五官定位
	 *
	 * @param url
	 *            人脸图片url
	 */
	public JSONObject FaceShapeUrl(String url,int mode) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();
		data.put("url", url);
		data.put("mode", mode);
		JSONObject respose = SendRequest(data, "api/faceshape");

		return respose;
	}

	/*!
	 * 人脸对比, 计算两个Face的相似性以及五官相似度。
	 *
	 * @param bitmapA 第一张人脸图片
	 * @param bitmapB 第二张人脸图片
	 */
	public JSONObject FaceCompare(Bitmap bitmapA, Bitmap bitmapB)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();
		String imageData = bitmapToBase64(bitmapA);
		data.put("imageA", imageData);

		imageData = bitmapToBase64(bitmapB);
		data.put("imageB", imageData);
		
		JSONObject respose = SendRequest(data, "api/facecompare");

		return respose;
	}

	/*!
	 * 人脸对比, 计算两个Face的相似性以及五官相似度。
	 *
	 * @param urlA 第一张人脸图片url
	 * @param urlB 第二张人脸图片url
	 */
	public JSONObject FaceCompareUrl(String urlA, String urlB)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		data.put("urlA", urlA);
		data.put("urlB", urlB);
		
		JSONObject respose = SendRequest(data, "api/facecompare");

		return respose;
	}

	/*!
	 * 人脸验证,给定一个Face和一个Person,返回是否是同一个人的判断以及置信度。
	 *
	 * @param bitmap 需要验证的人脸图片
	 * @param person_id 验证的目标person
	*/
	public JSONObject FaceVerify(Bitmap bitmap, String person_id)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);

		data.put("person_id", person_id);

		JSONObject respose = SendRequest(data, "api/faceverify");

		return respose;
	}

	/*!
	 * 人脸验证,给定一个Face和一个Person,返回是否是同一个人的判断以及置信度。
	 *
	 * @param url 需要验证的人脸图片url
	 * @param person_id 验证的目标person
	*/
	public JSONObject FaceVerifyUrl(String url, String person_id)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		data.put("url", url);

		data.put("person_id", person_id);

		JSONObject respose = SendRequest(data, "api/faceverify");

		return respose;
	}

	/*!
	 * 人脸识别,对于一个待识别的人脸图片,在一个Group中识别出最相似的Top5 Person作为其身份返回,返回的Top5中按照相似度从大到小排列。
	 *
	 * @param bitmap 需要识别的人脸图片
	 * @param group_id 人脸face组
	 */
	public JSONObject FaceIdentify(Bitmap bitmap, String group_id)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);

		data.put("group_id", group_id);

		JSONObject respose = SendRequest(data, "api/faceidentify");

		return respose;
	}

	/*!
	 * 人脸识别,对于一个待识别的人脸图片,在一个Group中识别出最相似的Top5 Person作为其身份返回,返回的Top5中按照相似度从大到小排列。
	 *
	 * @param url 需要识别的人脸图片url
	 * @param group_id 人脸face组
	 */
	public JSONObject FaceIdentifyUrl(String url, String group_id)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		data.put("url", url);
		data.put("group_id", group_id);

		JSONObject respose = SendRequest(data, "api/faceidentify");

		return respose;
	}

	/*!
	 * 创建一个Person,并将Person放置到group_ids指定的组当中
	 *
	 * @param bitmap 需要新建的人脸图片
	 * @param person_id 指定创建的人脸
	 * @param group_ids 加入的group列表
	*/
	public JSONObject NewPerson(Bitmap bitmap, String person_id,
		List<String> group_ids) throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);

		data.put("person_id", person_id);
		data.put("group_ids", new JSONArray(group_ids));

		JSONObject respose = SendRequest(data, "api/newperson");

		return respose;
	}

	/*!
	 * 创建一个Person,并将Person放置到group_ids指定的组当中
	 *
	 * @param url 需要新建的人脸图片url
	 * @param person_id 指定创建的人脸
	 * @param group_ids 加入的group列表
	*/
	public JSONObject NewPersonUrl(String url, String person_id,
		List<String> group_ids) throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		data.put("url", url);

		data.put("person_id", person_id);
		data.put("group_ids", new JSONArray(group_ids));

		JSONObject respose = SendRequest(data, "api/newperson");

		return respose;
	}

	/*!
	 * 删除一个person下的face,包括特征,属性和face_id.
	 *
	 * @param person_id 待删除人脸的person ID
	*/
	public JSONObject DelPerson(String person_id) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		data.put("person_id", person_id);

		JSONObject respose = SendRequest(data, "api/delperson");

		return respose;
	}

	/*!
	 * 增加一个人脸Face.将一组Face加入到一个Person中。注意,一个Face只能被加入到一个Person中。
	 * 一个Person最多允许包含100个Face。
	 *
	 * @param person_id 人脸Face的person id
	 * @param bitmap_arr 人脸图片列表
	*/
	public JSONObject AddFace(String person_id, List<Bitmap> bitmap_arr)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();
		List<String> images = new ArrayList<String>();
		for (Bitmap bitmap : bitmap_arr) {
			String imageData = bitmapToBase64(bitmap);
			images.add(imageData);
		}

		data.put("images", new JSONArray(images));

		data.put("person_id", person_id);

		JSONObject respose = SendRequest(data, "api/addface");

		return respose;
	}

	/*!
	 * 增加一个人脸Face.将一组Face加入到一个Person中。注意,一个Face只能被加入到一个Person中。
	 * 一个Person最多允许包含100个Face。
	 *
	 * @param person_id 人脸Face的person id
	 * @param url_arr 人脸图片url列表
	*/
	public JSONObject AddFaceUrl(String person_id, List<String> url_arr)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		
		data.put("urls", new JSONArray(url_arr));
		data.put("person_id", person_id);

		JSONObject respose = SendRequest(data, "api/addface");

		return respose;
	}
	/*!
     * 删除一个person下的face,包括特征,属性和face_id.
     *
     * @param person_id 待删除人脸的person ID
     * @param face_id_arr 删除人脸id的列表
    */
	public JSONObject DelFace(String person_id, List<String> face_id_arr)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		data.put("face_ids", new JSONArray(face_id_arr));
		data.put("person_id", person_id);
		JSONObject respose = SendRequest(data, "api/delface");

		return respose;

	}
	/*!
     * 设置Person的name.
     *
     * @param person_name 新的name
     * @param person_id 要设置的person id
    */
	public JSONObject SetInfo(String person_name, String person_id)
	throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();

		data.put("person_name", person_name);
		data.put("person_id", person_id);
		JSONObject respose = SendRequest(data, "api/setinfo");

		return respose;

	}

	/*!
	 * 获取一个Person的信息, 包括name, id, tag, 相关的face, 以及groups等信息。
	 *
	 * @param person_id 待查询个体的ID
	*/
	public JSONObject GetInfo(String person_id) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();

		data.put("person_id", person_id);
		JSONObject respose = SendRequest(data, "api/getinfo");

		return respose;
	}

	/*!
	 * 获取一个AppId下所有group列表
	 */
	public JSONObject GetGroupIds() throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();

		JSONObject respose = SendRequest(data, "api/getgroupids");

		return respose;
	}

	/*!
	 * 获取一个组Group中所有person列表
	 *
	 * @param group_id 待查询的组id
	*/
	public JSONObject GetPersonIds(String group_id) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();

		data.put("group_id", group_id);
		JSONObject respose = SendRequest(data, "api/getpersonids");

		return respose;
	}
	/*!
     * 获取一个组person中所有face列表
     *
     * @param person_id 待查询的个体id
    */
	public JSONObject GetFaceIds(String person_id) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();

		data.put("person_id", person_id);
		JSONObject respose = SendRequest(data, "api/getfaceids");

		return respose;
	}

	/*!
	 * 获取一个face的相关特征信息
	 *
	 * @param face_id 带查询的人脸ID
	*/
	public JSONObject GetFaceInfo(String face_id) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();

		data.put("face_id", face_id);
		JSONObject respose = SendRequest(data, "api/getfaceinfo");

		return respose;
	}


	/*!
	 * 判断一个图像的模糊程度
	 *
	 * @param bitmap 输入图片
	 */
	public JSONObject FuzzyDetect(Bitmap bitmap) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);

		JSONObject respose = SendRequest(data, "imageapi/fuzzydetect");

		return respose;
	}

	/*!
	 * 判断一个图像的模糊程度
	 *
	 * @param url 输入图片url
	 */
	public JSONObject FuzzyDetectUrl(String url) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();

		data.put("url", url);
		JSONObject respose = SendRequest(data, "imageapi/fuzzydetect");
		return respose;
	}

	/*!
	 * 识别一个图像是否为美食图像
	 *
	 * @param bitmap 输入图片
	 */
	public JSONObject FoodDetect(Bitmap bitmap) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);

		JSONObject respose = SendRequest(data, "imageapi/fooddetect");
		return respose;
	}

	/*!
	 * 识别一个图像是否为美食图像
	 *
	 * @param url 输入图片url
	 */
	public JSONObject FoodDetectUrl(String url) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();

		data.put("url", url);
		JSONObject respose = SendRequest(data, "imageapi/fooddetect");
		return respose;
	}


	/*!
	 * 识别一个图像的标签信息,对图像分类。
	 *
	 * @param bitmap 输入图片
	 */
	public JSONObject ImageTag(Bitmap bitmap) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);

		JSONObject respose = SendRequest(data, "imageapi/imagetag");
		return respose;
	}

	/*!
	 * 识别一个图像的标签信息,对图像分类。
	 *
	 * @param url 输入图片url
	 */
	public JSONObject ImageTagUrl(String url) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		data.put("url", url);
		JSONObject respose = SendRequest(data, "imageapi/imagetag");
		return respose;
	}

	/*!
	 * 识别一个图像是否为色情图像
	 *
	 * @param bitmap 输入图片
	 */
	public JSONObject ImagePorn(Bitmap bitmap) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);
		JSONObject respose = SendRequest(data, "imageapi/imageporn");
		return respose;
	}

	/*!
	 * 识别一个图像是否为色情图像
	 *
	 * @param url 输入图片url
	 */
	public JSONObject ImagePornUrl(String url) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		data.put("url", url);
		JSONObject respose = SendRequest(data, "imageapi/imageporn");
		return respose;
	}

	/*!
	 * 身份证OCR识别
	 *
	 * @param bitmap  输入图片
	 * @param cardType 身份证图片类型,0-正面,1-反面
	 */

	public JSONObject IdcardOcr(Bitmap bitmap, int cardType) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);
		data.put("card_type", cardType);

		JSONObject response = SendRequest(data, "ocrapi/idcardocr");
		return response;
	}

	/*!
	 * 身份证OCR识别
	 *
	 * @param url  输入图片url
	 * @param cardType 身份证图片类型,0-正面,1-反面
	 */
	public JSONObject IdcardOcrUrl(String url, int cardType) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		data.put("url", url);
		data.put("card_type", cardType);

		JSONObject response = SendRequest(data, "ocrapi/idcardocr");
		return response;
	}

	/*!
	 * 名片OCR识别
	 *
	 * @param bitmap  输入图片
	 */
	public JSONObject NamecardOcr(Bitmap bitmap) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);

		JSONObject response = SendRequest(data, "ocrapi/namecardocr");
		return response;
	}

	/*!
	 * 名片OCR识别
	 *
	 * @param url  输入图片url
	 */
	public JSONObject NamecardOcrUrl(String url) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		data.put("url", url);

		JSONObject response = SendRequest(data, "ocrapi/namecardocr");
		return response;
	}


//facein 人脸核身
	/*!
	 * 身份证OCR识别 --人脸核身相关接口
	 *
	 * @param bitmap  输入图片
	 * @param cardType 身份证图片类型,0-正面,1-反面
	 */
	public JSONObject IdcardOcrVIP(Bitmap bitmap, int cardType) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);
		data.put("card_type", cardType);

		JSONObject response = SendRequest(data, "ocrapi/idcardocr");
		return response;
	}

	/*
	 *静态人脸比对:用户自带数据源比对 --人脸核身相关接口
	 * 人脸对比, 计算两个Face的相似性以及五官相似度。
	 *
	 * @param bitmapA 第一张人脸图片
	 * @param bitmapB 第二张人脸图片
	 */

	public JSONObject FaceCompareVip(Bitmap bitmapA, Bitmap bitmapB) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();
		String imageData = bitmapToBase64(bitmapA);
		data.put("imageA", imageData);

		imageData = bitmapToBase64(bitmapB);
		data.put("imageB", imageData);

		JSONObject respose = SendRequest(data, "api/facecompare");

		return respose;
	}

	/*!
	 * 静态人脸比对:使用优图数据源比对 --人脸核身相关接口
	 *
	 * @param idcard  用户身份证号码
	 * @param name  用户身份证姓名
	 * @param bitmap 输入图片
	 */
	public JSONObject IdcardFaceCompare(Bitmap bitmap, String name, String idcard) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {
		JSONObject data = new JSONObject();
		String imageData = bitmapToBase64(bitmap);
		data.put("image", imageData);
		data.put("idcard_number", idcard);
		data.put("idcard_name", name);

		JSONObject respose = SendRequest(data, "openliveapi/idcardfacecompare");

		return respose;
	}


	/*
	*唇语获取 --人脸核身相关接口
	*
	*/
	public JSONObject LivegetFour() throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();

		JSONObject respose = SendRequest(data, "openliveapi/livegetfour");

		return respose;
	}

	/*!
	 * 视频人脸核身:用户自带数据源核身 --人脸核身相关接口
	 *
	 * @param video 需要检测的视频base64编码
	 * @param validateDat livegetfour得到的唇语验证数据
	 * @param bitmap 输入图片
	 * @param isCompare video中的照片和card是否做对比,True做对比,False不做对比
	 */
	public JSONObject LiveDetectFour(byte[] video, Bitmap bitmap, String validateData, boolean isCompare) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();
		String vedioData = Base64.encodeToString(video, Base64.DEFAULT);
		String imageData = bitmapToBase64(bitmap);
		data.put("video", vedioData);
		data.put("card", imageData);
		data.put("validate_data", validateData);
		data.put("compare_flag", isCompare);

		JSONObject respose = SendRequest(data, "openliveapi/livedetectfour");

		return respose;
	}

	/*!
	 * 视频人脸核身:使用优图数据源核身 --人脸核身相关接口
	 *
	 * @param video 需要检测的视频base64编码
	 * @param idcard 用户身份证号码
	 * @param name 用户身份证姓名
	 * @param validateData livegetfour得到的唇语验证数据
	 */
	public JSONObject IdcardLiveDetectFour(byte[] video, String validateData, String name, String idcard) throws IOException,
			JSONException, KeyManagementException, NoSuchAlgorithmException {

		JSONObject data = new JSONObject();
		String vedioData = Base64.encodeToString(video, Base64.DEFAULT);
		data.put("video", vedioData);
		data.put("idcard_number", idcard);
		data.put("idcard_name", name);
		data.put("validate_data", validateData);

		JSONObject respose = SendRequest(data, "openliveapi/idcardlivedetectfour");

		return respose;
	}



}