只需使用原生 android 的人脸检测库(而不是 OpenCV 或更复杂的东西),一旦应用程序启动,也会跟踪你的脸。您还可以使用 if 语句来检查面部是否被识别以及是否已经播放了语音消息。
if(recognized_faces != 0 && message_already_played == false){
//play the message
}
这是一段代码(一个活动),它从前置摄像头获取所有照片并对其进行分析。最初,代码在照片上用画布绘制,并将它们按顺序(就像是视频流)显示到“miaImmagine2”中。如果您不想要它,请不要显示 miaImmagine2 并仅显示摄像机层。我已经删除了很多你不需要的代码,所以可能有很多代码需要删除或调整。但它可以为您提供很好的帮助。所以这里是代码:
package mawashi.alex.driveawake;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.ErrorCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.media.AudioManager;
import android.media.FaceDetector;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
@SuppressLint("NewApi")
public class AwakeActivity extends Activity {
SurfaceView mVideoCaptureView;
Camera mCamera;
public Bitmap mFaceBitmap;
public TextView Testo;
public Bitmap mFace2Bitmap;
public ImageView miaImmagine2;
private MediaPlayer player;
public SurfaceView mSurfaceView;
public SurfaceHolder mSurfaceHolder;
private static final int MAX_FACES = 1;
int mFaceWidth;
int mFaceHeight;
int cameraType = 1; // front
int frame_sec = 1000000;
byte[] callbackBuffer;
PreviewCallback cb;
public FrameLayout preview;
Bitmap bmp;
Bitmap b;
MediaMetadataRetriever mediaMetadataRetriever=null;
Bitmap bmFrame = null;
FaceDetector.Face[] faces;
Activity activity;
Context mContext;
int index_sleep = 0;
int consec = 0;
boolean sleep = false;
public float scala = 1;
public int offset = 0;
int m = 1;
int Colore = Color.GRAY;
private MediaPlayer player2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.detection);
mVideoCaptureView = (SurfaceView) findViewById(R.id.Surface);
miaImmagine2 = (ImageView) findViewById(R.id.imageView2);
Testo = (TextView) findViewById(R.id.textView1);
SurfaceHolder videoCaptureViewHolder = mVideoCaptureView.getHolder();
videoCaptureViewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
player = MediaPlayer.create(this, R.raw.you_look_good);
videoCaptureViewHolder.addCallback(new Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceCreated(SurfaceHolder holder) {
startVideo();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
});
}
////////////////////////////////////////////////////////////////////////////////////////
//************BUTTONS***************************************************************
//ROTATE THE IMAGE OF 90 DEGREES
protected void setDisplayOrientation(Camera mCamera, int angle){
Method downPolymorphic;
try{
downPolymorphic = mCamera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
if (downPolymorphic != null)
downPolymorphic.invoke(mCamera, new Object[] { angle });
}catch (Exception e1){ }
}
/////////////////////////////////////////////////////////////////////////////////////////////
//******************************************************************************************
@Override
public void onPause(){
super.onPause();
player.pause();
}
//**************************************************************************************
//*******************************STARTVIDEO*********************************************
@SuppressLint("NewApi")
private void startVideo() {
SurfaceHolder videoCaptureViewHolder = null;
try {
mCamera = Camera.open(1);
} catch (RuntimeException e) {
Log.e("CameraTest", "Camera Open filed");
return;
}
mCamera.setErrorCallback(new ErrorCallback() {
public void onError(int error, Camera camera) {
}
});
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
Camera.Size cs = previewSizes.get(0);
parameters.setPreviewSize(cs.width,cs.height);
setDisplayOrientation(mCamera, 90);
mCamera.setParameters(parameters);
if (null != mVideoCaptureView)
videoCaptureViewHolder = mVideoCaptureView.getHolder();
try {
mCamera.setPreviewDisplay(videoCaptureViewHolder);
} catch (Throwable t) {
}
Log.v("CameraTest","Camera PreviewFrameRate = "+mCamera.getParameters().getPreviewFrameRate());
Size previewSize=mCamera.getParameters().getPreviewSize();
int dataBufferSize=(int)(previewSize.height*previewSize.width* (ImageFormat.getBitsPerPixel(mCamera.getParameters().getPreviewFormat())/8.0));
mCamera.addCallbackBuffer(new byte[dataBufferSize]);
mCamera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() {
private long timestamp=0;
public synchronized void onPreviewFrame(byte[] data, Camera camera) {
Size previewSize=camera.getParameters().getPreviewSize();
YuvImage yuvImage= new YuvImage(data,ImageFormat.NV21, previewSize.width, previewSize.height, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height),80, baos);
byte jpgData[]=baos.toByteArray();
bmp = BitmapFactory.decodeByteArray(jpgData, 0, jpgData.length);
Bitmap bmp2 = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas=new Canvas(bmp2);
Paint paint=new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
Matrix matrix = new Matrix();
matrix.setRotate(270,bmp.getWidth()/2,bmp.getHeight()/2);
canvas.drawBitmap(bmp, matrix, paint);
faces = new FaceDetector.Face[MAX_FACES];
int mFaceWidth = bmp2.getWidth();
int mFaceHeight = bmp2.getHeight();
PointF midPoint = new PointF(); //inizializza Punto di coordinate float
FaceDetector detector = new FaceDetector(mFaceWidth, mFaceHeight,MAX_FACES);
int facesFound = detector.findFaces(bmp2, faces); //bmp
if(facesFound > 0 && message_started==false){
faces[0].getMidPoint(midPoint);
float eyeDistance = faces[0].eyesDistance();
float confidence = faces[0].confidence();
player.start(); //play of "You look good today!"
message_started = true;
}
miaImmagine2.setImageBitmap(bmp2);
//+++++++++++++++++++++PARTE FINALE++++++++++++++++++++++++++++++++++++++++
try{
camera.addCallbackBuffer(data);
}catch (Exception e) {
Log.e("CameraTest", "addCallbackBuffer error");
return;
}
return;
}
});
try {
mCamera.startPreview();
} catch (Throwable e) {
mCamera.release();
mCamera = null;
return;
}
}
//*************************************STARTVIDEO_END***************************************
////////////////////////////////////////////////////////////////////////////////////////////
//*************************************STOPVIDEO********************************************
private void stopVideo() {
if(null==mCamera)
return;
try {
mCamera.stopPreview();
mCamera.setPreviewDisplay(null);
mCamera.setPreviewCallbackWithBuffer(null);
mCamera.release();
} catch (IOException e) {
e.printStackTrace();
return;
}
mCamera = null;
}
//**********************************STOPVIDEO_END********************************************
//////////////////////////////////////////////////////////////////////////////////////////////
//**********************************FINISH***************************************************
public void finish(){
stopVideo();
super.finish();
};
//**********************************FINISH_END***********************************************
}