这个代码允许用户从相册中选择图片,调用虚拟机中的图片,然后图片会自动调用人脸检测接口,检测后会返回一个结果,包含表情(笑不笑),还有对年龄的一个分析,以及外贸颜值的打分,还有好多字段,这里就先用着一个。
将选定的图片转换为Base64编码,并发送给百度人脸检测API进行检测,最后显示检测结果。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
buttonSelect = findViewById(R.id.buttonSelect);
buttonDetect = findViewById(R.id.buttonDetect);
client = new AipFace(APP_ID, API_KEY, SECRET_KEY); // 初始化AipFace对象
buttonSelect.setOnClickListener(v -> {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_IMAGE);
});
buttonDetect.setOnClickListener(v -> {
if (bitmap != null) {
try {
HashMap<String, Object> options = new HashMap<>();
options.put("face_field", "age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,face_type");
options.put("max_face_num", "10");
options.put("face_type", "LIVE");
options.put("liveness_control", "LOW");
JSONObject res = client.detect(encodeToBase64(bitmap), "BASE64", options);
Toast.makeText(getAppli
标签:,face,buttonSelect,Intent,put,new,options
From: https://www.cnblogs.com/1947475882-/p/18277684