Android人脸识别技术,可以参考下面的网站。

http://www.faceplusplus.com.cn/

本项目使用的就是该网站的api.

项目具体使用的技术代码

/**
* 用来压缩图片的方法
*/
private void resizePhoto() {
BitmapFactory.Options options = new Options();
options.inJustDecodeBounds = true; BitmapFactory.decodeFile(mcurrentPhotoPath, options); double ratio = Math.max(options.outWidth*1.0d/1024 , options.outHeight*1.0d/1024); options.inSampleSize = (int)Math.ceil(ratio); options.inJustDecodeBounds = false; mphotoImage = BitmapFactory.decodeFile(mcurrentPhotoPath, options);
}
//开启图库
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PIC_CODE);
得到图片位置
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch(requestCode) {
//获取图片的路径
case PIC_CODE:
if(null != intent) {
Uri uri = intent.getData();
Cursor cursor = getContentResolver().query( uri, null, null, null, null);
if (cursor==null) {
Log.e("M", "null");
}
cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
mcurrentPhotoPath = cursor.getString(idx);
cursor.close(); resizePhoto(); photoView.setImageBitmap(mphotoImage);
num_detected.setText("");
}
break;

不装逼了,贴代码。

Mainactivity.class

package com.crazylin.facedetected;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; import com.facepp.error.FaceppParseException; public class MainActivity extends Activity implements OnClickListener {
private static final int PIC_CODE = 0X110;
private Button getImage_btn, image_detect_btn, capture_btn;
private View loadingView;
private ImageView photoView;
private TextView num_detected;
private String mcurrentPhotoPath;
private Bitmap mphotoImage;
private Handler mhandler;
private Paint mPaint;
private TextView infoText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
setContentView(R.layout.activity_main); initView();
initEvent(); initHandler();
mPaint = new Paint(); } private void initHandler() {
mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case RESULT_OK:
loadingView.setVisibility(View.GONE);
JSONObject json = (JSONObject) msg.obj;
prepareResultBitmap(json);
photoView.setImageBitmap(mphotoImage);
break;
case RESULT_CANCELED:
loadingView.setVisibility(View.GONE);
String errorMessage = (String) msg.obj; if(TextUtils.isEmpty(errorMessage)) {
num_detected.setText("Error");
}else {
num_detected.setText(errorMessage);
}
break;
}
} };
} private void prepareResultBitmap(JSONObject json) {
Bitmap bitmap = Bitmap.createBitmap(mphotoImage.getWidth(), mphotoImage.getHeight(), mphotoImage.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(mphotoImage, 0, 0, null);
try {
JSONArray faces = json.getJSONArray("face");
int faceCount = faces.length();
num_detected.setText("find : " + faceCount); for(int i=0; i<faceCount; i++) {
//单独的face对象
JSONObject face = faces.getJSONObject(i); int age = face.getJSONObject("attribute").getJSONObject("age").getInt("value");
String gender = face.getJSONObject("attribute").getJSONObject("gender").getString("value");
//String race = face.getJSONObject("attribute").getJSONObject("race").getString("value"); Bitmap infoBitmap = BuildBitmapInfo(age, gender); int infoWidth = infoBitmap.getWidth();
int infoHeight = infoBitmap.getHeight(); if((bitmap.getWidth() < photoView.getWidth())&&(bitmap.getHeight() < photoView.getHeight())) {
float ratio = Math.max(bitmap.getWidth()*1.0f/photoView.getWidth(), bitmap.getHeight()*1.0f/photoView.getHeight());
infoBitmap = Bitmap.createScaledBitmap(infoBitmap, (int)(infoWidth * ratio), (int)(infoHeight * ratio), false);
} JSONObject position = face.getJSONObject("position");
float x = (float) position.getJSONObject("center").getDouble("x");
float y = (float) position.getJSONObject("center").getDouble("y"); float w = (float) position.getDouble("width");
float h = (float) position.getDouble("height"); x = x/100*bitmap.getWidth();
y = y/100*bitmap.getHeight(); w = w/100*bitmap.getWidth();
h = h/100*bitmap.getHeight(); mPaint.setColor(0xffffffff);
canvas.drawLine(x - w/2, y - h/2, x - w/2, y + h/2, mPaint);
canvas.drawLine(x - w/2, y - h/2, x + w/2, y - h/2, mPaint);
canvas.drawLine(x + w/2, y - h/2, x + w/2, y + h/2, mPaint);
canvas.drawLine(x - w/2, y + h/2, x + w/2, y + h/2, mPaint); canvas.drawBitmap(infoBitmap, x - infoBitmap.getWidth()/2, y - h/2 - infoBitmap.getHeight(), null); mphotoImage = bitmap; }
} catch (JSONException e) {
e.printStackTrace();
}
} private Bitmap BuildBitmapInfo(int age, String gender) {
System.out.println(gender + " age:" + age);
infoText.setText(gender + " age:" + age );
infoText.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(infoText.getDrawingCache());
infoText.destroyDrawingCache();
return bitmap;
} private void initEvent() {
getImage_btn.setOnClickListener(this);
image_detect_btn.setOnClickListener(this);
capture_btn.setOnClickListener(this);
} private void initView() {
getImage_btn = (Button) findViewById(R.id.get_image);
image_detect_btn = (Button) findViewById(R.id.detect_image);
capture_btn = (Button) findViewById(R.id.capture_image);
loadingView = findViewById(R.id.loading);
photoView = (ImageView) findViewById(R.id.photo_image);
num_detected = (TextView) findViewById(R.id.num_detected);
infoText = (TextView) findViewById(R.id.info_text); } @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch(requestCode) {
//获取图片的路径
case PIC_CODE:
if(null != intent) {
Uri uri = intent.getData();
Cursor cursor = getContentResolver().query( uri, null, null, null, null);
if (cursor==null) {
Log.e("M", "null");
}
cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
mcurrentPhotoPath = cursor.getString(idx);
cursor.close(); resizePhoto(); photoView.setImageBitmap(mphotoImage);
num_detected.setText("");
}
break;
case 2:
Bitmap bmPhoto = (Bitmap) intent.getExtras().get("data");
mcurrentPhotoPath = "capturing";
mphotoImage = bmPhoto;
photoView.setImageBitmap(mphotoImage);
num_detected.setText("");
break;
}
} /**
* 用来压缩图片的方法
*/
private void resizePhoto() {
BitmapFactory.Options options = new Options();
options.inJustDecodeBounds = true; BitmapFactory.decodeFile(mcurrentPhotoPath, options); double ratio = Math.max(options.outWidth*1.0d/1024 , options.outHeight*1.0d/1024); options.inSampleSize = (int)Math.ceil(ratio); options.inJustDecodeBounds = false; mphotoImage = BitmapFactory.decodeFile(mcurrentPhotoPath, options);
} @Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.get_image:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PIC_CODE);
break; case R.id.capture_image:
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent2, 2);
break;
case R.id.detect_image:
loadingView.setVisibility(View.VISIBLE);
if(mcurrentPhotoPath!=null && !mcurrentPhotoPath.trim().equals("")) {
if(mcurrentPhotoPath.equals("capturing")) { }else {
resizePhoto();
}
}else {
mphotoImage = BitmapFactory.decodeResource(getResources(), R.drawable.brother2);
} FaceDetect.detect(mphotoImage, new FaceDetect.Callback() {
@Override
public void success(JSONObject result) {
Message msg = Message.obtain(mhandler);
msg.what = RESULT_OK;
msg.obj = result;
msg.sendToTarget();
} @Override
public void error(FaceppParseException exception) {
Message msg = Message.obtain(mhandler);
msg.what = RESULT_CANCELED;
msg.obj = exception.getErrorMessage();
msg.sendToTarget();
}
});
break;
} } } </code></pre>

mainactivity的对应布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:id="@+id/get_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="Get Image" /> <Button
android:id="@+id/detect_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/get_image"
android:text="Detect" /> <TextView
android:id="@+id/num_detected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/detect_image"
android:layout_alignTop="@+id/detect_image"
android:gravity="center"
android:text="find: 0" /> <ImageView
android:id="@+id/photo_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/detect_image"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/brother2"/> <FrameLayout
android:id="@+id/loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="gone">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/> <TextView
android:id="@+id/info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:textColor="#AEEEEEff"
android:layout_gravity="center"
android:textSize="18sp"
android:text=" "
/> </FrameLayout> <Button
android:id="@+id/capture_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/num_detected"
android:layout_alignBottom="@+id/num_detected"
android:layout_alignParentLeft="true"
android:text="Capture" /> </RelativeLayout>

常量类

package com.crazylin.facedetected;

public class Constant {
public static final String KEY = "a9f32d6fd94da04bc162caabe7e87400";
public static final String SECRET = "BP9B_33cGnuwPOi4cq2bqNTTTeeLb3cV";
}

请求数据类

package com.crazylin.facedetected;

import java.io.ByteArrayOutputStream;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.util.Log; import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters; public class FaceDetect {
public interface Callback {
void success(JSONObject result); void error(FaceppParseException exeception);
} public static void detect(final Bitmap bm,final Callback callback) {
new Thread(new Runnable() { @Override
public void run() {
try {
//向Face++服务器提交请求
System.out.println("向Face++服务器提交请求");
HttpRequests requests = new HttpRequests(Constant.KEY, Constant.SECRET, true, true);
Bitmap bmsmall = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmsmall.compress(Bitmap.CompressFormat.JPEG, 50, stream); byte[] bytes = stream.toByteArray();
PostParameters params = new PostParameters();
params.setImg(bytes);
JSONObject json = requests.detectionDetect(params);
Log.e("Tag", json.toString()); if (null != callback) {
callback.success(json);
}
} catch (FaceppParseException e) {
System.out.println(e.toString());
e.printStackTrace();
if(null != callback) {
callback.error(e);
}
}
}
}).start();
}
}

ok,需要的尽管拿走吧。

下载地址

http://download.csdn.net/detail/u013270444/9437964

Android 人脸识别的更多相关文章

  1. 集成Android人脸识别demo分享

    本应用来源于虹软人工智能开放平台,人脸识别技术工程如何使用? 1.下载代码 git clone https://github.com/andyxm/ArcFaceDemo.git 2.下载虹软人脸识别 ...

  2. Android人脸识别Demo竖屏YUV方向调整和图片保存

    本博客包含三个常用方法,用于盛开Android版人脸识别Demo中竖屏使用时送入yuv数据,但一直无法识别的情况. 1.首先可以尝试顺时针旋转90°或270°,然后送入识别SDK. 2.旋转方向后依然 ...

  3. Android人脸识别App(带web上传注册信息)

    人脸识别+本机Web后端人脸sdk采用虹软sdk,本机web采用AndServer:上传姓名+人脸图片即可实现注册源码地址:https://github.com/joetang1989/ArcFace ...

  4. 1 android 人脸识别

    1 https://www.google.com.hk/search?newwindow=1&safe=strict&q=android+%E5%9B%BE%E7%89%87%E4%B ...

  5. Android之人脸识别

    **前言** 人工智能时代快速来临,其中人脸识别是当前比较热门的技术,在国内也越来越多的运用,例如刷脸打卡.刷脸App,身份识别,人脸门禁等等.当前的人脸识别技术分为WEBAPI和SDK调用两种法方式 ...

  6. Android人脸检测1(静态图片)

    搭建Android人脸识别环境花了很长时间(可以查看之前的文章),解决Android开发中的杂七杂八小问题也耗时不少. 今天记录一下,点击选择照片或者拍照上传照片进行人脸检测的小demo. (andr ...

  7. Android 使用FACE++架构包实现人脸识别

    今天给大家带来一个通过使用Face++来实现人脸识别的功能. 我们先去这个Face++官网看看:http://www.faceplusplus.com.cn 我们点开案例可以看到众多我们熟知的软件都是 ...

  8. Android多媒体-人脸识别

    1. 相关背景 Google 于2006年8月收购Neven Vision 公司 (该公司拥有 10 多项应用于移动设备领域的图像识别的专利),以此获得了图像识别的技术,并不是常快应用到免费的 Pic ...

  9. 基于虹软的Android的人脸识别SDK使用测试

    现在有很多人脸识别的技术我们可以拿来使用:但是个人认为还是离线端的SDK比较实用:所以个人一直在搜集人脸识别的SDK:原来使用开源的OpenCV:最近有个好友推荐虹软的ArcFace, 闲来无事就下来 ...

随机推荐

  1. 笨办法学Python(二十三)

    习题 23: 读代码 上一周你应该已经牢记了你的符号列表.现在你需要将这些运用起来,再花一周的时间,在网上阅读代码.这个任务初看会觉得很艰巨.我将直接把你丢到深水区呆几天,让你竭尽全力去读懂实实在在的 ...

  2. Html : 规范html代码的网站

    html代码的规范也是很重要的,这里推荐一个网站,很好用,仓鼠是经常用的啦! https://htmlformatter.com/ 以上

  3. 概念:RPG游戏中两个兵种互相攻击的逻辑

    直接上题目: 解析题目: 根据题目的解析,进行代码的实现: 输出结果: 心得: (1) 当我们面对‘公式结果不是我们想要的’时,应该在脑海里将一个完整的攻击流程进行想象,就会对流程有个更清晰的思路 ( ...

  4. JavaRebel 2.0 发布,一个JVM插件

    JavaRebel是一个JVM插件(-javaagent),能够即时重载java class更改,因此不需要重新部署一个应用或者重启容器,节约开发者时间. JavaRebel 2.0的新特征: 改变了 ...

  5. IOS NSURLConnection(大文件下载)

    NSURL:请求地址 NSURLRequest:一个NSURLRequest对象就代表一个请求,它包含的信息有 一个NSURL对象 请求方法.请求头.请求体 请求超时 … … NSMutableURL ...

  6. POJ-1509 Glass Beads---最小表示法模板

    题目链接: https://vjudge.net/problem/POJ-1509 题目大意: 给你一个循环串,然后找到一个位置,使得从这个位置开始的整个串字典序最小. 解题思路: 最小表示法模板 注 ...

  7. Android(java)学习笔记62:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解

    1. 先看看网路上的说法: android.intent.action.MAIN 决定应用程序最先启动的 Activity android.intent.category.LAUNCHER 决定应用程 ...

  8. 类型构造器--高阶类型(构造器):Kind (type theory)--类型的元

    元类型(0阶类型):nullary type, data types 一元类型(一阶类型):unary  adj. [数] 一元的   二元类型: is the kind of a binary ty ...

  9. php中的脚本加速扩展opcache

    今儿在azure里装php5.5.4,发现原先php5.4.php5.3中的zend guard laoder以及php5.2中的Zend Optimizer均不能再用,一直很喜欢用的eacceler ...

  10. 关于win10深度学习安装配置 CUDA9.0+VS2017+Cudnn7.4.1.5+Anaconda3(cupy安装包)+python3.7+pycharm

    0 查看电脑系统版本(非常重要) WIN+R->输入winver, 系统版本号必须高于1703,否则CUDA9.0难以运行!!!! 1 安装 NVIDIA 显卡驱动程序 下载地址:驱动程序 选择 ...