RxFace icon indicating copy to clipboard operation
RxFace copied to clipboard

用 RxJava, Retrofit, Okhttp 处理人脸识别的简单用例

RxFace

??? RxJava, Retrofit, Okhttp ?????????????????????????????????

Overview

????????????????????????????????? Demo, ????????? FacePlusPlus ?????????????????????/detection/detect ??????????????????????????????????????? get ???????????? post ?????????????????????????????????????????? post ???????????????????????????????????????????????????

??? demo ??????????????????????????? Retrofit ???????????? OkHttp????????? RxJava ???????????????????????????????????????????????????????????????????????????View ????????????????????? ButterKnife????????????????????? Glide???

Versions

v1.1

  1. ????????? compose ?????? work thread ???????????????????????? main thread ?????????????????????:???????????????????????????????????????RxWeekend??????RxJava????????????
  2. mSubscription.unsubscribe();
  3. ?????????Service ???????????????????????????

v1.0

Main difficulties

??????????????? get ??????????????? Url ????????????????????????????????????????????????????????????????????????????????? Retrofit ??? get ????????? @QueryMap ??????????????????: Get??????Demo???

??????????????????????????????????????????????????????????????? post ??????????????????????????????post ????????? MultipartTypedOutput???????????? stackoverflow ??????????????????????????????????????????????????? FacePlusPlus ????????? SDK Sample ?????? Httpurlconnection ???????????? post ????????????????????????

[Content-Disposition: form-data; name="api_key", Content-Type: text/plain; charset=US-ASCII, Content-Transfer-Encoding: 8bit]

[Content-Disposition: form-data; name="img"; filename="NoName", Content-Type: application/octet-stream, Content-Transfer-Encoding: binary]

????????? Retrofit ?????????????????????????????????????????????

public static MultipartTypedOutput mulipartData(Bitmap bitmap, String boundary){
   byte[] data = getBitmapByte(bitmap);
   MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();
   multipartTypedOutput.addPart("api_key", new TypedString(Constants.API_KEY));
   multipartTypedOutput.addPart("api_secret", new TypedString(Constants.API_SECRET));
   multipartTypedOutput.addPart("img", new TypedByteArray("application/octet-stream", data));
   return multipartTypedOutput;
}

?????? Sample ???????????????RestAdapter ??????????????????????????????????????????

private RequestInterceptor mRequestInterceptor = new RequestInterceptor() {
   @Override
   public void intercept(RequestFacade request) {
       request.addHeader("connection", "keep-alive");
       request.addHeader("Content-Type", "multipart/form-data; boundary="+ getBoundary() + "; charset=UTF-8");
   }
};

???????????????????????????String???????????????????????????????????????????????????????????????

Content-Disposition: form-data; name="api_key"
Content-Type: text/plain; charset=UTF-8
Content-Length: 32
Content-Transfer-Encoding: 8bit

???????????????????????????:

MultipartTypedOutput ??? final ????????????????????? CustomMultipartTypedOutput????????????????????????????????? boundary ????????????

TypedString ????????????????????????UTF-8?????????????????? AsciiTypeString?????????????????????????????? US-ASCII???

TypedByteArray ???????????? fileName() ?????????????????? null????????????????????????????????? fileName ??? "NoName"?????????????????? CustomTypedByteArray ??????????????? fileName ??? "NoName"???

????????????????????????????????? RestAdapter ??? header ????????? boundary ???????????? CustomMultipartTypedOutput ??? boundary ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?????? body ??????????????????????????????

public static CustomMultipartTypedOutput mulipartData(Bitmap bitmap, String boundary){
   byte[] data = getBitmapByte(bitmap);
   CustomMultipartTypedOutput multipartTypedOutput = new CustomMultipartTypedOutput(boundary);
   multipartTypedOutput.addPart("api_key", "8bit", new AsciiTypeString(Constants.API_KEY));
   multipartTypedOutput.addPart("api_secret", "8bit", new AsciiTypeString(Constants.API_SECRET));
   multipartTypedOutput.addPart("img", new CustomTypedByteArray("application/octet-stream", data));
   return multipartTypedOutput;
}

Preview

image_screen

movie_screen

More about me

Acknowledgments

License

Copyright 2015 MrFu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Bitdeli Badge