写了一个简单的调用百度人脸识别在线的JAVA接口,识别率挺高的,但是目前写得还不是很全面,然后打开相册选取图片那一部分,以及工具类代码,是有参考别人写的代码,具体是摘自哪里之前没有保存。
这里贴一些主要的代码。
人脸对比部分:
package ss.demo14;
import android.Manifest;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.chainsaw.Main;
import java.io.File;
import java.text.DecimalFormat;
import ss.demo14.mets.CFace;
/*
*@Author:Swallow
*@Date:2018/12/12
*/
public class FaceActivity extends AppCompatActivity implements View.OnClickListener {
public static final int SELECT_PHOTO1 = 2;
public static final int SELECT_PHOTO2 = 3;
public static final int TAKE_PHOTO = 4;
private ImageView imageViewShow1;
private ImageView imageViewShow2;
public TextView tv_cmp;
String path1, path2,fsres;
Button btn1, btn2, btn_make;
//private Bitmap orc_bitmap;
//新建Handler的对象,在这里接收Message,然后更新TextView控件的内容
//这一部分暂时没时间完成
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_face);
btn1 = (Button) findViewById(R.id.photo1);
btn2 = (Button) findViewById(R.id.photo2);
btn_make = (Button) findViewById(R.id.make);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn_make.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.photo1:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}else{
openAlbum1();
}
break;
case R.id.photo2:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}else {
//opencare();
openAlbum2();
}
break;
case R.id.make:
if(path1!=null&&path2!=null){
sendRequestWithHttpClient();
tv_cmp = (TextView)findViewById(R.id.cmp);
tv_cmp.setText(fsres);
}else{
Toast.makeText(this, "图片获取失败", Toast.LENGTH_LONG).show();
}
break;
}
}
//开启子线程
private void sendRequestWithHttpClient() {
new Thread(new Runnable() {
@Override
public void run() {
CFace cf = new CFace();
String ss = cf.cface(path1, path2);
//获取JSON字符串中的数据
String result = JSONObject.parseObject(ss).getString("result");
float score = JSONObject.parseObject(result).getFloatValue("score");
Log.v("score", "score"+score);
//Double so = score;
DecimalFormat df = new DecimalFormat("0.00");
Double cny = Double.parseDouble(df.format(score));
String fsres = face(cny);
}
}).start();
}
public String face(double d){
//ss=null;
if(d>=75.00){
fsres = "这两个人是同一个人";
}
else {
fsres = "这两个人不是同一个人";
}
Log.v("ss",fsres);
return fsres;
}
//按钮一事件
public void openAlbum1() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO1);
}
//按钮二事件
public void openAlbum2() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//打开相册后返回
case SELECT_PHOTO1:
if (resultCode == RESULT_OK) {
//判断手机系统版本号
if (Build.VERSION.SDK_INT > 19) {
path1 = handleImgeOnKitKat(data);
Bitmap bmap1 = displayImage(path1);//获取图片Bitmap显示到对应的控件上
if (bmap1 != null) {
imageViewShow1 = (ImageView) findViewById(R.id.iv_show1);
imageViewShow1.setImageBitmap(bmap1);
}
} else {