├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── Face.iml
├── README.md
├── RxFace.iml
├── app
├── .gitignore
├── app.iml
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── mrfu
│ │ └── face
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── mrfu
│ │ │ └── rxface
│ │ │ ├── AppApplication.java
│ │ │ ├── BaseActivity.java
│ │ │ ├── MainActivity.java
│ │ │ ├── business
│ │ │ ├── Constants.java
│ │ │ └── DealData.java
│ │ │ ├── loader
│ │ │ ├── ExecutorManager.java
│ │ │ ├── FaceApi.java
│ │ │ ├── SchedulersCompat.java
│ │ │ ├── WebServiceException.java
│ │ │ └── custom
│ │ │ │ ├── AsciiTypeString.java
│ │ │ │ ├── CustomMultipartTypedOutput.java
│ │ │ │ └── CustomTypedByteArray.java
│ │ │ └── models
│ │ │ ├── BaseResponse.java
│ │ │ ├── FaceResponse.java
│ │ │ └── NeedDataEntity.java
│ └── res
│ │ ├── drawable
│ │ └── jobs.jpg
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── content_main.xml
│ │ ├── menu
│ │ └── menu_main.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── logo.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── logo.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── logo.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── logo.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── logo.png
│ │ ├── values-v21
│ │ └── styles.xml
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── mrfu
│ └── face
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── images
├── image_screen.png
└── movie_screen.gif
├── sdk_unuse
├── HttpRequests.java
├── MainActivity.java
└── PostParameters.java
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/workspace.xml
4 | /.idea/libraries
5 | .DS_Store
6 | /build
7 | /captures
8 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | RxFace
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 | Android API 21 Platform
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Face.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | RxFace
2 | =====================
3 |
4 | 用 RxJava, Retrofit, Okhttp 处理人脸识别的简单用例
5 |
6 | ## Overview
7 |
8 | 这是一个人脸识别的简单 Demo, 使用了 [FacePlusPlus](http://www.faceplusplus.com.cn/) 的接口。他们的`/detection/detect` 人脸识别接口可以使用普通的 `get` 也可以用 `post` 传递图片二进制流的形式。其中 `post` 的时候遇到了相当多的坑,下面会提。
9 |
10 | 该 demo 的网络请求库使用了 [Retrofit](https://github.com/square/retrofit) 并集成了 [OkHttp](https://github.com/square/okhttp),使用 [RxJava](https://github.com/ReactiveX/RxJava) 进行封装,方便以流的形式处理网络回调以及图片处理,View 的注入框架用了 [ButterKnife](https://github.com/JakeWharton/butterknife),图片加载使用 [Glide](https://github.com/bumptech/glide)。
11 |
12 | ## Versions
13 |
14 | ### v1.1
15 |
16 | 1. 增加了 compose 复用 work thread 处理数据,然后在 main thread 处理结果的逻辑:你可以在这片文章看到更多:[RxWeekend——RxJava周末狂欢](http://www.jianshu.com/p/ce228f517586)
17 | 2. mSubscription.unsubscribe();
18 | 3. 增加了Service 返回错误情况的处理
19 |
20 | ### v1.0
21 |
22 |
23 |
24 | ## Main difficulties
25 |
26 | 当直接使用 `get` 通过传图片 Url 拿到人脸识别数据的话是相当简单的,如下请求链接只要使用 `Retrofit` 的 `get` 请求的 `@QueryMap` 传递参数即可: [Get数据Demo](http://apicn.faceplusplus.com/v2/detection/detect?api_key=7cd1e10dc037bbe9e6db2813d6127475&api_secret=gruCjvStG159LCJutENBt6yzeLK_5ggX&url=http://imglife.gmw.cn/attachement/jpg/site2/20111014/002564a5d7d21002188831.jpg)。
27 |
28 |
29 | 主要存在的困难点是,当获取本地图片,再使用 `post` 传二进制图片数据时,`post` 要使用 `MultipartTypedOutput`,可参考 [stackoverflow](http://stackoverflow.com/questions/25249042/retrofit-multiple-images-attached-in-one-multipart-request/25260556#25260556) 的回答。然而,这样并没有结束,根据 FacePlusPlus 提供的 SDK Sample 里的 Httpurlconnection 的得到的 `post` 请求头是这样的:
30 |
31 | `[Content-Disposition: form-data; name="api_key", Content-Type: text/plain; charset=US-ASCII, Content-Transfer-Encoding: 8bit]`
32 |
33 | `[Content-Disposition: form-data; name="img"; filename="NoName", Content-Type: application/octet-stream, Content-Transfer-Encoding: binary]`
34 |
35 | 而使用 `Retrofit` 默认实现的话,我们这样来实现:
36 |
37 | ```java
38 | public static MultipartTypedOutput mulipartData(Bitmap bitmap, String boundary){
39 | byte[] data = getBitmapByte(bitmap);
40 | MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();
41 | multipartTypedOutput.addPart("api_key", new TypedString(Constants.API_KEY));
42 | multipartTypedOutput.addPart("api_secret", new TypedString(Constants.API_SECRET));
43 | multipartTypedOutput.addPart("img", new TypedByteArray("application/octet-stream", data));
44 | return multipartTypedOutput;
45 | }
46 | ```
47 |
48 | 根据 Sample 的请求头,`RestAdapter` 的请求头参数我们这样设置来:
49 |
50 | ```java
51 | private RequestInterceptor mRequestInterceptor = new RequestInterceptor() {
52 | @Override
53 | public void intercept(RequestFacade request) {
54 | request.addHeader("connection", "keep-alive");
55 | request.addHeader("Content-Type", "multipart/form-data; boundary="+ getBoundary() + "; charset=UTF-8");
56 | }
57 | };
58 | ```
59 |
60 |
61 | 但是!!!它得到的String参数的头是这样的,这里没有贴出其他的差异,
62 |
63 | ```java
64 | Content-Disposition: form-data; name="api_key"
65 | Content-Type: text/plain; charset=UTF-8
66 | Content-Length: 32
67 | Content-Transfer-Encoding: 8bit
68 | ```
69 |
70 | 所以需要重写三个类:
71 |
72 | `MultipartTypedOutput` 为 final 类,所以重写为 `CustomMultipartTypedOutput`,并使其构造函数,增加 boundary 的设置;
73 |
74 | `TypedString `默认的编码格式是`UTF-8`,所以重写为 `AsciiTypeString`类,使其编码格式改为 `US-ASCII`;
75 |
76 | `TypedByteArray` 默认的的 `fileName()` 方法返回的是 null,而当传图片数据时需要 fileName 为 "NoName",所以重写为 `CustomTypedByteArray` 类,设置其 fileName 为 "NoName"。
77 |
78 | 同时需要注意的是在设置 `RestAdapter` 的 header 时,其 boundary 一定要和 `CustomMultipartTypedOutput` 的 boundary 相同,否则服务端无法匹配的!(这个地方,一时没注意,被整了一个多小时才发现!!)
79 |
80 | 最后 body 的传参,这样来得到:
81 |
82 | ```java
83 | public static CustomMultipartTypedOutput mulipartData(Bitmap bitmap, String boundary){
84 | byte[] data = getBitmapByte(bitmap);
85 | CustomMultipartTypedOutput multipartTypedOutput = new CustomMultipartTypedOutput(boundary);
86 | multipartTypedOutput.addPart("api_key", "8bit", new AsciiTypeString(Constants.API_KEY));
87 | multipartTypedOutput.addPart("api_secret", "8bit", new AsciiTypeString(Constants.API_SECRET));
88 | multipartTypedOutput.addPart("img", new CustomTypedByteArray("application/octet-stream", data));
89 | return multipartTypedOutput;
90 | }
91 | ```
92 |
93 |
94 | ## Preview
95 |
96 | 
97 |
98 | 
99 |
100 |
101 | ## More about me
102 |
103 | * [MrFu-傅圆的个人博客](http://mrfu.me/)
104 |
105 |
106 |
107 | ## Acknowledgments
108 |
109 | * [Glide](https://github.com/bumptech/glide) -Glide
110 | * [Retrofit](https://github.com/square/retrofit) - Retrofit
111 | * [OkHttp](https://github.com/square/okhttp) - OkHttp
112 | * [RxJava](https://github.com/ReactiveX/RxJava) - RxJava
113 | * [ButterKnife](https://github.com/JakeWharton/butterknife) - ButterKnife
114 |
115 |
116 |
117 | License
118 | ============
119 |
120 | Copyright 2015 MrFu
121 |
122 | Licensed under the Apache License, Version 2.0 (the "License");
123 | you may not use this file except in compliance with the License.
124 | You may obtain a copy of the License at
125 |
126 | http://www.apache.org/licenses/LICENSE-2.0
127 |
128 | Unless required by applicable law or agreed to in writing, software
129 | distributed under the License is distributed on an "AS IS" BASIS,
130 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131 | See the License for the specific language governing permissions and
132 | limitations under the License.
133 |
134 | [](https://bitdeli.com/free "Bitdeli Badge")
135 |
136 |
--------------------------------------------------------------------------------
/RxFace.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | generateDebugAndroidTestSources
19 | generateDebugSources
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 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion "23.0.2"
6 |
7 | defaultConfig {
8 | applicationId "mrfu.face"
9 | minSdkVersion 14
10 | targetSdkVersion 23
11 | versionCode 3
12 | versionName "1.1"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 |
21 | lintOptions {
22 | abortOnError false
23 | }
24 |
25 | // compileOptions {
26 | // sourceCompatibility JavaVersion.VERSION_1_8
27 | // targetCompatibility JavaVersion.VERSION_1_8
28 | // }
29 | }
30 |
31 | dependencies {
32 | compile fileTree(dir: 'libs', include: ['*.jar'])
33 | testCompile 'junit:junit:4.12'
34 | compile 'com.android.support:appcompat-v7:23.1.1'
35 | compile 'com.android.support:design:23.1.1'
36 | compile 'com.squareup.retrofit:retrofit:1.9.0'
37 | compile 'io.reactivex:rxjava:1.0.14'
38 | compile 'io.reactivex:rxandroid:1.0.1'
39 | compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
40 | compile 'com.squareup.okhttp:okhttp:2.0.0'
41 | compile 'com.jakewharton:butterknife:7.0.1'
42 | compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
43 | compile 'com.github.bumptech.glide:glide:3.6.0'
44 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/MrFu/Desktop/Android/adt-bundle-mac-x86_64-20131030/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/mrfu/face/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package mrfu.face;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/AppApplication.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 |
6 | /**
7 | * Created by MrFu on 15/12/15.
8 | */
9 | public class AppApplication extends Application {
10 |
11 | private static Context context;
12 |
13 | public static Context getContext() {
14 | return context;
15 | }
16 |
17 | @Override
18 | public void onCreate() {
19 | super.onCreate();
20 | context = getApplicationContext();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 |
6 | import butterknife.ButterKnife;
7 | import rx.Subscription;
8 | import rx.subscriptions.Subscriptions;
9 |
10 | /**
11 | * Created by MrFu on 16/1/10.
12 | */
13 | public class BaseActivity extends AppCompatActivity {
14 | protected Subscription mSubscription = Subscriptions.empty();
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | }
19 |
20 | @Override
21 | public void setContentView(int layoutResID) {
22 | super.setContentView(layoutResID);
23 | ButterKnife.bind(BaseActivity.this);
24 | }
25 |
26 | @Override
27 | protected void onDestroy() {
28 | super.onDestroy();
29 | ButterKnife.unbind(BaseActivity.this);
30 | if (mSubscription != null && !mSubscription.isUnsubscribed()) mSubscription.unsubscribe();
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/MainActivity.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface;
2 |
3 | import android.content.Intent;
4 | import android.graphics.Bitmap;
5 | import android.graphics.drawable.BitmapDrawable;
6 | import android.net.Uri;
7 | import android.os.Bundle;
8 | import android.support.v7.widget.Toolbar;
9 | import android.text.TextUtils;
10 | import android.util.Log;
11 | import android.view.Menu;
12 | import android.view.MenuItem;
13 | import android.view.View;
14 | import android.widget.Button;
15 | import android.widget.ImageView;
16 | import android.widget.TextView;
17 |
18 | import com.bumptech.glide.Glide;
19 |
20 | import java.util.HashMap;
21 | import java.util.Map;
22 | import java.util.concurrent.ExecutionException;
23 |
24 | import butterknife.Bind;
25 | import butterknife.OnClick;
26 | import mrfu.rxface.business.Constants;
27 | import mrfu.rxface.business.DealData;
28 | import mrfu.rxface.loader.FaceApi;
29 | import mrfu.rxface.models.FaceResponse;
30 | import mrfu.rxface.models.NeedDataEntity;
31 | import rx.Observable;
32 | import rx.Observer;
33 | import rx.android.schedulers.AndroidSchedulers;
34 | import rx.functions.Func1;
35 | import rx.schedulers.Schedulers;
36 |
37 | public class MainActivity extends BaseActivity {
38 |
39 | @Bind(R.id.iv_face_get)
40 | ImageView iv_face_get;
41 |
42 | @Bind(R.id.iv_face_post)
43 | ImageView iv_face_post;
44 |
45 | @Bind(R.id.btn_recongize_get)
46 | Button btn_recongize_get;
47 |
48 | @Bind(R.id.btn_recongize_post)
49 | Button btn_recongize_post;
50 |
51 | @Bind(R.id.tv_age_get)
52 | TextView tv_age_get;
53 |
54 | @Bind(R.id.tv_age_post)
55 | TextView tv_age_post;
56 |
57 | /**
58 | * 0:GET button
59 | * 1:POST button
60 | */
61 | private int type = 0;
62 |
63 | @Override
64 | protected void onCreate(Bundle savedInstanceState) {
65 | super.onCreate(savedInstanceState);
66 | setContentView(R.layout.activity_main);
67 |
68 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
69 | setSupportActionBar(toolbar);
70 |
71 | Glide.with(this).load(Constants.IMAGE_URL).fitCenter().into(iv_face_get);
72 | iv_face_post.setImageResource(R.drawable.jobs);
73 | }
74 |
75 | @OnClick(R.id.btn_recongize_get)
76 | public void btn_recongize_get(View view){
77 | type = 0;
78 | Map options = new HashMap<>();
79 | options.put("api_key", Constants.API_KEY);
80 | options.put("api_secret", Constants.API_SECRET);
81 | options.put("url", Constants.IMAGE_URL);
82 | mSubscription = FaceApi.getIns()//api_key={apiKey}&api_secret={apiSecret}&url={imgUrl}
83 | .getDataUrlGet(options)
84 | .observeOn(Schedulers.newThread())
85 | .flatMap(new Func1>() {
86 | @Override
87 | public Observable call(FaceResponse faceResponse) {
88 | Bitmap bitmap = null;
89 | try {
90 | //java.lang.IllegalArgumentException: YOu must call this method on a background thread
91 | bitmap = Glide.with(MainActivity.this).load(Constants.IMAGE_URL).asBitmap().into(-1, -1).get();
92 | } catch (InterruptedException | ExecutionException e) {
93 | e.printStackTrace();
94 | }
95 | NeedDataEntity entity = new NeedDataEntity();
96 | entity.bitmap = DealData.drawLineGetBitmap(faceResponse, bitmap);
97 | entity.displayStr = DealData.getDisplayInfo(faceResponse);
98 | return Observable.just(entity);
99 | }
100 | })
101 | .observeOn(AndroidSchedulers.mainThread())
102 | .subscribe(setBitmapDataObserver);
103 | }
104 |
105 | @OnClick(R.id.btn_recongize_post)
106 | public void btn_recongize_post(View view){
107 | type = 1;
108 | BitmapDrawable mDrawable = (BitmapDrawable) iv_face_post.getDrawable();
109 | final Bitmap bitmap = mDrawable.getBitmap();
110 |
111 | mSubscription = FaceApi.getIns()
112 | .getDataPost(DealData.mulipartData(bitmap, FaceApi.getIns().getBoundary()))
113 | .flatMap(new Func1>() {
114 | @Override
115 | public Observable call(FaceResponse faceResponse) {
116 | NeedDataEntity entity = new NeedDataEntity();
117 | entity.bitmap = DealData.drawLineGetBitmap(faceResponse, bitmap);
118 | entity.displayStr = DealData.getDisplayInfo(faceResponse);
119 | return Observable.just(entity);
120 | }
121 | })
122 | .subscribe(setBitmapDataObserver);
123 | }
124 |
125 | private Observer setBitmapDataObserver = new Observer() {
126 |
127 | @Override
128 | public void onNext(final NeedDataEntity needDataEntity) {
129 | if (needDataEntity == null){
130 | return;
131 | }
132 | switch (type){
133 | case 0:
134 | if (!TextUtils.isEmpty(needDataEntity.displayStr)){
135 | tv_age_get.setText(needDataEntity.displayStr);
136 | }
137 | if (needDataEntity.bitmap != null){
138 | iv_face_get.setImageBitmap(needDataEntity.bitmap);
139 | }
140 | break;
141 | case 1:
142 | if (!TextUtils.isEmpty(needDataEntity.displayStr)){
143 | tv_age_post.setText(needDataEntity.displayStr);
144 | }
145 | if (needDataEntity.bitmap != null){
146 | iv_face_post.setImageBitmap(needDataEntity.bitmap);
147 | }
148 | break;
149 | default:
150 | break;
151 | }
152 | }
153 |
154 | @Override
155 | public void onCompleted() {
156 | Log.i("MrFu", "onCompleted");
157 | }
158 |
159 | @Override
160 | public void onError(final Throwable error) {
161 | error.printStackTrace();
162 | }
163 | };
164 |
165 | @Override
166 | public boolean onCreateOptionsMenu(Menu menu) {
167 | // Inflate the menu; this adds items to the action bar if it is present.
168 | getMenuInflater().inflate(R.menu.menu_main, menu);
169 | return true;
170 | }
171 |
172 | @Override
173 | public boolean onOptionsItemSelected(MenuItem item) {
174 | // Handle action bar item clicks here. The action bar will
175 | // automatically handle clicks on the Home/Up button, so long
176 | // as you specify a parent activity in AndroidManifest.xml.
177 | int id = item.getItemId();
178 |
179 | //noinspection SimplifiableIfStatement
180 | if (id == R.id.action_settings) {
181 | Uri uri = Uri.parse("https://github.com/MrFuFuFu/RxFace");
182 | Intent i = new Intent(Intent.ACTION_VIEW, uri);
183 | i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
184 | startActivity(i);
185 | return true;
186 | }
187 |
188 | return super.onOptionsItemSelected(item);
189 | }
190 | }
191 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/business/Constants.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.business;
2 |
3 | /**
4 | * Created by MrFu on 15/12/15.
5 | */
6 | public class Constants {
7 | // public static final String FACE_SERVER_IP = "https://apicn.faceplusplus.com/v2";
8 | public static final String FACE_SERVER_IP = "http://apicn.faceplusplus.com/v2";
9 |
10 | public static final String API_KEY = "7cd1e10dc037bbe9e6db2813d6127475";
11 | public static final String API_SECRET = "gruCjvStG159LCJutENBt6yzeLK_5ggX";
12 |
13 | public static final String IMAGE_URL = "http://www.ipmm.cn/UploadImage/20130502/2013050209272791.jpg";
14 |
15 | public static final int TIME_OUT = 30 * 1000;
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/business/DealData.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.business;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.Matrix;
7 | import android.graphics.Paint;
8 |
9 | import java.io.ByteArrayOutputStream;
10 |
11 | import mrfu.rxface.loader.custom.AsciiTypeString;
12 | import mrfu.rxface.loader.custom.CustomMultipartTypedOutput;
13 | import mrfu.rxface.loader.custom.CustomTypedByteArray;
14 | import mrfu.rxface.models.FaceResponse;
15 |
16 | /**
17 | * Created by MrFu on 15/12/15.
18 | */
19 | public class DealData {
20 |
21 | /**
22 | * 设置参数
23 | * 使用 MultipartTypedOutput, 而不使用 Retrofit @Multipart 的 @Part 和 @PartMap 的形式
24 | * http://stackoverflow.com/questions/25249042/retrofit-multiple-images-attached-in-one-multipart-request/25260556#25260556
25 | *
26 | * @see CustomMultipartTypedOutput 重写 MultipartTypedOutput 使之接受 boundary 参数
27 | * @see AsciiTypeString , 重写 TypedByteArray, 使其编码格式为 US-ASCII
28 | * @see CustomTypedByteArray , 重写 TypedByteArray 设置其 fileName 为 "NoName",
29 | * 以上参数格式和参数类型都必须指定,否则会返回对应的错误
30 | * http://www.faceplusplus.com.cn/detection_detect/
31 | * @param bitmap need upload image
32 | * @param boundary must same with http header boundary
33 | * @return http post body param
34 | */
35 | public static CustomMultipartTypedOutput mulipartData(Bitmap bitmap, String boundary){
36 | byte[] data = getBitmapByte(bitmap);
37 | CustomMultipartTypedOutput multipartTypedOutput = new CustomMultipartTypedOutput(boundary);
38 | multipartTypedOutput.addPart("api_key", "8bit", new AsciiTypeString(Constants.API_KEY));
39 | multipartTypedOutput.addPart("api_secret", "8bit", new AsciiTypeString(Constants.API_SECRET));
40 | multipartTypedOutput.addPart("img", new CustomTypedByteArray("application/octet-stream", data));
41 | return multipartTypedOutput;
42 | }
43 |
44 | /**
45 | * convert bitmap to byte[]
46 | * @param bitmap need convert bitmap
47 | * @return byte[]
48 | */
49 | private static byte[] getBitmapByte(Bitmap bitmap){
50 | ByteArrayOutputStream stream = new ByteArrayOutputStream();
51 | float scale = Math.min(1, Math.min(600f / bitmap.getWidth(), 600f / bitmap.getHeight()));
52 | Matrix matrix = new Matrix();
53 | matrix.postScale(scale, scale);
54 | Bitmap imgSmall = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
55 | imgSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);
56 | return stream.toByteArray();
57 | }
58 |
59 | /**
60 | * draw to include face bitmap
61 | * @param entity data
62 | * @param viewBitmap display bitmap
63 | * @return face bitmap
64 | */
65 | public static Bitmap drawLineGetBitmap(FaceResponse entity, Bitmap viewBitmap){
66 | //use the red paint
67 | Paint paint = new Paint();
68 | paint.setColor(Color.RED);
69 | paint.setStrokeWidth(Math.max(viewBitmap.getWidth(), viewBitmap.getHeight()) / 100f);
70 |
71 | //create a new canvas
72 | Bitmap bitmap = Bitmap.createBitmap(viewBitmap.getWidth(), viewBitmap.getHeight(), viewBitmap.getConfig());
73 | Canvas canvas = new Canvas(bitmap);
74 | canvas.drawBitmap(viewBitmap, new Matrix(), null);
75 |
76 | try {
77 | //find out all faces
78 | final int count = entity.face.size();
79 | for (int i = 0; i < count; ++i) {
80 | float x, y, w, h;
81 | //get the center point
82 | x = (float) entity.face.get(i).position.center.x;
83 | y = (float) entity.face.get(i).position.center.y;
84 | //get face size
85 | w = (float) entity.face.get(i).position.width;
86 | h = (float) entity.face.get(i).position.height;
87 | //change percent value to the real size
88 | x = x / 100 * viewBitmap.getWidth();
89 | w = w / 100 * viewBitmap.getWidth() * 0.7f;
90 | y = y / 100 * viewBitmap.getHeight();
91 | h = h / 100 * viewBitmap.getHeight() * 0.7f;
92 | //draw the box to mark it out
93 | canvas.drawLine(x - w, y - h, x - w, y + h, paint);
94 | canvas.drawLine(x - w, y - h, x + w, y - h, paint);
95 | canvas.drawLine(x + w, y + h, x - w, y + h, paint);
96 | canvas.drawLine(x + w, y + h, x + w, y - h, paint);
97 | }
98 | //save new image
99 | viewBitmap = bitmap;
100 | return viewBitmap;
101 | }catch (Exception e){
102 | e.printStackTrace();
103 | }
104 | return null;
105 | }
106 |
107 | public static String getDisplayInfo(FaceResponse entity){
108 | if (entity == null || entity.face == null || entity.face.size() == 0
109 | || entity.face.get(0).attribute == null){
110 | return "";
111 | }
112 | int age = 0;
113 | if (entity.face.get(0).attribute.age != null){
114 | age = entity.face.get(0).attribute.age.value;
115 | }
116 | String gender = "";
117 | if (entity.face.get(0).attribute.gender != null){
118 | gender = "male".equalsIgnoreCase(entity.face.get(0).attribute.gender.value) ? "男" : "女";
119 | }
120 | return "年龄约 " + age + " 性别为 " + gender;
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/loader/ExecutorManager.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.loader;
2 |
3 | import android.os.Build;
4 | import android.support.annotation.NonNull;
5 |
6 | import java.io.File;
7 | import java.io.FileFilter;
8 | import java.util.concurrent.BlockingQueue;
9 | import java.util.concurrent.ExecutorService;
10 | import java.util.concurrent.LinkedBlockingQueue;
11 | import java.util.concurrent.RejectedExecutionHandler;
12 | import java.util.concurrent.ThreadFactory;
13 | import java.util.concurrent.ThreadPoolExecutor;
14 | import java.util.concurrent.TimeUnit;
15 | import java.util.concurrent.atomic.AtomicInteger;
16 |
17 | /**
18 | * Created by Joker on 2015/8/24.
19 | */
20 | public class ExecutorManager {
21 |
22 | public static final int DEVICE_INFO_UNKNOWN = 0;
23 | public static ExecutorService eventExecutor;
24 | //private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
25 | private static final int CPU_COUNT = ExecutorManager.getCountOfCPU();
26 | private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
27 | private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
28 | private static final int KEEP_ALIVE = 1;
29 | private static final BlockingQueue eventPoolWaitQueue = new LinkedBlockingQueue<>(128);
30 | private static final ThreadFactory eventThreadFactory = new ThreadFactory() {
31 | private final AtomicInteger mCount = new AtomicInteger(1);
32 |
33 | public Thread newThread(@NonNull Runnable r) {
34 | return new Thread(r, "eventAsyncAndBackground #" + mCount.getAndIncrement());
35 | }
36 | };
37 |
38 | private static final RejectedExecutionHandler eventHandler =
39 | new ThreadPoolExecutor.CallerRunsPolicy();
40 |
41 | static {
42 | eventExecutor =
43 | new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS,
44 | eventPoolWaitQueue, eventThreadFactory, eventHandler);
45 | }
46 |
47 | /**
48 | * Linux中的设备都是以文件的形式存在,CPU也不例外,因此CPU的文件个数就等价与核数。
49 | * Android的CPU 设备文件位于/sys/devices/system/cpu/目录,文件名的的格式为cpu\d+。
50 | *
51 | * 引用:http://www.jianshu.com/p/f7add443cd32#,感谢 liangfeizc :)
52 | * https://github.com/facebook/device-year-class
53 | */
54 | public static int getCountOfCPU() {
55 |
56 | if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
57 | return 1;
58 | }
59 | int count;
60 | try {
61 | count = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;
62 | } catch (SecurityException | NullPointerException e) {
63 | count = DEVICE_INFO_UNKNOWN;
64 | }
65 | return count;
66 | }
67 |
68 | private static final FileFilter CPU_FILTER = new FileFilter() {
69 | @Override public boolean accept(File pathname) {
70 |
71 | String path = pathname.getName();
72 | if (path.startsWith("cpu")) {
73 | for (int i = 3; i < path.length(); i++) {
74 | if (path.charAt(i) < '0' || path.charAt(i) > '9') {
75 | return false;
76 | }
77 | }
78 | return true;
79 | }
80 | return false;
81 | }
82 | };
83 | }
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/loader/FaceApi.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.loader;
2 |
3 | import com.squareup.okhttp.OkHttpClient;
4 |
5 | import java.util.Map;
6 | import java.util.Random;
7 | import java.util.concurrent.TimeUnit;
8 |
9 | import mrfu.rxface.BuildConfig;
10 | import mrfu.rxface.business.Constants;
11 | import mrfu.rxface.loader.custom.CustomMultipartTypedOutput;
12 | import mrfu.rxface.models.FaceResponse;
13 | import retrofit.RequestInterceptor;
14 | import retrofit.RestAdapter;
15 | import retrofit.client.OkClient;
16 | import retrofit.http.Body;
17 | import retrofit.http.GET;
18 | import retrofit.http.POST;
19 | import retrofit.http.QueryMap;
20 | import rx.Observable;
21 | import rx.functions.Func1;
22 |
23 | /**
24 | * face api
25 | * Created by MrFu on 15/12/15.
26 | */
27 | public class FaceApi {
28 | private static String mBoundry;
29 | private final static int boundaryLength = 32;
30 | private final static String boundaryAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
31 |
32 | public static FaceApi instance;
33 |
34 | public static FaceApi getIns() {
35 | if (null == instance) {
36 | synchronized (FaceApi.class) {
37 | if (null == instance) {
38 | instance = new FaceApi();
39 | }
40 | }
41 | }
42 | return instance;
43 | }
44 |
45 | private final MrFuService mWebService;
46 |
47 | public FaceApi() {
48 | RestAdapter restAdapter = new RestAdapter.Builder()
49 | .setEndpoint(Constants.FACE_SERVER_IP)
50 | .setClient(new OkClient(new OkHttpClient()))
51 | .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
52 | .setRequestInterceptor(mRequestInterceptor)
53 | .build();
54 | mWebService = restAdapter.create(MrFuService.class);
55 | mBoundry = setBoundary();
56 | }
57 |
58 | private RequestInterceptor mRequestInterceptor = new RequestInterceptor() {
59 | @Override
60 | public void intercept(RequestFacade request) {
61 | request.addHeader("connection", "keep-alive");
62 | request.addHeader("Content-Type", "multipart/form-data; boundary="+ getBoundary() + "; charset=UTF-8");
63 | }
64 | };
65 |
66 | public String getBoundary(){
67 | return mBoundry;
68 | }
69 |
70 | /**
71 | * 设置 Content-Type 的 boundary,这里有个强坑:
72 | * header 的 boundary 和 CustomMultipartTypedOutput 的 boundary 必须相同!!
73 | * @return mBoundry
74 | */
75 | private static String setBoundary() {
76 | StringBuilder sb = new StringBuilder();
77 | Random random = new Random();
78 | for (int i = 0; i < boundaryLength; ++i)
79 | sb.append(boundaryAlphabet.charAt(random.nextInt(boundaryAlphabet.length())));
80 | return sb.toString();
81 | }
82 |
83 | public interface MrFuService {
84 | @POST("/detection/detect")
85 | Observable uploadImagePost(
86 | @Body CustomMultipartTypedOutput listMultipartOutput
87 | );
88 |
89 | //http://apicn.faceplusplus.com/v2/detection/detect?api_key=7cd1e10dc037bbe9e6db2813d6127475&api_secret=gruCjvStG159LCJutENBt6yzeLK_5ggX&url=http://imglife.gmw.cn/attachement/jpg/site2/20111014/002564a5d7d21002188831.jpg
90 | @GET("/detection/detect")
91 | Observable uploadImageUrlGet(
92 | @QueryMap Map options
93 | );
94 | }
95 |
96 | public Observable getDataPost(CustomMultipartTypedOutput listMultipartOutput) {
97 | return mWebService.uploadImagePost(listMultipartOutput)
98 | .timeout(Constants.TIME_OUT, TimeUnit.MILLISECONDS)
99 | .concatMap(new Func1>() {
100 | @Override
101 | public Observable call(FaceResponse faceResponse) {
102 | return faceResponse.filterWebServiceErrors();
103 | }
104 | }).compose(SchedulersCompat.applyExecutorSchedulers());
105 | }
106 |
107 | public Observable getDataUrlGet(Map options) {
108 | return mWebService.uploadImageUrlGet(options)
109 | .timeout(Constants.TIME_OUT, TimeUnit.MILLISECONDS)
110 | .concatMap(new Func1>() {
111 | @Override
112 | public Observable call(FaceResponse faceResponse) {
113 | return faceResponse.filterWebServiceErrors();
114 | }
115 | }).compose(SchedulersCompat.applyExecutorSchedulers());//http://www.jianshu.com/p/e9e03194199e
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/loader/SchedulersCompat.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.loader;
2 |
3 | import rx.Observable;
4 | import rx.android.schedulers.AndroidSchedulers;
5 | import rx.schedulers.Schedulers;
6 |
7 | /**
8 | * 小鄧子:【译】避免打断链式结构:使用.compose( )操作符 http://www.jianshu.com/p/e9e03194199e
9 | * Created by Joker on 2015/8/10.
10 | */
11 | public class SchedulersCompat {
12 |
13 | private static final Observable.Transformer computationTransformer =
14 | new Observable.Transformer() {
15 | @Override public Object call(Object observable) {
16 | return ((Observable) observable).subscribeOn(Schedulers.computation())
17 | .observeOn(AndroidSchedulers.mainThread());
18 | }
19 | };
20 |
21 | private static final Observable.Transformer ioTransformer = new Observable.Transformer() {
22 | @Override public Object call(Object observable) {
23 | return ((Observable) observable).subscribeOn(Schedulers.io())
24 | .observeOn(AndroidSchedulers.mainThread());
25 | }
26 | };
27 | private static final Observable.Transformer newTransformer = new Observable.Transformer() {
28 | @Override public Object call(Object observable) {
29 | return ((Observable) observable).subscribeOn(Schedulers.newThread())
30 | .observeOn(AndroidSchedulers.mainThread());
31 | }
32 | };
33 | private static final Observable.Transformer trampolineTransformer = new Observable.Transformer() {
34 | @Override public Object call(Object observable) {
35 | return ((Observable) observable).subscribeOn(Schedulers.trampoline())
36 | .observeOn(AndroidSchedulers.mainThread());
37 | }
38 | };
39 |
40 | private static final Observable.Transformer executorTransformer = new Observable.Transformer() {
41 | @Override public Object call(Object observable) {
42 | return ((Observable) observable).subscribeOn(Schedulers.from(ExecutorManager.eventExecutor))
43 | .observeOn(AndroidSchedulers.mainThread());
44 | }
45 | };
46 |
47 | /**
48 | * Don't break the chain: use RxJava's compose() operator
49 | */
50 | public static Observable.Transformer applyComputationSchedulers() {
51 |
52 | return (Observable.Transformer) computationTransformer;
53 | }
54 |
55 | public static Observable.Transformer applyIoSchedulers() {
56 |
57 | return (Observable.Transformer) ioTransformer;
58 | }
59 |
60 | public static Observable.Transformer applyNewSchedulers() {
61 |
62 | return (Observable.Transformer) newTransformer;
63 | }
64 |
65 | public static Observable.Transformer applyTrampolineSchedulers() {
66 |
67 | return (Observable.Transformer) trampolineTransformer;
68 | }
69 |
70 | public static Observable.Transformer applyExecutorSchedulers() {
71 |
72 | return (Observable.Transformer) executorTransformer;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/loader/WebServiceException.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.loader;
2 |
3 | /**
4 | * Created by MrFu on 16/1/10.
5 | */
6 | public class WebServiceException extends Exception {
7 | public WebServiceException(String detailMessage) {
8 | super(detailMessage);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/loader/custom/AsciiTypeString.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.loader.custom;
2 |
3 | import java.io.UnsupportedEncodingException;
4 |
5 | import retrofit.mime.TypedByteArray;
6 |
7 | /**
8 | * 重写 TypedByteArray, 使其编码格式为 US-ASCII
9 | * Created by MrFu on 15/12/16.
10 | */
11 | public class AsciiTypeString extends TypedByteArray {
12 |
13 | public AsciiTypeString(String string) {
14 | super("text/plain; charset=US-ASCII", convertToBytes(string));
15 | }
16 |
17 | private static byte[] convertToBytes(String string) {
18 | try {
19 | return string.getBytes("US-ASCII");
20 | } catch (UnsupportedEncodingException e) {
21 | throw new RuntimeException(e);
22 | }
23 | }
24 |
25 | @Override public String toString() {
26 | try {
27 | return "TypedString[" + new String(getBytes(), "US-ASCII") + "]";
28 | } catch (UnsupportedEncodingException e) {
29 | throw new AssertionError("Must be able to decode US-ASCII");
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/loader/custom/CustomMultipartTypedOutput.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.loader.custom;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.IOException;
5 | import java.io.OutputStream;
6 | import java.util.ArrayList;
7 | import java.util.LinkedList;
8 | import java.util.List;
9 | import java.util.UUID;
10 |
11 | import retrofit.mime.TypedOutput;
12 |
13 | /**
14 | * 重写 MultipartTypedOutput 使之接受 boundary 参数
15 | * Created by MrFu on 15/12/16.
16 | */
17 | public class CustomMultipartTypedOutput implements TypedOutput {
18 | public static final String DEFAULT_TRANSFER_ENCODING = "binary";
19 |
20 | private static final class MimePart {
21 | private final TypedOutput body;
22 | private final String name;
23 | private final String transferEncoding;
24 | private final boolean isFirst;
25 | private final String boundary;
26 |
27 | private byte[] partBoundary;
28 | private byte[] partHeader;
29 | private boolean isBuilt;
30 |
31 | public MimePart(String name, String transferEncoding, TypedOutput body, String boundary,
32 | boolean isFirst) {
33 | this.name = name;
34 | this.transferEncoding = transferEncoding;
35 | this.body = body;
36 | this.isFirst = isFirst;
37 | this.boundary = boundary;
38 | }
39 |
40 | public void writeTo(OutputStream out) throws IOException {
41 | build();
42 | out.write(partBoundary);
43 | out.write(partHeader);
44 | body.writeTo(out);
45 | }
46 |
47 | public long size() {
48 | build();
49 | if (body.length() > -1) {
50 | return body.length() + partBoundary.length + partHeader.length;
51 | } else {
52 | return -1;
53 | }
54 | }
55 |
56 | private void build() {
57 | if (isBuilt) return;
58 | partBoundary = buildBoundary(boundary, isFirst, false);
59 | partHeader = buildHeader(name, transferEncoding, body);
60 | isBuilt = true;
61 | }
62 | }
63 |
64 | private final List mimeParts = new LinkedList();
65 |
66 | private final byte[] footer;
67 | private final String boundary;
68 | private long length;
69 |
70 | public CustomMultipartTypedOutput() {
71 | this(UUID.randomUUID().toString());
72 | }
73 |
74 | public CustomMultipartTypedOutput(String boundary) {
75 | this.boundary = boundary;
76 | footer = buildBoundary(boundary, false, true);
77 | length = footer.length;
78 | }
79 |
80 | List getParts() throws IOException {
81 | List parts = new ArrayList(mimeParts.size());
82 | for (MimePart part : mimeParts) {
83 | ByteArrayOutputStream bos = new ByteArrayOutputStream();
84 | part.writeTo(bos);
85 | parts.add(bos.toByteArray());
86 | }
87 | return parts;
88 | }
89 |
90 | public void addPart(String name, TypedOutput body) {
91 | addPart(name, DEFAULT_TRANSFER_ENCODING, body);
92 | }
93 |
94 | public void addPart(String name, String transferEncoding, TypedOutput body) {
95 | if (name == null) {
96 | throw new NullPointerException("Part name must not be null.");
97 | }
98 | if (transferEncoding == null) {
99 | throw new NullPointerException("Transfer encoding must not be null.");
100 | }
101 | if (body == null) {
102 | throw new NullPointerException("Part body must not be null.");
103 | }
104 |
105 | MimePart part = new MimePart(name, transferEncoding, body, boundary, mimeParts.isEmpty());
106 | mimeParts.add(part);
107 |
108 | long size = part.size();
109 | if (size == -1) {
110 | length = -1;
111 | } else if (length != -1) {
112 | length += size;
113 | }
114 | }
115 |
116 | public int getPartCount() {
117 | return mimeParts.size();
118 | }
119 |
120 | @Override public String fileName() {
121 | return null;
122 | }
123 |
124 | @Override public String mimeType() {
125 | return "multipart/form-data; boundary=" + boundary;
126 | }
127 |
128 | @Override public long length() {
129 | return length;
130 | }
131 |
132 | @Override public void writeTo(OutputStream out) throws IOException {
133 | for (MimePart part : mimeParts) {
134 | part.writeTo(out);
135 | }
136 | out.write(footer);
137 | }
138 |
139 | private static byte[] buildBoundary(String boundary, boolean first, boolean last) {
140 | try {
141 | // Pre-size for the last boundary, the worst case scenario.
142 | StringBuilder sb = new StringBuilder(boundary.length() + 8);
143 |
144 | if (!first) {
145 | sb.append("\r\n");
146 | }
147 | sb.append("--");
148 | sb.append(boundary);
149 | if (last) {
150 | sb.append("--");
151 | }
152 | sb.append("\r\n");
153 | return sb.toString().getBytes("UTF-8");
154 | } catch (IOException ex) {
155 | throw new RuntimeException("Unable to write multipart boundary", ex);
156 | }
157 | }
158 |
159 | private static byte[] buildHeader(String name, String transferEncoding, TypedOutput value) {
160 | try {
161 | // Initial size estimate based on always-present strings and conservative value lengths.
162 | StringBuilder headers = new StringBuilder(128);
163 |
164 | headers.append("Content-Disposition: form-data; name=\"");
165 | headers.append(name);
166 |
167 | String fileName = value.fileName();
168 | if (fileName != null) {
169 | headers.append("\"; filename=\"");
170 | headers.append(fileName);
171 | }
172 |
173 | headers.append("\"\r\nContent-Type: ");
174 | headers.append(value.mimeType());
175 |
176 | long length = value.length();
177 | if (length != -1) {
178 | headers.append("\r\nContent-Length: ").append(length);
179 | }
180 |
181 | headers.append("\r\nContent-Transfer-Encoding: ");
182 | headers.append(transferEncoding);
183 | headers.append("\r\n\r\n");
184 |
185 | return headers.toString().getBytes("UTF-8");
186 | } catch (IOException ex) {
187 | throw new RuntimeException("Unable to write multipart header", ex);
188 | }
189 | }
190 | }
191 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/loader/custom/CustomTypedByteArray.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.loader.custom;
2 |
3 | import retrofit.mime.TypedByteArray;
4 |
5 | /**
6 | * 重写 TypedByteArray 设置其 fileName 为 "NoName",
7 | * Created by MrFu on 15/12/16.
8 | */
9 | public class CustomTypedByteArray extends TypedByteArray {
10 | /**
11 | * Constructs a new typed byte array. Sets mimeType to {@code application/unknown} if absent.
12 | *
13 | * @param mimeType
14 | * @param bytes
15 | * @throws NullPointerException if bytes are null
16 | */
17 | public CustomTypedByteArray(String mimeType, byte[] bytes) {
18 | super(mimeType, bytes);
19 | }
20 |
21 | @Override
22 | public String fileName() {
23 | return "NoName";
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/models/BaseResponse.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.models;
2 |
3 | import mrfu.rxface.loader.WebServiceException;
4 | import rx.Observable;
5 |
6 | /**
7 | * Created by MrFu on 16/1/10.
8 | */
9 | public class BaseResponse {
10 | public Observable filterWebServiceErrors() {
11 | if (true){//judge result status is ok~~
12 | return Observable.just(this);
13 | }else {
14 | return Observable.error(new WebServiceException("Service return Error message"));
15 | }
16 | //demo code just like blow, the common is a class object, you can deal the error code in here.
17 | // public class BaseResponse {
18 | // public Common common;
19 | //
20 | // public Observable filterWebServiceErrors() {
21 | // if (common == null){
22 | // return Observable.error(new WebServiceException("啊喔,服务器除了点小问题"));
23 | // }else {
24 | // int code = Integer.parseInt(common.status);
25 | // switch (code){
26 | // case Constants.RESULT_OK://正常
27 | // return Observable.just(this);
28 | // case Constants.RESULT_NORMAL_UPDATE://普通升级
29 | // case Constants.RESULT_FORCE_UPDATE://墙纸升级
30 | // if (AppApplication.getInstance().updateModel == null) {
31 | // AppApplication.getInstance().updateModel = common.update;
32 | // AppApplication.getInstance().updateModel.code = code;
33 | // }
34 | // return Observable.just(this);
35 | // default://出错
36 | // return Observable.error(new WebServiceException(BaseResponse.this.common.memo));
37 | // }
38 | // }
39 | // }
40 | // }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/models/FaceResponse.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.models;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * Created by MrFu on 15/12/16.
7 | */
8 | public class FaceResponse extends BaseResponse{
9 |
10 | /**
11 | * face : [{"attribute":{"age":{"range":6,"value":18},"gender":{"confidence":99.9996,"value":"Male"},"race":{"confidence":99.8977,"value":"White"},"smiling":{"value":81.2229}},"face_id":"5bf244c54d5731974e25ee024b950cd3","position":{"center":{"x":47.833333,"y":49.036403},"eye_left":{"x":42.140333,"y":42.28394},"eye_right":{"x":53.658,"y":42.389936},"height":31.263383,"mouth_left":{"x":42.352167,"y":56.311991},"mouth_right":{"x":53.747833,"y":56.89379},"nose":{"x":47.392667,"y":51.794004},"width":24.333333},"tag":""},{"attribute":{"age":{"range":5,"value":24},"gender":{"confidence":99.5758,"value":"Male"},"race":{"confidence":99.94800000000001,"value":"White"},"smiling":{"value":93.0865}},"face_id":"092cb7660d3f8d115b8af331465624a1","position":{"center":{"x":83.833333,"y":52.462527},"eye_left":{"x":77.052667,"y":47.005353},"eye_right":{"x":87.198,"y":44.253961},"height":28.265525,"mouth_left":{"x":77.304167,"y":60.063169},"mouth_right":{"x":86.635167,"y":60.254176},"nose":{"x":86.9965,"y":54.840685},"width":22},"tag":""},{"attribute":{"age":{"range":11,"value":38},"gender":{"confidence":74.2956,"value":"Female"},"race":{"confidence":96.8155,"value":"White"},"smiling":{"value":86.556}},"face_id":"3c9d898f732c84d07d760f184bfec814","position":{"center":{"x":13,"y":52.248394},"eye_left":{"x":8.483217,"y":44.584797},"eye_right":{"x":18.669667,"y":46.517987},"height":27.837259,"mouth_left":{"x":8.44005,"y":60.162955},"mouth_right":{"x":17.914333,"y":60.197645},"nose":{"x":8.59085,"y":53.623769},"width":21.666667},"tag":""}]
12 | * img_height : 490
13 | * img_id : f3f8c2826537ce51ca1995143e8b9289
14 | * img_width : 629
15 | * session_id : a7f871065a064bdfabe06de48189dcac
16 | * url : http://imglife.gmw.cn/attachement/jpg/site2/20111014/002564a5d7d21002188831.jpg
17 | */
18 |
19 | public int img_height;
20 | public String img_id;
21 | public int img_width;
22 | public String session_id;
23 | public String url;
24 | /**
25 | * attribute : {"age":{"range":6,"value":18},"gender":{"confidence":99.9996,"value":"Male"},"race":{"confidence":99.8977,"value":"White"},"smiling":{"value":81.2229}}
26 | * face_id : 5bf244c54d5731974e25ee024b950cd3
27 | * position : {"center":{"x":47.833333,"y":49.036403},"eye_left":{"x":42.140333,"y":42.28394},"eye_right":{"x":53.658,"y":42.389936},"height":31.263383,"mouth_left":{"x":42.352167,"y":56.311991},"mouth_right":{"x":53.747833,"y":56.89379},"nose":{"x":47.392667,"y":51.794004},"width":24.333333}
28 | * tag :
29 | */
30 |
31 | public List face;
32 |
33 | public static class FaceEntity {
34 | /**
35 | * age : {"range":6,"value":18}
36 | * gender : {"confidence":99.9996,"value":"Male"}
37 | * race : {"confidence":99.8977,"value":"White"}
38 | * smiling : {"value":81.2229}
39 | */
40 |
41 | public AttributeEntity attribute;
42 | public String face_id;
43 | /**
44 | * center : {"x":47.833333,"y":49.036403}
45 | * eye_left : {"x":42.140333,"y":42.28394}
46 | * eye_right : {"x":53.658,"y":42.389936}
47 | * height : 31.263383
48 | * mouth_left : {"x":42.352167,"y":56.311991}
49 | * mouth_right : {"x":53.747833,"y":56.89379}
50 | * nose : {"x":47.392667,"y":51.794004}
51 | * width : 24.333333
52 | */
53 |
54 | public PositionEntity position;
55 | public String tag;
56 |
57 | public static class AttributeEntity {
58 | /**
59 | * range : 6
60 | * value : 18
61 | */
62 |
63 | public AgeEntity age;
64 | /**
65 | * confidence : 99.9996
66 | * value : Male
67 | */
68 |
69 | public GenderEntity gender;
70 | /**
71 | * confidence : 99.8977
72 | * value : White
73 | */
74 |
75 | public RaceEntity race;
76 | /**
77 | * value : 81.2229
78 | */
79 |
80 | public SmilingEntity smiling;
81 |
82 | public static class AgeEntity {
83 | public int range;
84 | public int value;
85 | }
86 |
87 | public static class GenderEntity {
88 | public double confidence;
89 | public String value;
90 | }
91 |
92 | public static class RaceEntity {
93 | public double confidence;
94 | public String value;
95 | }
96 |
97 | public static class SmilingEntity {
98 | public double value;
99 | }
100 | }
101 |
102 | public static class PositionEntity {
103 | /**
104 | * x : 47.833333
105 | * y : 49.036403
106 | */
107 |
108 | public CenterEntity center;
109 | /**
110 | * x : 42.140333
111 | * y : 42.28394
112 | */
113 |
114 | public EyeLeftEntity eye_left;
115 | /**
116 | * x : 53.658
117 | * y : 42.389936
118 | */
119 |
120 | public EyeRightEntity eye_right;
121 | public double height;
122 | /**
123 | * x : 42.352167
124 | * y : 56.311991
125 | */
126 |
127 | public MouthLeftEntity mouth_left;
128 | /**
129 | * x : 53.747833
130 | * y : 56.89379
131 | */
132 |
133 | public MouthRightEntity mouth_right;
134 | /**
135 | * x : 47.392667
136 | * y : 51.794004
137 | */
138 |
139 | public NoseEntity nose;
140 | public double width;
141 |
142 | public static class CenterEntity {
143 | public double x;
144 | public double y;
145 | }
146 |
147 | public static class EyeLeftEntity {
148 | public double x;
149 | public double y;
150 | }
151 |
152 | public static class EyeRightEntity {
153 | public double x;
154 | public double y;
155 | }
156 |
157 | public static class MouthLeftEntity {
158 | public double x;
159 | public double y;
160 | }
161 |
162 | public static class MouthRightEntity {
163 | public double x;
164 | public double y;
165 | }
166 |
167 | public static class NoseEntity {
168 | public double x;
169 | public double y;
170 | }
171 | }
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/app/src/main/java/mrfu/rxface/models/NeedDataEntity.java:
--------------------------------------------------------------------------------
1 | package mrfu.rxface.models;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | /**
6 | * Created by MrFu on 15/12/16.
7 | */
8 | public class NeedDataEntity {
9 | public Bitmap bitmap;
10 | public String displayStr;
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/jobs.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/drawable/jobs.jpg
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
15 |
16 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
17 |
18 |
23 |
24 |
25 |
30 |
31 |
36 |
37 |
47 |
48 |
54 |
55 |
65 |
66 |
72 |
73 |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-hdpi/logo.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-mdpi/logo.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-xhdpi/logo.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-xxhdpi/logo.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/app/src/main/res/mipmap-xxxhdpi/logo.png
--------------------------------------------------------------------------------
/app/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 | >
2 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 | 16dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | RxFace
3 | Settings
4 | Github
5 | Get识别
6 | Post识别
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/test/java/mrfu/face/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package mrfu.face;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:1.3.0'
9 | // classpath 'me.tatarka:gradle-retrolambda:3.2.3'
10 |
11 | // NOTE: Do not place your application dependencies here; they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | jcenter()
19 | }
20 | }
21 |
22 | task clean(type: Delete) {
23 | delete rootProject.buildDir
24 | }
25 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Dec 15 21:05:14 CST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/images/image_screen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/images/image_screen.png
--------------------------------------------------------------------------------
/images/movie_screen.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MrFuFuFu/RxFace/f8753b8d76d183d7459a80d057c14ea6f4ec4253/images/movie_screen.gif
--------------------------------------------------------------------------------
/sdk_unuse/HttpRequests.java:
--------------------------------------------------------------------------------
1 | package mrfu.face.sdk;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.net.HttpURLConnection;
6 | import java.net.URL;
7 | import java.util.Date;
8 |
9 | import org.apache.http.entity.mime.MultipartEntity;
10 | import org.apache.http.entity.mime.content.StringBody;
11 | import org.json.JSONException;
12 | import org.json.JSONObject;
13 |
14 | import com.facepp.error.FaceppParseException;
15 |
16 | /**
17 | * request to faceplusplus.com
18 | * {@code new HttpRequests(apiKey, apiSecret).request("detection", "detect", postParameters)}
19 | *
20 | * {@code new HttpRequests(apiKey, apiSecret).train()}
21 | * @author moon5ckq
22 | * @since 1.0.0
23 | * @version 1.3.0
24 | */
25 | public class HttpRequests {
26 |
27 | static final private String WEBSITE_CN = "https://apicn.faceplusplus.com/v2/";
28 | static final private String DWEBSITE_CN = "http://apicn.faceplusplus.com/v2/";
29 | static final private String WEBSITE_US = "https://apius.faceplusplus.com/v2/";
30 | static final private String DWEBSITE_US = "http://apius.faceplusplus.com/v2/";
31 |
32 | static final private int BUFFERSIZE = 1048576;
33 | static final private int TIMEOUT = 30000;
34 | static final private int SYNC_TIMEOUT = 60000;
35 |
36 | private String webSite;
37 | private String apiKey, apiSecret;
38 | private PostParameters params;
39 | private int httpTimeOut = TIMEOUT;
40 |
41 |
42 | /**
43 | * default is 30 sec
44 | * set http timeout limit (million second)
45 | * @param timeOut
46 | */
47 | public void setHttpTimeOut(int timeOut) {
48 | this.httpTimeOut = timeOut;
49 | }
50 |
51 | /**
52 | * (million second)
53 | * @return http timeout limit
54 | */
55 | public int getHttpTimeOut() {
56 | return this.httpTimeOut;
57 | }
58 |
59 | /**
60 | * @return api_key
61 | */
62 | public String getApiKey() {
63 | return apiKey;
64 | }
65 |
66 | /**
67 | * @param apiKey
68 | */
69 | public void setApiKey(String apiKey) {
70 | this.apiKey = apiKey;
71 | }
72 |
73 | /**
74 | * @return api_secret
75 | */
76 | public String getApiSecret() {
77 | return apiSecret;
78 | }
79 |
80 | /**
81 | * @param apiSecret
82 | */
83 | public void setApiSecret(String apiSecret) {
84 | this.apiSecret = apiSecret;
85 | }
86 |
87 | /**
88 | * if isCN is true, then use AliCloud, false to Amazon
89 | * if isDebug is true, then use http, otherwise https
90 | * @param isCN
91 | * @param isDebug
92 | */
93 | public void setWebSite(boolean isCN, boolean isDebug) {
94 | if (isCN && isDebug) webSite = DWEBSITE_CN;
95 | else if (isCN && !isDebug) webSite = WEBSITE_CN;
96 | else if (!isCN && isDebug) webSite = DWEBSITE_US;
97 | else if (!isCN && !isDebug) webSite = WEBSITE_US;
98 | }
99 |
100 | /**
101 | * @return a webSite clone
102 | */
103 | public String getWebSite() {
104 | return new String(webSite);
105 | }
106 |
107 | /**
108 | * {@link #request(String, String, PostParameters)}
109 | * faceplusplus.com/[control]/[action]
110 | * default use parameters which {@link #getParams}
111 | * @param control
112 | * @param action
113 | * @return a result object
114 | */
115 | public JSONObject request(String control, String action) throws FaceppParseException {
116 | return request(control, action, getParams());
117 | }
118 |
119 |
120 | /**
121 | * default timeout time is 1 minute
122 | * @param sessionId
123 | * @return the getSession Result
124 | * @throws FaceppParseException
125 | */
126 | public JSONObject getSessionSync(String sessionId) throws FaceppParseException {
127 | return getSessionSync(sessionId, SYNC_TIMEOUT);
128 | }
129 |
130 | /**
131 | * timeout time is [timeOut]ms, the method is synchronized.
132 | * @param sessionId
133 | * @param timeOut
134 | * @return the getSession Result
135 | * @throws FaceppParseException
136 | */
137 | public JSONObject getSessionSync(String sessionId, long timeOut) throws FaceppParseException {
138 | final StringBuilder sb = new StringBuilder();
139 | long t = new Date().getTime() + timeOut;
140 | while (true) {
141 | JSONObject rst = HttpRequests.this.request("info", "get_session", new PostParameters().setSessionId(sessionId));
142 | try {
143 | if (rst.getString("status").equals("SUCC")) {
144 | sb.append(rst.toString());
145 | break;
146 | } else if (rst.getString("status").equals("INVALID_SESSION")) {
147 | sb.append("INVALID_SESSION");
148 | break;
149 | }
150 | } catch (JSONException e) {
151 | sb.append("Unknow error.");
152 | break;
153 | }
154 | try {
155 | Thread.sleep(1000);
156 | } catch (InterruptedException e) {
157 | sb.append("Thread.sleep error.");
158 | break;
159 | }
160 | if (new Date().getTime() >= t) {
161 | sb.append("Time Out");
162 | break;
163 | }
164 | }
165 |
166 | String rst = sb.toString();
167 | if (rst.equals("INVALID_SESSION")) {
168 | throw new FaceppParseException("Invaild session, unknow error.");
169 | } else if (rst.equals("Unknow error.")) {
170 | throw new FaceppParseException("Unknow error.");
171 | } else if (rst.equals("Thread.sleep error.")) {
172 | throw new FaceppParseException("Thread.sleep error.");
173 | } else if (rst.equals("Time Out")) {
174 | throw new FaceppParseException("Get session time out.");
175 | } else {
176 | try {
177 | JSONObject result = new JSONObject(rst);
178 | result.put("response_code", 200);
179 | return result;
180 | } catch (JSONException e) {
181 | }
182 | }
183 |
184 | return null;
185 | }
186 |
187 | /**
188 | * faceplusplus.com/[control]/[action]?[params]
189 | * http request timeout time is 5000ms
190 | * @param control
191 | * @param action
192 | * @param params
193 | * @return a result object
194 | * @throws FaceppParseException
195 | */
196 | public JSONObject request(String control, String action, PostParameters params) throws FaceppParseException {
197 | URL url;
198 | HttpURLConnection urlConn = null;
199 | try {
200 | url = new URL(webSite+control+"/"+action);
201 | urlConn = (HttpURLConnection) url.openConnection();
202 | urlConn.setRequestMethod("POST");
203 | urlConn.setConnectTimeout(httpTimeOut);
204 | urlConn.setReadTimeout(httpTimeOut);
205 | urlConn.setDoOutput(true);
206 |
207 | urlConn.setRequestProperty("connection", "keep-alive");
208 | urlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + params.boundaryString());
209 |
210 | MultipartEntity reqEntity =params.getMultiPart();
211 |
212 | reqEntity.addPart("api_key", new StringBody(apiKey));
213 | reqEntity.addPart("api_secret", new StringBody(apiSecret));
214 |
215 | reqEntity.writeTo(urlConn.getOutputStream());
216 |
217 | String resultString = null;
218 | if (urlConn.getResponseCode() == 200)
219 | resultString = readString(urlConn.getInputStream());
220 | else
221 | resultString = readString(urlConn.getErrorStream());
222 | //FaceppResult result = new FaceppResult( new JSONObject(resultString), urlConn.getResponseCode());
223 |
224 | JSONObject result = new JSONObject(resultString);
225 | if (result.has("error")) {
226 |
227 | if (result.getString("error").equals("API not found"))
228 | throw new FaceppParseException("API not found");
229 |
230 | throw new FaceppParseException("API error.", result.getInt("error_code"),
231 | result.getString("error"), urlConn.getResponseCode());
232 | }
233 | result.put("response_code", urlConn.getResponseCode());
234 | urlConn.getInputStream().close();
235 |
236 | return result;
237 | } catch (Exception e) { throw new FaceppParseException("error :" + e.toString());
238 |
239 | } finally { if (urlConn != null)
240 | urlConn.disconnect();
241 |
242 | }
243 | }
244 |
245 | private static String readString(InputStream is) {
246 | StringBuffer rst = new StringBuffer();
247 |
248 | byte[] buffer = new byte[BUFFERSIZE];
249 | int len = 0;
250 |
251 | try {
252 | while ((len = is.read(buffer)) > 0)
253 | for (int i = 0; i < len; ++i)
254 | rst.append((char)buffer[i]);
255 | } catch (IOException e) {
256 | e.printStackTrace();
257 | }
258 |
259 | return rst.toString();
260 | }
261 |
262 | /**
263 | * create {@link HttpRequests}
264 | * api_key=...,api_secret=...
265 | * use https and AliCloud default
266 | * @param apiKey
267 | * @param apiSecret
268 | */
269 | public HttpRequests(String apiKey, String apiSecret) {
270 | super();
271 | this.apiKey = apiKey;
272 | this.apiSecret = apiSecret;
273 | this.webSite = WEBSITE_CN;
274 | }
275 |
276 | /**
277 | * use https default
278 | * create a empty {@link HttpRequests} object
279 | */
280 | public HttpRequests() {
281 | super();
282 | }
283 |
284 | /**
285 | * create {@link HttpRequests}
286 | * api_key=...,api_secret=...
287 | * the isCN and isDebug use like {@link setWebSite}}
288 | * @param apiKey
289 | * @param apiSecret
290 | * @param isCN
291 | * @param isDebug
292 | */
293 | public HttpRequests(String apiKey, String apiSecret, boolean isCN, boolean isDebug) {
294 | super();
295 | this.apiKey = apiKey;
296 | this.apiSecret = apiSecret;
297 | setWebSite(isCN, isDebug);
298 | }
299 |
300 | /**
301 | * @return {@link PostParameters} object
302 | */
303 | public PostParameters getParams() {
304 | if (params == null) params = new PostParameters();
305 | return params;
306 | }
307 |
308 | /**
309 | * set default PostParameters
310 | * @param params
311 | */
312 | public void setParams(PostParameters params) {
313 | this.params = params;
314 | }
315 |
316 | /**
317 | * used by offline detect
318 | * example: request.offlineDetect(detecter.getImageByteArray(), detecter.getResultJsonString());
319 | * @param image
320 | * @param jsonResult
321 | * @return
322 | * @throws FaceppParseException
323 | */
324 | public JSONObject offlineDetect(byte[] image, String jsonResult) throws FaceppParseException {
325 | return offlineDetect(image, jsonResult, this.params);
326 | }
327 | /**
328 | * used by offline detect
329 | * example: request.offlineDetect(detecter.getImageByteArray(), detecter.getResultJsonString(), params);
330 | * @param image
331 | * @param jsonResult
332 | * @param params
333 | * @return
334 | * @throws FaceppParseException
335 | */
336 | public JSONObject offlineDetect(byte[] image, String jsonResult, PostParameters params) throws FaceppParseException{
337 | if (params == null) params = new PostParameters();
338 | params.setImg(image);
339 | params.setMode("offline");
340 | params.addAttribute("offline_result", jsonResult);
341 | return request("detection", "detect", params);
342 | }
343 |
344 | //all api here
345 | public JSONObject detectionDetect() throws FaceppParseException {
346 | return request("detection", "detect");
347 | }
348 | public JSONObject detectionDetect(PostParameters params) throws FaceppParseException{
349 | return request("detection", "detect", params);
350 | }
351 |
352 | public JSONObject detectionLandmark() throws FaceppParseException {
353 | return request("detection", "landmark");
354 | }
355 | public JSONObject detectionLandmark(PostParameters params) throws FaceppParseException{
356 | return request("detection", "landmark", params);
357 | }
358 |
359 | public JSONObject trainVerify() throws FaceppParseException {
360 | return request("train", "verify");
361 | }
362 | public JSONObject trainVerify(PostParameters params) throws FaceppParseException{
363 | return request("train", "verify", params);
364 | }
365 |
366 | public JSONObject trainSearch() throws FaceppParseException {
367 | return request("train", "search");
368 | }
369 | public JSONObject trainSearch(PostParameters params) throws FaceppParseException{
370 | return request("train", "search", params);
371 | }
372 |
373 | public JSONObject trainIdentify() throws FaceppParseException {
374 | return request("train", "identify");
375 | }
376 | public JSONObject trainIdentify(PostParameters params) throws FaceppParseException{
377 | return request("train", "identify", params);
378 | }
379 |
380 | public JSONObject recognitionCompare() throws FaceppParseException {
381 | return request("recognition", "compare");
382 | }
383 | public JSONObject recognitionCompare(PostParameters params) throws FaceppParseException{
384 | return request("recognition", "compare", params);
385 | }
386 |
387 | public JSONObject recognitionVerify() throws FaceppParseException {
388 | return request("recognition", "verify");
389 | }
390 | public JSONObject recognitionVerify(PostParameters params) throws FaceppParseException{
391 | return request("recognition", "verify", params);
392 | }
393 |
394 | public JSONObject recognitionSearch() throws FaceppParseException {
395 | return request("recognition", "search");
396 | }
397 | public JSONObject recognitionSearch(PostParameters params) throws FaceppParseException{
398 | return request("recognition", "search", params);
399 | }
400 |
401 | public JSONObject recognitionIdentify() throws FaceppParseException {
402 | return request("recognition", "identify");
403 | }
404 | public JSONObject recognitionIdentify(PostParameters params) throws FaceppParseException{
405 | return request("recognition", "identify", params);
406 | }
407 |
408 | public JSONObject groupingGrouping() throws FaceppParseException {
409 | return request("grouping", "grouping");
410 | }
411 | public JSONObject groupingGrouping(PostParameters params) throws FaceppParseException{
412 | return request("grouping", "grouping", params);
413 | }
414 |
415 | public JSONObject personCreate() throws FaceppParseException {
416 | return request("person", "create");
417 | }
418 | public JSONObject personCreate(PostParameters params) throws FaceppParseException{
419 | return request("person", "create", params);
420 | }
421 |
422 | public JSONObject personDelete() throws FaceppParseException {
423 | return request("person", "delete");
424 | }
425 | public JSONObject personDelete(PostParameters params) throws FaceppParseException{
426 | return request("person", "delete", params);
427 | }
428 |
429 | public JSONObject personAddFace() throws FaceppParseException {
430 | return request("person", "add_face");
431 | }
432 | public JSONObject personAddFace(PostParameters params) throws FaceppParseException{
433 | return request("person", "add_face", params);
434 | }
435 |
436 | public JSONObject personRemoveFace() throws FaceppParseException {
437 | return request("person", "remove_face");
438 | }
439 | public JSONObject personRemoveFace(PostParameters params) throws FaceppParseException{
440 | return request("person", "remove_face", params);
441 | }
442 |
443 | public JSONObject personSetInfo() throws FaceppParseException {
444 | return request("person", "set_info");
445 | }
446 | public JSONObject personSetInfo(PostParameters params) throws FaceppParseException{
447 | return request("person", "set_info", params);
448 | }
449 |
450 | public JSONObject personGetInfo() throws FaceppParseException {
451 | return request("person", "get_info");
452 | }
453 | public JSONObject personGetInfo(PostParameters params) throws FaceppParseException{
454 | return request("person", "get_info", params);
455 | }
456 |
457 | public JSONObject facesetCreate() throws FaceppParseException {
458 | return request("faceset", "create");
459 | }
460 | public JSONObject facesetCreate(PostParameters params) throws FaceppParseException{
461 | return request("faceset", "create", params);
462 | }
463 |
464 | public JSONObject facesetDelete() throws FaceppParseException {
465 | return request("faceset", "delete");
466 | }
467 | public JSONObject facesetDelete(PostParameters params) throws FaceppParseException{
468 | return request("faceset", "delete", params);
469 | }
470 |
471 | public JSONObject facesetAddFace() throws FaceppParseException {
472 | return request("faceset", "add_face");
473 | }
474 | public JSONObject facesetAddFace(PostParameters params) throws FaceppParseException{
475 | return request("faceset", "add_face", params);
476 | }
477 |
478 | public JSONObject facesetRemoveFace() throws FaceppParseException {
479 | return request("faceset", "remove_face");
480 | }
481 | public JSONObject facesetRemoveFace(PostParameters params) throws FaceppParseException{
482 | return request("faceset", "remove_face", params);
483 | }
484 |
485 | public JSONObject facesetSetInfo() throws FaceppParseException {
486 | return request("faceset", "set_info");
487 | }
488 | public JSONObject facesetSetInfo(PostParameters params) throws FaceppParseException{
489 | return request("faceset", "set_info", params);
490 | }
491 |
492 | public JSONObject facesetGetInfo() throws FaceppParseException {
493 | return request("faceset", "get_info");
494 | }
495 | public JSONObject facesetGetInfo(PostParameters params) throws FaceppParseException{
496 | return request("faceset", "get_info", params);
497 | }
498 |
499 | public JSONObject groupCreate() throws FaceppParseException {
500 | return request("group", "create");
501 | }
502 | public JSONObject groupCreate(PostParameters params) throws FaceppParseException{
503 | return request("group", "create", params);
504 | }
505 |
506 | public JSONObject groupDelete() throws FaceppParseException {
507 | return request("group", "delete");
508 | }
509 | public JSONObject groupDelete(PostParameters params) throws FaceppParseException{
510 | return request("group", "delete", params);
511 | }
512 |
513 | public JSONObject groupAddPerson() throws FaceppParseException {
514 | return request("group", "add_person");
515 | }
516 | public JSONObject groupAddPerson(PostParameters params) throws FaceppParseException{
517 | return request("group", "add_person", params);
518 | }
519 |
520 | public JSONObject groupRemovePerson() throws FaceppParseException {
521 | return request("group", "remove_person");
522 | }
523 | public JSONObject groupRemovePerson(PostParameters params) throws FaceppParseException{
524 | return request("group", "remove_person", params);
525 | }
526 |
527 | public JSONObject groupSetInfo() throws FaceppParseException {
528 | return request("group", "set_info");
529 | }
530 | public JSONObject groupSetInfo(PostParameters params) throws FaceppParseException{
531 | return request("group", "set_info", params);
532 | }
533 |
534 | public JSONObject groupGetInfo() throws FaceppParseException {
535 | return request("group", "get_info");
536 | }
537 | public JSONObject groupGetInfo(PostParameters params) throws FaceppParseException{
538 | return request("group", "get_info", params);
539 | }
540 |
541 | public JSONObject infoGetImage() throws FaceppParseException {
542 | return request("info", "get_image");
543 | }
544 | public JSONObject infoGetImage(PostParameters params) throws FaceppParseException{
545 | return request("info", "get_image", params);
546 | }
547 |
548 | public JSONObject infoGetFace() throws FaceppParseException {
549 | return request("info", "get_face");
550 | }
551 | public JSONObject infoGetFace(PostParameters params) throws FaceppParseException{
552 | return request("info", "get_face", params);
553 | }
554 |
555 | public JSONObject infoGetPersonList() throws FaceppParseException {
556 | return request("info", "get_person_list");
557 | }
558 | public JSONObject infoGetPersonList(PostParameters params) throws FaceppParseException{
559 | return request("info", "get_person_list", params);
560 | }
561 |
562 | public JSONObject infoGetFacesetList() throws FaceppParseException {
563 | return request("info", "get_faceset_list");
564 | }
565 | public JSONObject infoGetFacesetList(PostParameters params) throws FaceppParseException{
566 | return request("info", "get_faceset_list", params);
567 | }
568 |
569 | public JSONObject infoGetGroupList() throws FaceppParseException {
570 | return request("info", "get_group_list");
571 | }
572 | public JSONObject infoGetGroupList(PostParameters params) throws FaceppParseException{
573 | return request("info", "get_group_list", params);
574 | }
575 |
576 | public JSONObject infoGetSession() throws FaceppParseException {
577 | return request("info", "get_session");
578 | }
579 | public JSONObject infoGetSession(PostParameters params) throws FaceppParseException{
580 | return request("info", "get_session", params);
581 | }
582 |
583 | /**
584 | * @deprecated this api is deprecated
585 | * @return
586 | * @throws FaceppParseException
587 | */
588 | public JSONObject infoGetQuota() throws FaceppParseException {
589 | return request("info", "get_quota");
590 | }
591 | /**
592 | * @deprecated this api is deprecated
593 | * @return
594 | * @throws FaceppParseException
595 | */
596 | public JSONObject infoGetQuota(PostParameters params) throws FaceppParseException{
597 | return request("info", "get_quota", params);
598 | }
599 |
600 | public JSONObject infoGetApp() throws FaceppParseException {
601 | return request("info", "get_app");
602 | }
603 | public JSONObject infoGetApp(PostParameters params) throws FaceppParseException{
604 | return request("info", "get_app", params);
605 | }
606 |
607 | }
608 |
--------------------------------------------------------------------------------
/sdk_unuse/MainActivity.java:
--------------------------------------------------------------------------------
1 | package mrfu.face.sdk;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.database.Cursor;
6 | import android.graphics.Bitmap;
7 | import android.graphics.BitmapFactory;
8 | import android.graphics.BitmapFactory.Options;
9 | import android.graphics.Canvas;
10 | import android.graphics.Color;
11 | import android.graphics.Matrix;
12 | import android.graphics.Paint;
13 | import android.os.Bundle;
14 | import android.provider.MediaStore.Images.ImageColumns;
15 | import android.util.Log;
16 | import android.view.Menu;
17 | import android.view.View;
18 | import android.view.View.OnClickListener;
19 | import android.widget.Button;
20 | import android.widget.ImageView;
21 | import android.widget.TextView;
22 |
23 | import com.facepp.error.FaceppParseException;
24 |
25 | import org.json.JSONException;
26 | import org.json.JSONObject;
27 |
28 | import java.io.ByteArrayOutputStream;
29 |
30 | import mrfu.face.R;
31 |
32 | /**
33 | * A simple demo, get a picture form your phone
34 | * Use the facepp api to detect
35 | * Find all face on the picture, and mark them out.
36 | * @author moon5ckq
37 | */
38 | public class MainActivity extends Activity {
39 |
40 | final private static String TAG = "MainActivity";
41 | final private int PICTURE_CHOOSE = 1;
42 |
43 | private ImageView imageView = null;
44 | private Bitmap img = null;
45 | private Button buttonDetect = null;
46 | private TextView textView = null;
47 |
48 |
49 | @Override
50 | public void onCreate(Bundle savedInstanceState) {
51 | super.onCreate(savedInstanceState);
52 | setContentView(R.layout.activity_main);
53 |
54 | // Button button = (Button)this.findViewById(R.id.button1);
55 | // button.setOnClickListener(new OnClickListener() {
56 | //
57 | // public void onClick(View arg0) {
58 | // //get a picture form your phone
59 | // Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
60 | // photoPickerIntent.setType("image/*");
61 | // startActivityForResult(photoPickerIntent, PICTURE_CHOOSE);
62 | // }
63 | // });
64 |
65 | // textView = (TextView)this.findViewById(R.id.textView1);
66 |
67 | // buttonDetect = (Button)this.findViewById(R.id.button2);
68 | buttonDetect.setVisibility(View.INVISIBLE);
69 | buttonDetect.setOnClickListener(new OnClickListener() {
70 | public void onClick(View arg0) {
71 |
72 | textView.setText("Waiting ...");
73 |
74 | FaceppDetect faceppDetect = new FaceppDetect();
75 | faceppDetect.setDetectCallback(new DetectCallback() {
76 |
77 | public void detectResult(JSONObject rst) {
78 | //Log.v(TAG, rst.toString());
79 |
80 | //use the red paint
81 | Paint paint = new Paint();
82 | paint.setColor(Color.RED);
83 | paint.setStrokeWidth(Math.max(img.getWidth(), img.getHeight()) / 100f);
84 |
85 | //create a new canvas
86 | Bitmap bitmap = Bitmap.createBitmap(img.getWidth(), img.getHeight(), img.getConfig());
87 | Canvas canvas = new Canvas(bitmap);
88 | canvas.drawBitmap(img, new Matrix(), null);
89 |
90 |
91 | try {
92 | //find out all faces
93 | final int count = rst.getJSONArray("face").length();
94 | for (int i = 0; i < count; ++i) {
95 | float x, y, w, h;
96 | //get the center point
97 | x = (float)rst.getJSONArray("face").getJSONObject(i)
98 | .getJSONObject("position").getJSONObject("center").getDouble("x");
99 | y = (float)rst.getJSONArray("face").getJSONObject(i)
100 | .getJSONObject("position").getJSONObject("center").getDouble("y");
101 |
102 | //get face size
103 | w = (float)rst.getJSONArray("face").getJSONObject(i)
104 | .getJSONObject("position").getDouble("width");
105 | h = (float)rst.getJSONArray("face").getJSONObject(i)
106 | .getJSONObject("position").getDouble("height");
107 |
108 | //change percent value to the real size
109 | x = x / 100 * img.getWidth();
110 | w = w / 100 * img.getWidth() * 0.7f;
111 | y = y / 100 * img.getHeight();
112 | h = h / 100 * img.getHeight() * 0.7f;
113 |
114 | //draw the box to mark it out
115 | canvas.drawLine(x - w, y - h, x - w, y + h, paint);
116 | canvas.drawLine(x - w, y - h, x + w, y - h, paint);
117 | canvas.drawLine(x + w, y + h, x - w, y + h, paint);
118 | canvas.drawLine(x + w, y + h, x + w, y - h, paint);
119 | }
120 |
121 | //save new image
122 | img = bitmap;
123 |
124 | MainActivity.this.runOnUiThread(new Runnable() {
125 |
126 | public void run() {
127 | //show the image
128 | imageView.setImageBitmap(img);
129 | textView.setText("Finished, "+ count + " faces.");
130 | }
131 | });
132 |
133 | } catch (JSONException e) {
134 | e.printStackTrace();
135 | MainActivity.this.runOnUiThread(new Runnable() {
136 | public void run() {
137 | textView.setText("Error.");
138 | }
139 | });
140 | }
141 |
142 | }
143 | });
144 | faceppDetect.detect(img);
145 | }
146 | });
147 |
148 | // imageView = (ImageView)this.findViewById(R.id.imageView1);
149 | imageView.setImageBitmap(img);
150 | }
151 |
152 | @Override
153 | public boolean onCreateOptionsMenu(Menu menu) {
154 | // getMenuInflater().inflate(R.menu.activity_main, menu);
155 | return true;
156 | }
157 |
158 | @Override
159 | protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
160 | super.onActivityResult(requestCode, resultCode, intent);
161 |
162 | //the image picker callback
163 | if (requestCode == PICTURE_CHOOSE) {
164 | if (intent != null) {
165 | //The Android api ~~~
166 | //Log.d(TAG, "idButSelPic Photopicker: " + intent.getDataString());
167 | Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null);
168 | cursor.moveToFirst();
169 | int idx = cursor.getColumnIndex(ImageColumns.DATA);
170 | String fileSrc = cursor.getString(idx);
171 | //Log.d(TAG, "Picture:" + fileSrc);
172 |
173 | //just read size
174 | Options options = new Options();
175 | options.inJustDecodeBounds = true;
176 | img = BitmapFactory.decodeFile(fileSrc, options);
177 |
178 | //scale size to read
179 | options.inSampleSize = Math.max(1, (int)Math.ceil(Math.max((double)options.outWidth / 1024f, (double)options.outHeight / 1024f)));
180 | options.inJustDecodeBounds = false;
181 | img = BitmapFactory.decodeFile(fileSrc, options);
182 | textView.setText("Clik Detect. ==>");
183 |
184 |
185 | imageView.setImageBitmap(img);
186 | buttonDetect.setVisibility(View.VISIBLE);
187 | }
188 | else {
189 | Log.d(TAG, "idButSelPic Photopicker canceled");
190 | }
191 | }
192 | }
193 |
194 | private class FaceppDetect {
195 | DetectCallback callback = null;
196 |
197 | public void setDetectCallback(DetectCallback detectCallback) {
198 | callback = detectCallback;
199 | }
200 |
201 | public void detect(final Bitmap image) {
202 |
203 | new Thread(new Runnable() {
204 |
205 | public void run() {
206 | HttpRequests httpRequests = new HttpRequests("4480afa9b8b364e30ba03819f3e9eff5", "Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M", true, false);
207 | //Log.v(TAG, "image size : " + img.getWidth() + " " + img.getHeight());
208 |
209 | ByteArrayOutputStream stream = new ByteArrayOutputStream();
210 | float scale = Math.min(1, Math.min(600f / img.getWidth(), 600f / img.getHeight()));
211 | Matrix matrix = new Matrix();
212 | matrix.postScale(scale, scale);
213 |
214 | Bitmap imgSmall = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, false);
215 | //Log.v(TAG, "imgSmall size : " + imgSmall.getWidth() + " " + imgSmall.getHeight());
216 |
217 | imgSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);
218 | byte[] array = stream.toByteArray();
219 |
220 | try {
221 | //detect
222 | JSONObject result = httpRequests.detectionDetect(new PostParameters().setImg(array));
223 | //finished , then call the callback function
224 | if (callback != null) {
225 | callback.detectResult(result);
226 | }
227 | } catch (FaceppParseException e) {
228 | e.printStackTrace();
229 | MainActivity.this.runOnUiThread(new Runnable() {
230 | public void run() {
231 | textView.setText("Network error.");
232 | }
233 | });
234 | }
235 |
236 | }
237 | }).start();
238 | }
239 | }
240 |
241 | interface DetectCallback {
242 | void detectResult(JSONObject rst);
243 | }
244 | }
245 |
--------------------------------------------------------------------------------
/sdk_unuse/PostParameters.java:
--------------------------------------------------------------------------------
1 | package mrfu.face.sdk;
2 |
3 | import java.io.File;
4 | import java.io.UnsupportedEncodingException;
5 | import java.nio.charset.Charset;
6 | import java.util.ArrayList;
7 | import java.util.Random;
8 |
9 | import org.apache.http.entity.mime.HttpMultipartMode;
10 | import org.apache.http.entity.mime.MultipartEntity;
11 | import org.apache.http.entity.mime.content.ByteArrayBody;
12 | import org.apache.http.entity.mime.content.FileBody;
13 | import org.apache.http.entity.mime.content.StringBody;
14 |
15 | /**
16 | * Http Multipart
17 | * {@code new PostParameters().setMode("oneface").setImg(new File("...")).setTag("some message")}
18 | * @author moon5kcq
19 | * @since 1.0.0
20 | * @version 1.3.0
21 | */
22 | public class PostParameters {
23 | private MultipartEntity multiPart = null;
24 | private final static int boundaryLength = 32;
25 | private final static String boundaryAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
26 | private String boundary;
27 |
28 | /**
29 | * auto generate boundary string
30 | * @return a boundary string
31 | */
32 | private String getBoundary() {
33 | StringBuilder sb = new StringBuilder();
34 | Random random = new Random();
35 | for (int i = 0; i < boundaryLength; ++i)
36 | sb.append(boundaryAlphabet.charAt(random.nextInt(boundaryAlphabet.length())));
37 | return sb.toString();
38 | }
39 |
40 | /**
41 | * @return get MultipartEntity (apache)
42 | */
43 | public MultipartEntity getMultiPart() {
44 | return multiPart;
45 | }
46 |
47 | /**
48 | * default boundary is auto generate {@link #getBoundary()}
49 | */
50 | public PostParameters() {
51 | super();
52 | boundary = getBoundary();
53 | multiPart = new MultipartEntity(HttpMultipartMode.STRICT , boundary, Charset.forName("UTF-8"));
54 | }
55 |
56 | /**
57 | * @return multipart boundary string
58 | */
59 | public String boundaryString() {
60 | return boundary;
61 | }
62 |
63 | /**
64 | * async=true|false
65 | * @param flag
66 | * @return this
67 | */
68 | public PostParameters setAsync(boolean flag) {
69 | addString("async", ""+flag);
70 | return this;
71 | }
72 |
73 | /**
74 | * url=...
75 | * @param url
76 | * @return this
77 | */
78 | public PostParameters setUrl(String url){
79 | addString("url", url);
80 | return this;
81 | }
82 |
83 | /**
84 | * attribute = gender | age | race | all | none
85 | * @param type
86 | * @return this
87 | */
88 | public PostParameters setAttribute(String type){
89 | addString("attribute", type);
90 | return this;
91 | }
92 |
93 | /**
94 | * tag=...
95 | * @param tag
96 | * @return this
97 | */
98 | public PostParameters setTag(String tag){
99 | addString("tag", tag);
100 | return this;
101 | }
102 |
103 | /**
104 | * img=...
105 | * @param file
106 | * @return this
107 | */
108 | public PostParameters setImg(File file) {
109 | multiPart.addPart("img", new FileBody(file));
110 | return this;
111 | }
112 |
113 | /**
114 | * img=...
115 | * @param data
116 | * @return this
117 | */
118 | public PostParameters setImg(byte[] data) {
119 | setImg(data, "NoName");
120 | return this;
121 | }
122 |
123 | /**
124 | * img=...(name in multipart is ...)
125 | * @param data
126 | * @param fileName
127 | * @return this
128 | */
129 | public PostParameters setImg(byte[] data, String fileName) {
130 | multiPart.addPart("img", new ByteArrayBody(data, fileName));
131 | return this;
132 | }
133 |
134 | /**
135 | * face_id1=...
136 | * @param id
137 | * @return this
138 | */
139 | public PostParameters setFaceId1(String id){
140 | addString("face_id1", id);
141 | return this;
142 | }
143 |
144 | /**
145 | * face_id2=...
146 | * @param id
147 | * @return this
148 | */
149 | public PostParameters setFaceId2(String id){
150 | addString("face_id2", id);
151 | return this;
152 | }
153 |
154 | /**
155 | * group_name=...
156 | * @param groupName
157 | * @return this
158 | */
159 | public PostParameters setGroupName(String groupName){
160 | addString("group_name", groupName);
161 | return this;
162 | }
163 |
164 | /**
165 | * group_id=...
166 | * @param groupId
167 | * @return this
168 | */
169 | public PostParameters setGroupId(String groupId){
170 | addString("group_id", groupId);
171 | return this;
172 | }
173 |
174 | /**
175 | * key_face_id=...
176 | * @param id
177 | * @return this
178 | */
179 | public PostParameters setKeyFaceId(String id){
180 | addString("key_face_id", id);
181 | return this;
182 | }
183 |
184 | /**
185 | * count=...
186 | * @param count
187 | * @return this
188 | */
189 | public PostParameters setCount(int count) {
190 | addString("count", new Integer(count).toString());
191 | return this;
192 | }
193 |
194 | /**
195 | * type= all | search | recognize
196 | * @param type
197 | * @return this
198 | */
199 | public PostParameters setType(String type) {
200 | addString("type", type);
201 | return this;
202 | }
203 |
204 | /**
205 | * face_id=...
206 | * @param faceId
207 | * @return this
208 | */
209 | public PostParameters setFaceId(String faceId) {
210 | addString("face_id", faceId);
211 | return this;
212 | }
213 |
214 | /**
215 | * faceset_id=...
216 | * @param facesetId
217 | * @return this
218 | */
219 | public PostParameters setFacesetId(String facesetId) {
220 | addString("faceset_id", facesetId);
221 | return this;
222 | }
223 |
224 | /**
225 | * faceset_id=..., ..., ...
226 | * @param facesetIds
227 | * @return this
228 | */
229 | public PostParameters setFacesetId(String[] facesetId) {
230 | setFacesetId(toStringList(facesetId));
231 | return this;
232 | }
233 |
234 | /**
235 | * faceset_id=...
236 | * @param facesetIds
237 | * @return this
238 | */
239 | public PostParameters setFacesetId(ArrayList facesetId) {
240 | setFacesetId(toStringList(facesetId));
241 | return this;
242 | }
243 |
244 | /**
245 | * person_id=...
246 | * @param personId
247 | * @return this
248 | */
249 | public PostParameters setPersonId(String personId) {
250 | addString("person_id", personId);
251 | return this;
252 | }
253 |
254 | /**
255 | * person_name=...
256 | * @param personName
257 | * @return this
258 | */
259 | public PostParameters setPersonName(String personName) {
260 | addString("person_name", personName);
261 | return this;
262 | }
263 |
264 | /**
265 | * name=...
266 | * @param name
267 | * @return this
268 | */
269 | public PostParameters setName(String name) {
270 | addString("name", name);
271 | return this;
272 | }
273 |
274 | /**
275 | * session_id=...
276 | * @param id
277 | * @return this
278 | */
279 | public PostParameters setSessionId(String id) {
280 | addString("session_id", id);
281 | return this;
282 | }
283 |
284 | /**
285 | * mode= oneface | normal
286 | * @param type
287 | * @return this
288 | */
289 | public PostParameters setMode(String type) {
290 | addString("mode", type);
291 | return this;
292 | }
293 |
294 |
295 | /**
296 | * face_id=... , ... , ...
297 | * @param faceIds
298 | * @return this
299 | */
300 | public PostParameters setFaceId(String[] faceIds) {
301 | return setFaceId(toStringList(faceIds));
302 | }
303 | /**
304 | * person_id=... , ... , ...
305 | * @param personIds
306 | * @return this
307 | */
308 | public PostParameters setPersonId(String[] personIds) {
309 | return setPersonId(toStringList(personIds));
310 | }
311 | /**
312 | * person_name=... , ... , ...
313 | * @param personNames
314 | * @return this
315 | */
316 | public PostParameters setPersonName(String[] personNames) {
317 | return setPersonName(toStringList(personNames));
318 | }
319 | /**
320 | * group_id=... , ... , ...
321 | * @param groupIds
322 | * @return this
323 | */
324 | public PostParameters setGroupId(String[] groupIds) {
325 | return setGroupId(toStringList(groupIds));
326 | }
327 | /**
328 | * group_name=... , ... , ...
329 | * @param groupNames
330 | * @return this
331 | */
332 | public PostParameters setGroupName(String[] groupNames) {
333 | return setGroupName(toStringList(groupNames));
334 | }
335 |
336 | /**
337 | * face=... , ... , ...
338 | * @param faceIds
339 | * @return this
340 | */
341 | public PostParameters setFaceId(ArrayList faceIds) {
342 | return setFaceId(toStringList(faceIds));
343 | }
344 | /**
345 | * person_id=... , ... , ...
346 | * @param personIds
347 | * @return this
348 | */
349 | public PostParameters setPersonId(ArrayList personIds) {
350 | return setPersonId(toStringList(personIds));
351 | }
352 | /**
353 | * person_name=... , ... , ...
354 | * @param personNames
355 | * @return this
356 | */
357 | public PostParameters setPersonName(ArrayList personNames) {
358 | return setPersonName(toStringList(personNames));
359 | }
360 | /**
361 | * group_id=... , ... , ...
362 | * @param groupIds
363 | * @return this
364 | */
365 | public PostParameters setGroupId(ArrayList groupIds) {
366 | return setGroupId(toStringList(groupIds));
367 | }
368 | /**
369 | * group_name=... , ... , ...
370 | * @param groupNames
371 | * @return this
372 | */
373 | public PostParameters setGroupName(ArrayList groupNames) {
374 | return setGroupName(toStringList(groupNames));
375 | }
376 |
377 | /**
378 | * img_id=...
379 | * @param imgId
380 | * @return this
381 | */
382 | public PostParameters setImgId(String imgId) {
383 | addString("img_id", imgId);
384 | return this;
385 | }
386 |
387 | /**
388 | * faceset_name=...
389 | * @param facesetName
390 | * @return this
391 | */
392 | public PostParameters setFacesetName(String facesetName) {
393 | addString("faceset_name", facesetName);
394 | return this;
395 | }
396 |
397 | /**
398 | * faceset_name=... , ... , ...
399 | * @param facesetNames
400 | * @return this
401 | */
402 | public PostParameters setFacesetName(ArrayList facesetNames) {
403 | return setFacesetName(toStringList(facesetNames));
404 | }
405 | /**
406 | * faceset_name=... , ... , ...
407 | * @param facesetNames
408 | * @return this
409 | */
410 | public PostParameters setFacesetName(String[] facesetNames) {
411 | return setFacesetName(toStringList(facesetNames));
412 | }
413 |
414 | /**
415 | * `attr`=`value`
416 | * @param attr value
417 | * @return this
418 | */
419 | public PostParameters addAttribute(String attr, String value) {
420 | addString(attr, value);
421 | return this;
422 | }
423 |
424 | private void addString(String id, String str) {
425 | try {
426 | multiPart.addPart(id, new StringBody(str, Charset.forName("UTF-8")));
427 | } catch (UnsupportedEncodingException e) {
428 | e.printStackTrace();
429 | }
430 | }
431 |
432 | private String toStringList(String[] sa) {
433 | StringBuilder sb = new StringBuilder();
434 | for (int i = 0; i < sa.length; ++i) {
435 | if (i != 0) sb.append(',');
436 | sb.append(sa[i]);
437 | }
438 |
439 | return sb.toString();
440 | }
441 |
442 | private String toStringList(ArrayList sa) {
443 | StringBuilder sb = new StringBuilder();
444 | for (int i = 0; i < sa.size(); ++i) {
445 | if (i != 0) sb.append(',');
446 | sb.append(sa.get(i));
447 | }
448 |
449 | return sb.toString();
450 | }
451 | }
452 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------