百度人脸检测官方文档
其实文档说的很清楚,自己看看一步一步来就能实现。
创建百度账号
有就登陆,没有就去自己注册吧。登陆后选作左边菜单栏,点击人脸识别。
在应用管理中创建应用
创建完成后在应用管理中查看自己API的版本,核对API文档
这里是V3,我们看官方的V3文档人脸检测API
文档很简单,主要调用方式就是Http调用
首先获取Access Token例如下列请求:
这也就是间的GET请求
参数说明:
-
grant_type: 必须参数,固定为client_credentials;
-
client_id: 必须参数,应用的API Key;
-
client_secret: 必须参数,应用的Secret Key;
API Key和Secret Key就在 应用列表中;如图下所示
Android 代码,这里我才用RxJava+Retrofit,如果不会这两个童鞋可以去搜索搜索学习下,会让你代码变得很结构化,看着舒服
先导入包
//AndroidRxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.5'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
实体类
- 获取Token返回的Json数据,我们关心的是 access_token
public class BaiDuToken {
/**
* refresh_token : 25.8f8631333ad77fb8d216d09cb00ba8dc.315360000.1872732064.282335-16121838
* expires_in : 2592000
* session_key : 9mzpoiOmdxX+8Xu7v+s3ScD6Eauk/E7CYzXsd+QLra09zKThNweb5g+mvHU8KGdnKwgIy+w0I43a+eArbJnpInEITpeRgA==
* access_token : 24.ef5328fadwe35bff02fd6c4de0558.2592000.1559964064.282335-16121838
* scope : vis-faceverify_FACE_Police audio_tts_post public brain_all_scope vis-faceverify_faceverify_h5-face-liveness brain_body_analysis brain_body_attr vis-faceverify_FACE_V3 brain_gesture_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi
* session_secret : bf63738180a0eb6643262565e173be22263
*/
private String refresh_token;
private int expires_in;
private String session_key;
private String access_token;
private String scope;
private String session_secret;
}
- 请求人脸检测的实体类
public class BodyDetect {
/**
* 必填
* 图片信息(总数据大小应小于10M),图片上传方式根据image_type来判断
*/
private String image;
/**
* 必填
* 图片类型
* BASE64:图片的base64值,base64编码后的图片数据,编码后的图片大小不超过2M;
* URL:图片的 URL地址( 可能由于网络等原因导致下载图片时间过长);
* FACE_TOKEN: 人脸图片的唯一标识,调用人脸检测接口时,会为每个人脸图片赋予一个唯一的FACE_TOKEN,同一张图片多次检测得到的FACE_TOKEN是同一个。
*/
private String image_type;
/**
* 非必填
* 包括age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type信息
* 逗号分隔. 默认只返回face_token、人脸框、概率和旋转角度
*/
private String face_field = "age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type";
/**
* 非必填
* 最多处理人脸的数目,默认值为1,仅检测图片中面积最大的那个人脸;最大值10,检测图片中面积最大的几张人脸。
*/
private int max_face_num = 1;
/**
* 非必填
* 人脸的类型
* LIVE表示生活照:通常为手机、相机拍摄的人像图片、或从网络获取的人像图片等
* IDCARD表示身份证芯片照:二代身份证内置芯片中的人像照片
* WATERMARK表示带水印证件照:一般为带水印的小图,如公安网小图
* CERT表示证件照片:如拍摄的身份证、工卡、护照、学生证等证件图片
* 默认LIVE
*/
private String face_type = "LIVE";
/**
* 非必填
* 活体控制 检测结果中不符合要求的人脸会被过滤
* NONE: 不进行控制
* LOW:较低的活体要求(高通过率 低攻击拒绝率)
* NORMAL: 一般的活体要求(平衡的攻击拒绝率, 通过率)
* HIGH: 较高的活体要求(高攻击拒绝率 低通过率)
* 默认NONE
*/
private String liveness_control = "LOW";
- 脸部数据返回实体类
(因查找所有字段所以类定义出来后发现有6000多行,相当汗颜,所以就不贴实体类代码了FaceEntity)
返回的Json解析就好很简单
返回值参数说明参考官方文档
{"error_code":0,"error_msg":"SUCCESS","log_id":747956973934496741,"timestamp":1557393449,"cached":0,"result":{"face_num":1,"face_list":[{"face_token":"40ea41dfa3c960c28e9a43d40d9b581e","location":{"left":263.09,"top":802.61,"width":554,"height":600,"rotation":-3},"face_probability":1,"angle":{"yaw":2.77,"pitch":2.29,"roll":-5.66},"liveness":{"livemapscore":0.53},"age":28,"beauty":56.71,"expression":{"type":"none","probability":1},"face_shape":{"type":"square","probability":0.38},"gender":{"type":"male","probability":1},"glasses":{"type":"none","probability":1},"landmark":[{"x":406.61,"y":905.51},{"x":682.46,"y":890.12},{"x":551.1,"y":1023.27},{"x":559.38,"y":1205.84}],"landmark72":[{"x":271.37,"y":938.66},{"x":280.66,"y":1029.54},{"x":296.73,"y":1120},{"x":318.86,"y":1209.2},{"x":375.84,"y":1301.11},{"x":469.74,"y":1369.87},{"x":565.61,"y":1385.65},{"x":659.8,"y":1358.1},{"x":746.34,"y":1282.74},{"x":799.34,"y":1186.68},{"x":818.35,"y":1091.88},{"x":825.7,"y":996.84},{"x":824.31,"y":903.86},{"x":347.37,"y":918.04},{"x":373.76,"y":897.75},{"x":403.5,"y":890.07},{"x":433.13,"y":893.74},{"x":460.88,"y":913.74},{"x":434.09,"y":921.48},{"x":404.11,"y":926.57},{"x":373.61,"y":924.79},{"x":406.61,"y":905.51},{"x":302.89,"y":854.38},{"x":333.87,"y":809.08},{"x":380.22,"y":795.11},{"x":426.7,"y":798.48},{"x":469.64,"y":825.46},{"x":426.4,"y":829.1},{"x":382.21,"y":830.98},{"x":340.27,"y":840.14},{"x":630.01,"y":904.71},{"x":655.98,"y":880.91},{"x":685.19,"y":873.25},{"x":715.87,"y":877.17},{"x":744.62,"y":893.82},{"x":719.58,"y":905.06},{"x":688.65,"y":910.03},{"x":657.94,"y":908.66},{"x":682.46,"y":890.12},{"x":611.72,"y":817.29},{"x":652.52,"y":784.3},{"x":699.29,"y":776.42},{"x":747.24,"y":784.53},{"x":782.65,"y":826.45},{"x":743.57,"y":816.77},{"x":700.59,"y":813.37},{"x":656.33,"y":815.89},{"x":503.49,"y":911.87},{"x":493.37,"y":959.85},{"x":484.25,"y":1009.03},{"x":464.36,"y":1075.7},{"x":508.26,"y":1068.58},{"x":597.82,"y":1063.74},{"x":640.91,"y":1065.55},{"x":615.22,"y":1001.85},{"x":600.93,"y":954.81},{"x":586.48,"y":907.58},{"x":551.1,"y":1023.27},{"x":453.95,"y":1211.77},{"x":497.89,"y":1171.11},{"x":558.44,"y":1161.78},{"x":619.29,"y":1165.32},{"x":665.14,"y":1200.98},{"x":622.47,"y":1240.7},{"x":560.97,"y":1257.96},{"x":497.42,"y":1247.63},{"x":503.75,"y":1195.6},{"x":559.87,"y":1190.72},{"x":615.71,"y":1190.11},{"x":613.67,"y":1211.71},{"x":558.75,"y":1218.49},{"x":503.99,"y":1217.94}],"landmark150":{"cheek_right_1":{"x":291.34,"y":966.28},"cheek_right_3":{"x":297.66,"y":1052.59},"cheek_right_5":{"x":311.43,"y":1138.9},"cheek_right_7":{"x":333.91,"y":1225.52},"cheek_right_9":{"x":392.47,"y":1310.17},"cheek_right_11":{"x":479.28,"y":1365.48},"chin_2":{"x":569.05,"y":1379.77},"cheek_left_11":{"x":656.54,"y":1362.88},"cheek_left_9":{"x":739.1,"y":1299.59},"cheek_left_7":{"x":790.44,"y":1208.74},"cheek_left_5":{"x":807.05,"y":1117.68},"cheek_left_3":{"x":814.19,"y":1028.75},"cheek_left_1":{"x":814.42,"y":939.96},"eye_right_corner_right":{"x":357.87,"y":918.41},"eye_right_eyelid_upper_2":{"x":380.89,"y":897.92},"eye_right_eyelid_upper_4":{"x":409.14,"y":891.59},"eye_right_eyelid_upper_6":{"x":437.58,"y":897.04},"eye_right_corner_left":{"x":464.34,"y":920.29},"eye_right_eyelid_lower_6":{"x":438.14,"y":926.05},"eye_right_eyelid_lower_4":{"x":409.54,"y":929.89},"eye_right_eyelid_lower_2":{"x":380.78,"y":927.04},"eye_right_eyeball_center":{"x":410.45,"y":908.96},"eyebrow_right_corner_right":{"x":315.58,"y":848.58},"eyebrow_right_upper_2":{"x":345.39,"y":809.84},"eyebrow_right_upper_3":{"x":387.1,"y":799.98},"eyebrow_right_upper_4":{"x":428.11,"y":803.49},"eyebrow_right_corner_left":{"x":467.65,"y":829.22},"eyebrow_right_lower_3":{"x":427.46,"y":833.38},"eyebrow_right_lower_2":{"x":387.23,"y":833.29},"eyebrow_right_lower_1":{"x":348.98,"y":838.49},"eye_left_corner_right":{"x":624.37,"y":909.62},"eye_left_eyelid_upper_2":{"x":648.19,"y":885.25},"eye_left_eyelid_upper_4":{"x":676.26,"y":877.52},"eye_left_eyelid_upper_6":{"x":706.31,"y":881.22},"eye_left_corner_left":{"x":734.88,"y":897.68},"eye_left_eyelid_lower_6":{"x":710.24,"y":907.54},"eye_left_eyelid_lower_4":{"x":679.88,"y":912.97},"eye_left_eyelid_lower_2":{"x":650.65,"y":912.07},"eye_left_eyeball_center":{"x":676.61,"y":893.55},"eyebrow_left_corner_right":{"x":613.75,"y":824},"eyebrow_left_upper_2":{"x":652.11,"y":793.64},"eyebrow_left_upper_3":{"x":694.31,"y":785.83},"eyebrow_left_upper_4":{"x":738.71,"y":790.23},"eyebrow_left_corner_left":{"x":775.21,"y":823.63},"eyebrow_left_lower_3":{"x":737.25,"y":819.25},"eyebrow_left_lower_2":{"x":696.61,"y":819.09},"eyebrow_left_lower_1":{"x":655.3,"y":823.13},"nose_right_contour_1":{"x":502.5,"y":915.54},"nose_right_contour_2":{"x":491.26,"y":960.04},"nose_right_contour_3":{"x":480.28,"y":1004.8},"nose_right_contour_4":{"x":458.63,"y":1072.42},"nose_right_contour_6":{"x":503.03,"y":1061.2},"nose_left_contour_6":{"x":596.17,"y":1056.26},"nose_left_contour_4":{"x":644.22,"y":1064.88},"nose_left_contour_3":{"x":613.98,"y":999.11},"nose_left_contour_2":{"x":597.95,"y":954.78},"nose_left_contour_1":{"x":582.09,"y":911.42},"nose_tip":{"x":544.82,"y":1009.35},"mouth_corner_right_outer":{"x":448.88,"y":1214.75},"mouth_lip_upper_outer_3":{"x":490.52,"y":1169.64},"mouth_lip_upper_outer_6":{"x":553.95,"y":1159.76},"mouth_lip_upper_outer_9":{"x":620.82,"y":1165},"mouth_corner_left_outer":{"x":671.49,"y":1204.98},"mouth_lip_lower_outer_9":{"x":627.46,"y":1245.04},"mouth_lip_lower_outer_6":{"x":559.31,"y":1259.83},"mouth_lip_lower_outer_3":{"x":492.75,"y":1250.7},"mouth_lip_upper_inner_3":{"x":498.28,"y":1203.18},"mouth_lip_upper_inner_6":{"x":556.25,"y":1200.17},"mouth_lip_upper_inner_9":{"x":617.13,"y":1198.72},"mouth_lip_lower_inner_9":{"x":615.66,"y":1206.91},"mouth_lip_lower_inner_6":{"x":556.31,"y":1211.29},"mouth_lip_lower_inner_3":{"x":499,"y":1212.25},"cheek_right_2":{"x":293.56,"y":1008.82},"cheek_right_4":{"x":303.6,"y":1095.59},"cheek_right_6":{"x":320.31,"y":1182.91},"cheek_right_8":{"x":359,"y":1271.21},"cheek_right_10":{"x":432.44,"y":1342.76},"chin_1":{"x":521.96,"y":1378.01},"chin_3":{"x":615.05,"y":1376.71},"cheek_left_10":{"x":701.57,"y":1336.37},"cheek_left_8":{"x":769.14,"y":1257.08},"cheek_left_6":{"x":801.83,"y":1164.16},"cheek_left_4":{"x":811.15,"y":1073.3},"cheek_left_2":{"x":815.33,"y":984.4},"eyebrow_right_upper_1":{"x":316.41,"y":838.58},"eyebrow_right_upper_5":{"x":467.24,"y":813.04},"eyebrow_left_upper_1":{"x":612.69,"y":807.9},"eyebrow_left_upper_5":{"x":773.25,"y":814.17},"eye_right_eyelid_upper_1":{"x":367.73,"y":906.8},"eye_right_eyelid_upper_3":{"x":394.17,"y":892.97},"eye_right_eyelid_upper_5":{"x":423.9,"y":892.21},"eye_right_eyelid_upper_7":{"x":452.57,"y":906.23},"eye_right_eyelid_lower_7":{"x":451.24,"y":923.7},"eye_right_eyelid_lower_5":{"x":424.11,"y":929.76},"eye_right_eyelid_lower_3":{"x":394.73,"y":930.5},"eye_right_eyelid_lower_1":{"x":369.13,"y":923.67},"eye_right_eyeball_right":{"x":386.77,"y":912.12},"eye_right_eyeball_left":{"x":435.12,"y":910.29},"eye_left_eyelid_upper_1":{"x":634,"y":895.34},"eye_left_eyelid_upper_3":{"x":660.84,"y":879.11},"eye_left_eyelid_upper_5":{"x":691.4,"y":876.99},"eye_left_eyelid_upper_7":{"x":721.59,"y":887.31},"eye_left_eyelid_lower_7":{"x":722.25,"y":903.24},"eye_left_eyelid_lower_5":{"x":695.31,"y":912.32},"eye_left_eyelid_lower_3":{"x":664.7,"y":914.41},"eye_left_eyelid_lower_1":{"x":637.12,"y":911.47},"eye_left_eyeball_right":{"x":652.86,"y":897.42},"eye_left_eyeball_left":{"x":701.22,"y":894.15},"nose_bridge_1":{"x":541.91,"y":912.71},"nose_bridge_2":{"x":543.4,"y":956.33},"nose_bridge_3":{"x":544.97,"y":999.56},"nose_right_contour_5":{"x":485.83,"y":1092.44},"nose_right_contour_7":{"x":496.71,"y":1041.47},"nose_left_contour_7":{"x":599.71,"y":1035.86},"nose_left_contour_5":{"x":616.08,"y":1087.5},"nose_middle_contour":{"x":549.63,"y":1083.28},"mouth_corner_right_inner":{"x":455.12,"y":1213.16},"mouth_corner_left_inner":{"x":664.41,"y":1203.75},"mouth_lip_upper_outer_1":{"x":459.1,"y":1197.78},"mouth_lip_upper_outer_2":{"x":472.71,"y":1182.04},"mouth_lip_upper_outer_4":{"x":508.83,"y":1158.37},"mouth_lip_upper_outer_5":{"x":531.01,"y":1154.67},"mouth_lip_upper_outer_7":{"x":576.99,"y":1153.23},"mouth_lip_upper_outer_8":{"x":600.47,"y":1155.09},"mouth_lip_upper_outer_10":{"x":640.75,"y":1175.04},"mouth_lip_upper_outer_11":{"x":657.57,"y":1189.16},"mouth_lip_lower_outer_11":{"x":659.21,"y":1220.72},"mouth_lip_lower_outer_10":{"x":644.88,"y":1235.44},"mouth_lip_lower_outer_8":{"x":606.89,"y":1255.68},"mouth_lip_lower_outer_7":{"x":583.77,"y":1260.4},"mouth_lip_lower_outer_5":{"x":535.67,"y":1262.95},"mouth_lip_lower_outer_4":{"x":512.86,"y":1259.63},"mouth_lip_lower_outer_2":{"x":475.48,"y":1242.38},"mouth_lip_lower_outer_1":{"x":461.29,"y":1229.28},"mouth_lip_upper_inner_1":{"x":465.29,"y":1209.12},"mouth_lip_upper_inner_2":{"x":480.92,"y":1205.79},"mouth_lip_upper_inner_4":{"x":517.46,"y":1200.45},"mouth_lip_upper_inner_5":{"x":536.49,"y":1199.47},"mouth_lip_upper_inner_7":{"x":577.14,"y":1198.16},"mouth_lip_upper_inner_8":{"x":597.12,"y":1197.58},"mouth_lip_upper_inner_10":{"x":635.77,"y":1199.58},"mouth_lip_upper_inner_11":{"x":653.3,"y":1201.12},"mouth_lip_lower_inner_11":{"x":652.65,"y":1206.76},"mouth_lip_lower_inner_10":{"x":634.83,"y":1206.95},"mouth_lip_lower_inner_8":{"x":595.94,"y":1208.42},"mouth_lip_lower_inner_7":{"x":576.95,"y":1209.67},"mouth_lip_lower_inner_5":{"x":536.28,"y":1211.84},"mouth_lip_lower_inner_4":{"x":517.86,"y":1212.16},"mouth_lip_lower_inner_2":{"x":481.6,"y":1213.9},"mouth_lip_lower_inner_1":{"x":465.98,"y":1215.13}},"race":{"type":"yellow","probability":1},"quality":{"occlusion":{"left_eye":0,"right_eye":0,"nose":0,"mouth":0,"left_cheek":0.06,"right_cheek":0.01,"chin_contour":0},"blur":0,"illumination":148,"completeness":1},"eye_status":{"left_eye":1,"right_eye":1},"emotion":{"type":"neutral","probability":0.42},"face_type":{"type":"human","probability":0.87}}]}}
API接口定义
public interface BaiDuApi {
@GET("https://aip.baidubce.com/oauth/2.0/token")
Observable<BaiDuToken> getToken(@QueryMap Map<String, String> data);
@Headers({"Content-Type: application/json;charset=UTF-8"})
@POST
Observable<FaceEntity> getDetect(@Url String access_token, @Body BodyDetect bodyDetect);
// Call<ResponseBody> getDetect(@Url String access_token, @Body BodyDetect bodyDetect);
}
在初始化配置Retrofit (BaiDuHttpUtil)
public class BaiDuHttpUtil {
private static final String TAG = "BaiDuHttpUtil";
private BaiDuApi api;
private static BaiDuHttpUtil util;
private BaiDuHttpUtil() {
init();
}
public BaiDuApi getApi() {
return api;
}
public static BaiDuHttpUtil getInstance() {
synchronized (BaiDuHttpUtil.class) {
if (util == null) {
util = new BaiDuHttpUtil();
}
}
return util;
}
private void init() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(Config.OUT_TIME, TimeUnit.SECONDS);//连接 超时时间
builder.writeTimeout(Config.OUT_TIME, TimeUnit.SECONDS);//写操作 超时时间
builder.readTimeout(Config.OUT_TIME, TimeUnit.SECONDS);//读操作 超时时间
builder.retryOnConnectionFailure(true);//错误重连
Interceptor tor = chain -> {//拦截器
Request request = chain.request();
Log.e(TAG, request.url().toString());
return chain.proceed(request);
};
builder.addInterceptor(tor);
Retrofit retrofit = new Retrofit.Builder()
.client(builder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(Config.BAIDU_BASE_URL)//这个配置自己服务器,没有添加(https://aip.baidubce.com/rest/2.0/)也可以
.build();
api = retrofit.create(BaiDuApi.class);
}
/**
* 获取百度token
*
* @param client_id API Key
* @param client_secret Secret Key
* @return
*/
public Observable<BaiDuToken> getToken(String client_id, String client_secret) {
HashMap<String, String> data = new HashMap<>();
data.put("grant_type", "client_credentials");
data.put("client_id", client_id);
data.put("client_secret", client_secret);
return api.getToken(data);
}
private static final String BAI_DU_FACE_URL = "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=";
/**
* 脸部检测
* @param bodyDetect
* @return
*/
public Observable<FaceEntity> getFace(BodyDetect bodyDetect) {
String token = App.mSharedPreferences.getString("Token", "");
if (token.equals("")) {
FaceEntity face = new FaceEntity();
face.setError_code(-1);
face.setError_msg("Token不存在");
return Observable.just(face);
}
String url = BAI_DU_FACE_URL + token;//检测脸部url
return api.getDetect(url, bodyDetect);
}
}
定义好这些然后就是简单的调用了
-
获取Token的调用
是不是觉得调用代码很简洁
BaiDuHttpUtil.getInstance().getToken("你自己的API Key", "你自己的Secret Key")
.subscribeOn(Schedulers.io())
.doOnSubscribe(disposable -> {
//这里可以执行加载动画
})
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.filter(token -> {//这里判断下Token是否为空
if (token.getAccess_token() != null && !token.getAccess_token().equals("")) {
return true;
}
return false;
})
.subscribe(token -> {//将Token存储在本地
SharedPreferences.Editor edit = App.mSharedPreferences.edit();
edit.putString("Token", token.getAccess_token());
edit.apply();
Log.e(TAG, token.toString());
}, Throwable::printStackTrace, () -> {
//这里执行停止加载动画
});
-
获取人脸检测数据的调用
String picPic = "/storage/emulated/0/DCIM/Camera/IMG_20190509_171526.jpg";
Observable.just(picPic)
.subscribeOn(Schedulers.io())
.doOnSubscribe(disposable -> {
//启动加载动画
})
.subscribeOn(AndroidSchedulers.mainThread())
.flatMap(SDKModel::createBody)//创建请求参数
.flatMap(bodyDetect -> BaiDuHttpUtil.getInstance().getFace(bodyDetect))//请求获取face数据
.filter(faceEntity -> {//判断下数据是否正确
if (faceEntity.getError_code() != 0) {
Log.e(TAG, faceEntity.getError_code() + faceEntity.getError_msg());
return false;
}
return true;
})
.observeOn(Schedulers.newThread())
.flatMap(faceEntity -> SDKModel.drawPoint(picPic, faceEntity))//获取的点画在原图上
.observeOn(AndroidSchedulers.mainThread())
.subscribe(faceBitmap -> imageView.setImageBitmap(faceBitmap),//显示图片
Throwable::printStackTrace, () -> {
//关闭加载动画
});
- SDKModel.class
public class SDKModel {
private static final String TAG = "SDKModel";
public static Observable<BodyDetect> createBody(String picPath) {
return Observable.defer(() -> {
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//读取图片到ByteArrayOutputStream
bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos); //参数如果为100那么就不压缩
byte[] bytes = baos.toByteArray();
String strbm = Base64.encodeToString(bytes, Base64.DEFAULT);
bitmap.recycle();
BodyDetect bodyDetect = new BodyDetect();
bodyDetect.setImage(strbm);
bodyDetect.setImage_type("BASE64");
return Observable.just(bodyDetect);
});
}
public static Observable<Bitmap> drawPoint(String picPath, FaceEntity entity) {
return Observable.defer(() -> {
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
FaceEntity.ResultBean.FaceListBean.LocationBean locat = entity.getResult().getFace_list().get(0).getLocation();
Log.e(TAG, locat.toString());
Log.e(TAG, "left=" + (int) locat.getLeft() + " top=" + (int) locat.getTop());
Rect rect = new Rect((int) Math.ceil(locat.getLeft()),
(int) Math.ceil(locat.getTop()),
(int) (locat.getWidth() + locat.getLeft()),
(int) (locat.getHeight() + locat.getTop()));
canvas.save();
canvas.rotate(locat.getRotation(),
locat.getLeft() + (locat.getWidth() / 2),
locat.getTop() + (locat.getHeight() / 2));
canvas.drawRect(rect, paint);
canvas.restore();
for (FaceEntity.ResultBean.FaceListBean.Landmark72Bean bean : entity.getResult().getFace_list().get(0).getLandmark72()) {
canvas.drawPoint(bean.getX(), bean.getY(), paint);
}
bitmap.recycle();
return Observable.just(mutableBitmap);
});
}
public static Observable<String> callToString(Call<ResponseBody> call) {
return Observable.defer(() -> {
return Observable.just(call.execute().body().string());
});
}
}
网友评论