主要思路:
1.在Activity 的onCreate中添加DrawingView,用于显示矩形框
<pre name="code" class="java"><span style="font-size:18px;">public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 屏幕方向设置
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
....
drawingView = new DrawingView(this);
LayoutParams layoutParamsDrawing = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(drawingView, layoutParamsDrawing);
}</span>
<span style="font-size:18px;">FaceDetectionListener faceDetectionListener = new FaceDetectionListener() {
@Override
public void onFaceDetection(final Face[] faces, Camera camera) {
if (faces.length == 0) {
prompt.setText(" No Face Detected! ");
drawingView.setHaveFace(false);
Log.i(TAG, " No Face Detected! ");
} else {
prompt.setText(String.valueOf(faces.length)
+ " Face Detected :) ");
drawingView.setHaveFace(true);
detectedFaces = faces;
Log.i(TAG, " Face Detected :) ");
}
}
};</span>
3.1在surface创建时,创建相机对象并绑定监听器
<span style="font-size:18px;">public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
camera.setFaceDetectionListener(faceDetectionListener);
}</span>
3.2在surface改变时,开启相机人脸识别
<span style="font-size:18px;">public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
...
int maxDetectFace = camera.getParameters()
.getMaxNumDetectedFaces();
if (maxDetectFace > 0)
<strong>camera.startFaceDetection();</strong>
previewing = true;
...
}</span>
下载链接:
参考文献:
http://www.tuicool.com/articles/JJVbmu
http://www.tuicool.com/articles/BRji6z