Android自定义人脸识别框,Android实现人脸识别的详细过程

该博客主要介绍了如何在Android平台上进行人脸识别。通过FaceDetector API寻找人脸,对检测到的人脸进行坐标处理,并根据人脸位置变化决定是否绘制矩形框。同时,涉及到图像的旋转和缩放操作,以适应不同设备的屏幕尺寸。代码中还包含了从YuvImage到JPEG的转换过程,以及在全屏和正常模式下处理图像的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import android.media.FaceDetector;

import android.media.FaceDetector.Face;

//Harrison add

private void DrawRectOnFace() {

if (numberOfFaceDetected != 0) {

Face mFace1 = mFace[0];

PointF midPoint = new PointF();

mFace1.getMidPoint(midPoint);

if ((Math.abs(mPreMidPoint.x-midPoint.x) < 50) && (Math.abs(mPreMidPoint.y-midPoint.y) < 50)) {

Log.i("Harrison", "not draw Rect .");

return ;

}

mPreMidPoint.x = midPoint.x;

mPreMidPoint.y = midPoint.y;

mFindFaceView.setVisibility(View.VISIBLE);

} else {

mPreMidPoint.x = 0;

mPreMidPoint.y = 0;

mFindFaceView.clearDraw();

mFindFaceView.setVisibility(View.GONE);

return;

}

mFindFaceView.drawRects(mFace, numberOfFaceDetected);

}

//调用API找人脸,需要import进软件包哦!

private void FindFacesInBitmap(Bitmap myBitmap) {

imageWidth = myBitmap.getWidth();

imageHeight = myBitmap.getHeight();

Log.i("Harrison", "imageWidth="+imageWidth+",  imageHeight="+imageHeight);

mFace = new FaceDetector.Face[numberOfFace];

mFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);

numberOfFaceDetected = mFaceDetect.findFaces(myBitmap, mFace);

Log.i("Harrison", "numberOfFaceDetected="+numberOfFaceDetected);

}

private Bitmap rotateMyBitmap(Bitmap bitmap) {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

matrix.postRotate(90);

Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

return rotateBitmap;

}

private Bitmap scaleMyBitmap(Bitmap bitmap) {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

int nWidth = mFindFaceView.getFaceViewWidth();;

int nHeight = mFindFaceView.getFaceViewHeight();

// Log.i("Harrison", "nWidth="+nWidth+",  nHeight"+nHeight);

float scaleWidth = ((float) nWidth)/width;

float scaleHeight = ((float)nHeight)/height;

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

return resizedBitmap;

}

//处理图片格式,一般摄像头抓到的数据为ImageFormat.NV21,不同的格式,需要调整的。

private void decodeToBitMap(byte[] data, android.hardware.Camera _camera) {

mCameraDevice.setPreviewCallback(null);

Size size = mCameraDevice.getParameters().getPreviewSize();

//FileOutputStream outStream = null;

try {

YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);

if (image != null) {

ByteArrayOutputStream stream = new ByteArrayOutputStream();

image.compressToJpeg(new Rect(0, 0, size.width, size.height), 80, stream);

//  outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));

//  outStream.write(stream.toByteArray());

//  outStream.close();

//  Log.i("Harrison", "write file to sdcard.");

//在我的手机上有两种预览模式,发现全屏模式时需要调整图片的大小才能正确定位。

Bitmap myBitmap=BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());

stream.close();

if (mPreviewLayoutProxy.getFullScreenMode()) { // fullscreen mode

Bitmap tmpScaleBmp=null;

Bitmap tmpRotateBmp=null;

//手机是竖屏横排是与其别的哦

if (((mOrientation/90) == 0) || ((mOrientation/90) == 2)) {

tmpRotateBmp = rotateMyBitmap(myBitmap);

tmpScaleBmp = scaleMyBitmap(tmpRotateBmp);

FindFacesInBitmap(tmpScaleBmp);

if (tmpRotateBmp != null) {

tmpRotateBmp.recycle();

tmpRotateBmp = null;

}

} else {

FindFacesInBitmap(scaleMyBitmap(myBitmap));

}

if (tmpScaleBmp != null) {

tmpScaleBmp.recycle();

tmpScaleBmp = null;

}

} else { //normal mode

FindFacesInBitmap(myBitmap);

}

DrawRectOnFace();

if (myBitmap != null) {

myBitmap.recycle();

myBitmap = null;

}

}

} catch (Exception ex) {

Log.e("Sys", "Error:" + ex.getMessage());

}

mCameraDevice.setPreviewCallback(mPreviewCallback);

}

private  final class PostPreviewCallback implements PreviewCallback {

@Override

public void onPreviewFrame(byte[] data, android.hardware.Camera camera) {

decodeToBitMap(data, camera);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值