首页 > 其他分享 >8

8

时间:2024-07-01 11:21:44浏览次数:1  
标签: String beauty age intent result expression

将用户拍摄的照片发送到百度的人脸识别API进行分析,并返回检测结果。
首先,检查 accessToken 是否正确获取。如果 accessToken 为空,说明没得到访问令牌,不发送请求。
创建一个JsonObject,用来存储待发送的JSON数据。使用Gson将JsonObject转换成一个JSON字符串。创建一个RequestBody,将JSON字符串和指定的媒体类型(application/json; charset=utf-8)关联起来。构建一个HTTP POST请求,目标URL为百度人脸检测API地址加上当前的accessToken。调用OkHttp的newCall方法发起请求,然后执行该请求。如果请求成功,继续下一步。获取服务器返回的响应体字符串,并尝试解析它作为一个JSON对象。从该对象提取检测结果,将提取到的检测结果组合成一个字符串,并传递给名为"MoodRecordActivity"的新Activity。该页面用于后续的app开发使用。
private void sendFaceDetectRequest(String imageBase64) {
if (accessToken == null) {
Log.e("MainActivity", "Access token is null, cannot send face detect request.");
return;
}
executorService.execute(() -> {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("image", imageBase64);
jsonObject.addProperty("image_type", "BASE64");
jsonObject.addProperty("face_field", "age,beauty,expression");
String jsonStr = new Gson().toJson(jsonObject);

    MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
    RequestBody body = RequestBody.create(jsonStr, mediaType);

    Request request = new Request.Builder()
            .url(FACE_DETECT_URL + "?access_token=" + this.accessToken)
            .post(body)
            .build();

    try {
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String responseStr = response.body().string();
            handler.post(() -> {
                try {
                    JSONObject jsonResponse = new JSONObject(responseStr);
                    JSONObject result = jsonResponse.optJSONObject("result");
                    if (result != null) {
                        int age = result.getJSONArray("face_list").getJSONObject(0).getInt("age");
                        String beauty = result.getJSONArray("face_list").getJSONObject(0).getString("beauty");
                        String expression = result.getJSONArray("face_list").getJSO

String expression = result.getJSONArray("face_list").getJSONObject(0).getString("expression");

                        String resultText = "Age: " + age + "\nBeauty: " + beauty + "\nExpression: " + expression;

                        Intent intent = new Intent(MainActivity.this, MoodRecordActivity.class);
                        intent.putExtra("age", String.valueOf(age));  // 确保年龄是字符串类型
                        intent.putExtra("beauty", beauty);
                        intent.putExtra("expression", expression);
                        intent.putExtra("image", imageBase64);
                        startActivity(intent);
                    } else {
                        Log.e("MainActivity", "Face detect request failed: result is null");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            });
        } else {
            Log.e("MainActivity", "Face detect request failed");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
});

}

标签:,String,beauty,age,intent,result,expression
From: https://www.cnblogs.com/1947475882-/p/18277689

相关文章