之前看到有人在博客写用face++做人脸识别app,后来我也照着教程去试了一遍,发现根本行不通,原因在于他调用的库是旧版本,face++已经全面更新了版本.后来我照着face++官网新版本的API文档打了一遍代码,发现识别的结果还算差强人意,但要识别多个人脸属性,需要重复调用几个函数,太麻烦了,这也是小编今天写这篇文章的初衷,因为小编发现有一个网站提供的第三方库不仅能识别人脸,而且调用方法简便,识别多个人脸属性如性别、年龄、肤色、笑容等只需要几行代码搞定。

使用工具:Android Studio

一、打开百度云网站:http://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/app/list

(1)注册、登录后,点击“创建应用”

(2)填写人脸识别模块信息(应用名称随便写)

填写完毕后,就会得到它的APPID、API Key和secret key,如下

二、将第三方库jar包加入Android studio项目里面去

网盘下载:识别jar包(通用)     提取密码:e13p

(1)将Android Studio由Android模式调成Project模式,可以看到libs文件夹,将jar包放入里面

(2)右击鼠标,选择“Add As Library”,

之后,build.gradle(Module:app)会自动出现以下指令,说明jar包添加成功

三、调用jar包里面的AipOcr函数进行人脸识别

(1)从libs文件夹里面的新添加 jar包可以看到具体函数,其中我们需要调用的函数是AipOcr,调用参数有APPID、apiKey、secretKey、图片的二进制数据流以及需要查询的人脸属性(如性别、年龄、肤色等)

(2)获取图片,该app选用两种途径:从相册中选择和调用手机相机拍摄

从相册中选择:

getImage=(Button)findViewById(R.id.getImage);getImage.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent in=new Intent(Intent.ACTION_PICK);in.setType("image/*");startActivityForResult(in,PHOTO_ALBUM);}});

调用相机拍摄:

获取调用权限

void readRequest(){if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);}if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);}}
Camera.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {File outputImage=new File(Environment.getExternalStorageDirectory()+File.separator+"face.jpg");try{if(outputImage.exists()){outputImage.delete();}outputImage.createNewFile();}catch (IOException e){e.printStackTrace();}imageUri=Uri.fromFile(outputImage);ImagePath=outputImage.getAbsolutePath();//拍摄后图片的存储路径Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);startActivityForResult(intent,CAMERA);}});
 @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);//从相册中选择的图片if(requestCode==PHOTO_ALBUM){if(data!=null){Uri uri=data.getData();Cursor cursor=getContentResolver().query(uri,null,null,null,null);cursor.moveToNext();ImagePath=cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));cursor.close();resizePhoto();myPhoto.setImageBitmap(myBitmapImage);Log.i("图片路径",ImagePath);}}//相机拍摄的图片else if(requestCode==CAMERA){try{resizePhoto();myPhoto.setImageBitmap(myBitmapImage);}catch (Exception e){e.printStackTrace();}}}//调整图片的比例,使其大小小于1M,能够显示在手机屏幕上public void resizePhoto(){BitmapFactory.Options options=new BitmapFactory.Options();options.inJustDecodeBounds=true;//返回图片宽高信息BitmapFactory.decodeFile(ImagePath,options);//让图片小于1024double radio=Math.max(options.outWidth*1.0d/1024f,options.outHeight*1.0d/1024f);options.inSampleSize=(int)Math.ceil(radio);//向上取整倍数options.inJustDecodeBounds=false;//显示图片myBitmapImage=BitmapFactory.decodeFile(ImagePath,options);}

(3)调用函数进行人脸识别

detect.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {res=null;detect_tip.setVisibility(View.VISIBLE);detect_tip.setText("识别中...");if(myBitmapImage==null){myBitmapImage=BitmapFactory.decodeResource(getResources(),R.mipmap.face2);bitmapSmall=Bitmap.createBitmap(myBitmapImage,0,0,myBitmapImage.getWidth(),myBitmapImage.getHeight());}else{//由于有些图片在一些型号手机角度会倾斜,需要进行摆正int degree=getPicRotate(ImagePath);Matrix m=new Matrix();m.setRotate(degree);bitmapSmall=Bitmap.createBitmap(myBitmapImage,0,0,myBitmapImage.getWidth(),myBitmapImage.getHeight(),m,true);}//将图片由路径转为二进制数据流ByteArrayOutputStream stream=new ByteArrayOutputStream();//图片转数据流bitmapSmall.compress(Bitmap.CompressFormat.JPEG,100,stream);final byte[] arrays=stream.toByteArray();//网络申请调用函数进行人脸识别new Thread(new Runnable() {@Overridepublic void run() {HashMap<String,String> options=new HashMap<>();options.put("face_fields","age,gender,race,beauty,expression");//人脸属性:年龄,性别,肤色,颜值,笑容AipFace client=new AipFace("10734368","6cvleSFbyRIRHzhijfYrHZFj","SDnCUfrtH0lgrK01HgTe2ZRLNsmCx5xy");client.setConnectionTimeoutInMillis(2000);client.setSocketTimeoutInMillis(6000);res=client.detect(arrays,options);try{Message message = Message.obtain();message.what = 1;message.obj = res;handler.sendMessage(message);}catch (Exception e){e.printStackTrace();Message message = Message.obtain();message.what = 2;handler.sendMessage(message);}}}).start();}});}

(4)将返回来的网络数据进行处理

private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if(msg.what==1){JSONObject res=(JSONObject) msg.obj;face_resultNum=res.optString("result_num");if(Integer.parseInt(face_resultNum)>=1) {try {JSONArray js = new JSONArray(res.optString("result"));face_age = js.optJSONObject(0).optString("age");face_gender = js.optJSONObject(0).optString("gender");if (face_gender.equals("female")) {face_gender = "女";} else {face_gender = "男";}face_race = js.optJSONObject(0).optString("race");if (face_race.equals("yellow")) {face_race = "黄种人";} else if (face_race.equals("white")) {face_race = "白种人";} else if (face_race.equals("black")) {face_race = "黑种人";}else if(face_race.equals("arabs")){face_race = "阿拉伯人";}int express = Integer.parseInt(js.optJSONObject(0).optString("expression"));if (express == 0) {face_expression = "无";} else if (express == 1) {face_expression = "微笑";} else {face_expression = "大笑";}face_beauty = js.optJSONObject(0).optString("beauty");double  beauty=Math.ceil(Double.parseDouble(face_beauty)+25);if(beauty>=100){//对获得的颜值数据进行一定处理,使得结果更为合理beauty=99.0;}else if(beauty<70){beauty+=10;}else if(beauty>80 && beauty<90){beauty+=5;}else if(beauty>=90 && beauty<95){beauty+=2;}face_beauty=String.valueOf(beauty);} catch (JSONException e) {e.printStackTrace();}detect_tip.setVisibility(View.GONE);AlertDialog.Builder alertDialog = new AlertDialog.Builder(faceRecognition.this);String[] mItems = {"性别:" + face_gender, "年龄:" + face_age, "肤色:" + face_race, "颜值:" + face_beauty, "笑容:" + face_expression};alertDialog.setTitle("人脸识别报告").setItems(mItems, null).create().show();}else{detect_tip.setVisibility(View.VISIBLE);detect_tip.setText("图片不够清晰,请重新选择");}}else{detect_tip.setVisibility(View.VISIBLE);detect_tip.setText("图片不够清晰,请重新选择");}}};
由于有些图片在一些型号手机角度会倾斜(如三星),需要获取图片的角度
public int getPicRotate(String path) {int degree = 0;try {ExifInterface exifInterface = new ExifInterface(path);int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);switch (orientation) {case ExifInterface.ORIENTATION_ROTATE_90:degree = 90;break;case ExifInterface.ORIENTATION_ROTATE_180:degree = 180;break;case ExifInterface.ORIENTATION_ROTATE_270:degree = 270;break;}} catch (IOException e) {e.printStackTrace();}return degree;}

layout布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="20sp"android:background="@color/color2"android:padding="5sp"android:gravity="center"android:text="火眼金睛:一张图看穿你的岁月"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/face2"android:layout_gravity="center"android:layout_weight="1"android:id="@+id/myPhoto"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="20sp"android:layout_gravity="center"android:gravity="center"android:id="@+id/detect_tip"android:text="识别中..."/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp"android:text="选图"android:background="@drawable/button_decoration"android:textColor="@color/white"android:textSize="25sp"android:layout_weight="1"android:id="@+id/getImage"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp"android:text="拍照"android:background="@drawable/button_decoration"android:textColor="@color/white"android:textSize="25sp"android:layout_weight="1"android:id="@+id/Camera"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:layout_weight="1"android:background="@drawable/button_decoration"android:textColor="@color/white"android:textSize="25sp"android:id="@+id/detect"android:text="识别"/></LinearLayout>
</LinearLayout>

四、调试结果

  

完整代码下载:

CSDN下载

Github下载

Android开发实现人脸识别相关推荐

  1. android 动态人脸识别码,Android开发中人脸识别(静态)

    知道没有妹纸,你们是不会看的.先放效果图 最近,项目中需要用到人脸识别,苦于无奈,各种百度,google有关Android开发中人脸识别的内容,最终发现Android官方自带的FaceDetector ...

  2. Android园区部队人脸识别源码门禁项目讲解

    Android园区部队人脸识别源码门禁项目讲解 这边搞人脸识别相关项目有一段时间,今天抽时间讲述一个经典的人脸识别项目:部队人脸识别门禁系统. 大家都知道部队对人员管理安全要求是相当高的,很多保密的技 ...

  3. 基于Android系统的人脸识别签到软件

    项目名称:   基于Android系统的人脸识别签到软件 目  录 1 项目介绍..... 1 1.1 项目背景.... 1 1.2 产品特点.... 2 1.3 可行性分析.... 2 1.3.1 ...

  4. 基于Android端的照片比对系统,基于Android系统的人脸识别系统

    [文章摘要] 当前随着基于Android系统的移动终端设备的广泛应用,以及图像采集设备的普遍集成,使得Android系统的图像采集设备除了具有照相.摄像功能以外,正在扩展新的实用型功能.其中,利用An ...

  5. 人脸识别4:Android InsightFace实现人脸识别Face Recognition(含源码)

    人脸识别4:Android InsightFace实现人脸识别Face Recognition(含源码) 目录 人脸识别4:Android InsightFace实现人脸识别Face Recognit ...

  6. 微信小程序开发工具结合腾讯云开发AI人脸识别和身份证识别——基于腾讯云开发者实验项目

    微信小程序开发工具结合腾讯云开发AI人脸识别和身份证识别--基于腾讯云开发者实验项目 开通腾讯云相关权限(AI人脸识别,文字识别-身份证识别) 查看API密钥 部署微信小程序 成功演示 代码包 开通腾 ...

  7. 强!一个Java开发的人脸识别系统,获取人脸68个关键点(附源码)

    点击上方蓝色字体,选择"标星公众号" 优质文章,第一时间送达 关注公众号后台回复pay或mall获取实战项目资料视频 点击此链接:多套SpringCloud/SpringBoot实 ...

  8. 自动售卖系统开发系列——人脸识别自动售卖机三代BrotherSharp

    大纲: 售卖机三代BrotherSharp的简介 售卖机三代BrotherSharp的方案介绍    #系统整体组成    #软件平台    #硬件平台 售卖机三代BrotherSharp的实现过程 ...

  9. 自动售卖系统开发系列——人脸识别自动售卖机二代ChingTom

    大纲: 售卖机二代ChingTom的简介 售卖机二代ChingTom的方案介绍    #系统整体组成    #软件平台    #硬件平台 售卖机二代ChingTom的实现过程    #业务逻辑介绍   ...

最新文章

  1. SQL Server 2012中的Contained Database尝试
  2. iOS开发-动画总结
  3. ALEIDoc EDI(5)--Inbound Function
  4. 第七章 二叉搜索树(b1)BST:查找
  5. linux+android4.2键值关系,Android4.0 添加一个新的Android 键值
  6. java中array,arrayList,iterator;
  7. Spring Boot Web应用开发 CORS 跨域请求支持
  8. 如何利用业务时间提升自我
  9. PAT乙级(1028 人口普查)
  10. laravel input值必须不等于0_【第十一期】实现 Javascript 版本的 Laravel 风格参数验证器...
  11. Zepto Api参考
  12. 你知道吗?世界上绝美神奇的25条路
  13. linux 硬件raid 坏道,Linux服务器磁盘坏道的修复过程
  14. python简易爬虫:xpath解析方式抓取几页小猪短租官网的住房信息
  15. 计算机40个快捷键,计算机快捷键40个_计算机常用快捷键大全分享
  16. i3s/s3m/3D Tile
  17. 江南大学计算机面试英语,江南大学英语面试!!!
  18. 计算机专业进中国移动难吗,【计算机】中国移动面试技巧和注意事项
  19. SpringBoot入门与常用配置
  20. mysql rbo cbo_Oracle的RBO和CBO详细介绍和优化模式设置方法

热门文章

  1. 力扣146题 LRU 缓存机制
  2. 调试器GDB的基本使用方法
  3. 塔尔斯基关于实数的8条公理(原文)
  4. excel行列值相同,交叉单元格高亮显示
  5. 麦克风阵列杂音很重解决方案(科大讯飞麦克风阵列+6.0)
  6. 如何用快启动pe修复win10系统引导? 神器
  7. 【C语言】字符串函数详解
  8. C语言人物复杂移动与异步输入
  9. 解决 Java 加载 pfx 报密码错误
  10. 如何优雅的在 Word 中插入代码,PlanetB 的完美替代方案