android 人脸识别
来源:互联网 发布:java action models 编辑:程序博客网 时间:2024/05/18 22:51
package com.example.d_facedetect;
import android.app.Activity;
public class MainActivity extends Activity {
private MyImageView mImageView;
private Bitmap mFaceBitmap;
private int mFaceWidth, mFaceHeight;
private static final int MAX_FACES = 10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImageView = new MyImageView(this, null);
setContentView(mImageView, new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.aaa);
mFaceBitmap = bitmap.copy(Config.RGB_565, true);
bitmap.recycle();
mFaceWidth = mFaceBitmap.getWidth();
mFaceHeight = mFaceBitmap.getHeight();
mImageView.setImageBitmap(mFaceBitmap);
setFace();
mImageView.invalidate();
}
public void setFace() {
FaceDetector fd;
FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACES];
PointF midpoint = new PointF();
int[] fpx = null;
int[] fpy = null;
int count = 0;
try {
fd = new FaceDetector(mFaceWidth, mFaceHeight, MAX_FACES);
count = fd.findFaces(mFaceBitmap, faces);
} catch (Exception e) {
e.printStackTrace();
}
if (count > 0) {
fpx = new int[count];
fpy = new int[count];
for (int i = 0; i < count; i++) {
try {
faces[i].getMidPoint(midpoint);
fpx[i] = (int) midpoint.x;
fpy[i] = (int) midpoint.y;
} catch (Exception e) {
}
}
}
mImageView.setDisplayPoints(fpx, fpy, count, 0);
mImageView.drawRect(faces);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyImageView extends ImageView{
private int mDisplayStyle = 0;
private int[] mPX;
private int[] mPY;
private Face[] faces;
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void drawRect(Face[] faces) {
this.faces = faces;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(faces == null || faces.length == 0){
return;
}
Paint p = new Paint();
p.setColor(R.color.red);
p.setStyle(Style.STROKE);
p.setStrokeWidth(3);
for (int i = 0; i < faces.length; i++) {
Face face = faces[i];
if(face == null){
continue;
}
PointF eyeCenter = new PointF();
face.getMidPoint(eyeCenter);
float eyeDis = face.eyesDistance();
float w = eyeDis * 2;
float h = eyeDis * 3;
canvas.drawRect(eyeCenter.x - 5, eyeCenter.y + 5, eyeCenter.x + 5, eyeCenter.y - 5, p);
canvas.drawRect(eyeCenter.x - w/2, eyeCenter.y + h / 2, eyeCenter.x + w/2, eyeCenter.y - h / 2, p);
}
}
public void setDisplayPoints(int [] xx, int [] yy, int total, int style) {
mDisplayStyle = style;
mPX = null;
mPY = null;
if (xx != null && yy != null && total > 0) {
mPX = new int[total];
mPY = new int[total];
for (int i = 0; i < total; i++) {
mPX[i] = xx[i];
mPY[i] = yy[i];
}
}
}
}
}
import android.app.Activity;
public class MainActivity extends Activity {
private MyImageView mImageView;
private Bitmap mFaceBitmap;
private int mFaceWidth, mFaceHeight;
private static final int MAX_FACES = 10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImageView = new MyImageView(this, null);
setContentView(mImageView, new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.aaa);
mFaceBitmap = bitmap.copy(Config.RGB_565, true);
bitmap.recycle();
mFaceWidth = mFaceBitmap.getWidth();
mFaceHeight = mFaceBitmap.getHeight();
mImageView.setImageBitmap(mFaceBitmap);
setFace();
mImageView.invalidate();
}
public void setFace() {
FaceDetector fd;
FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACES];
PointF midpoint = new PointF();
int[] fpx = null;
int[] fpy = null;
int count = 0;
try {
fd = new FaceDetector(mFaceWidth, mFaceHeight, MAX_FACES);
count = fd.findFaces(mFaceBitmap, faces);
} catch (Exception e) {
e.printStackTrace();
}
if (count > 0) {
fpx = new int[count];
fpy = new int[count];
for (int i = 0; i < count; i++) {
try {
faces[i].getMidPoint(midpoint);
fpx[i] = (int) midpoint.x;
fpy[i] = (int) midpoint.y;
} catch (Exception e) {
}
}
}
mImageView.setDisplayPoints(fpx, fpy, count, 0);
mImageView.drawRect(faces);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyImageView extends ImageView{
private int mDisplayStyle = 0;
private int[] mPX;
private int[] mPY;
private Face[] faces;
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void drawRect(Face[] faces) {
this.faces = faces;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(faces == null || faces.length == 0){
return;
}
Paint p = new Paint();
p.setColor(R.color.red);
p.setStyle(Style.STROKE);
p.setStrokeWidth(3);
for (int i = 0; i < faces.length; i++) {
Face face = faces[i];
if(face == null){
continue;
}
PointF eyeCenter = new PointF();
face.getMidPoint(eyeCenter);
float eyeDis = face.eyesDistance();
float w = eyeDis * 2;
float h = eyeDis * 3;
canvas.drawRect(eyeCenter.x - 5, eyeCenter.y + 5, eyeCenter.x + 5, eyeCenter.y - 5, p);
canvas.drawRect(eyeCenter.x - w/2, eyeCenter.y + h / 2, eyeCenter.x + w/2, eyeCenter.y - h / 2, p);
}
}
public void setDisplayPoints(int [] xx, int [] yy, int total, int style) {
mDisplayStyle = style;
mPX = null;
mPY = null;
if (xx != null && yy != null && total > 0) {
mPX = new int[total];
mPY = new int[total];
for (int i = 0; i < total; i++) {
mPX[i] = xx[i];
mPY[i] = yy[i];
}
}
}
}
}
- android 人脸识别
- android 人脸识别
- android 人脸识别
- Android 人脸识别
- Android 人脸识别
- 1 android 人脸识别
- (opencv)Android人脸识别
- Android人脸识别技术
- Android多媒体-人脸识别
- Android人脸识别技术
- android 人脸检测&&识别
- Android 实现人脸识别
- Android自带的人脸识别
- android人脸识别技术浅析
- android人脸识别类FaceDetector
- 基于android的人脸识别
- Android中的图片人脸识别技术
- android camera2人脸识别流程分析
- SVN化的WEB目录更新方案以及管理服务器群的PSSH并行工具的使用
- Java泛型的好处
- .Net执行cmd命令
- linux常用文件处理命令——课堂笔记
- SQL Server2012 T-SQL对分页的增强尝试
- android 人脸识别
- IE10中的无前缀CSS3渐变
- uva 375 - Inscribed Circles and Isosceles Triangles
- Oracle语句优化
- Android 之Adapter
- fastcgi
- 关于值类型和引用类型
- 20120709-the key factor affecting explicit or implicit interface implementation
- grep总结