团队app十日冲刺7

基本的功能已经完成,但还存在许多不足的地方,主页面简介且难看。上传图片后返回的结果布局也十分丑陋。考虑到调用图片过于麻烦,还是直接拍照上传比较好。做出一些改进,这里是调用图片进行识别的代码。。
要导入这几个包,有一个是百度智能云的sdk包,可以再平台上找。

 

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;

import java.util.HashMap;
import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {

private static final int PICK_IMAGE = 1;
private ImageView imageView;
private Button buttonSelect, buttonDetect;
private Bitmap bitmap;

private String APP_ID = "67129805";
private String API_KEY = "YnFw5LSDvq76bzSdjWuGWlXL";
private String SECRET_KEY = "lOa0jDMgcQT0baNlzjIR7GMwCFoFEUUy";
private AipFace client;
private static final int YOUR_PERMISSIONS_REQUEST_READ_STORAGE = 1;


@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(getApplicationContext(), res.toString(2), Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}

private String encodeToBase64(Bitmap image) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64Util.encode(byteArray);
}
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />

<Button
android:id="@+id/buttonSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Image"
android:layout_above="@id/imageView"
android:layout_centerHorizontal="true" />

<Button
android:id="@+id/buttonDetect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detect Face"
android:layout_below="@id/imageView"
android:layout_centerHorizontal="true" />

</RelativeLayout>
posted @ 2024-04-27 00:05  涨涨涨张  阅读(4)  评论(0编辑  收藏  举报