android 人臉識別

android的人臉識別目前爲上(4.2)只能進行人臉的識別,而不能進行人臉的對比。

下面是具體代碼

package com.example.facedetectdemo;

import java.io.IOException;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Face;
import android.hardware.Camera.FaceDetectionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AndroidCamera extends Activity implements SurfaceHolder.Callback{

	 Camera camera;
	 SurfaceView surfaceView;
	 SurfaceHolder surfaceHolder;
	 boolean previewing = false;
	 LayoutInflater controlInflater = null;
	 
	 Button buttonTakePicture;
	 TextView prompt;
	 
	 final int RESULT_SAVEIMAGE = 0;
	 
	   /** Called when the activity is first created. */
	   @Override
	   public void onCreate(Bundle savedInstanceState) {
	       super.onCreate(savedInstanceState);
	       setContentView(R.layout.activity_main);
	       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	      
	       getWindow().setFormat(PixelFormat.UNKNOWN);
	       surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
	       surfaceHolder = surfaceView.getHolder();
	       surfaceHolder.addCallback(this);
	       surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	      
	       controlInflater = LayoutInflater.from(getBaseContext());
	       View viewControl = controlInflater.inflate(R.layout.control, null);
	       LayoutParams layoutParamsControl
	        = new LayoutParams(LayoutParams.FILL_PARENT,
	        LayoutParams.FILL_PARENT);
	       this.addContentView(viewControl, layoutParamsControl);
	      
	       buttonTakePicture = (Button)findViewById(R.id.takepicture);
	      
	       LinearLayout layoutBackground = (LinearLayout)findViewById(R.id.background);	      
	       prompt = (TextView)findViewById(R.id.prompt);
	   }
	  
	   FaceDetectionListener faceDetectionListener
	   = new FaceDetectionListener(){

	  @Override
	  public void onFaceDetection(Face[] faces, Camera camera) {
	   
	   if (faces.length == 0){
	    prompt.setText(" No Face Detected! ");
	   }else{
	    prompt.setText(String.valueOf(faces.length) + " Face Detected :) ");
	    Log.i("faceScore", faces[0].score+"");
	    Point leftEye = faces[0].leftEye;
	    
//	    leftEye
	    
	   }
	   
	   
	  }};
	  
	  
	 @Override
	 public void surfaceChanged(SurfaceHolder holder, int format, int width,
	   int height) {
//	  // TODO Auto-generated method stub
	  if(previewing){
	   camera.stopFaceDetection();
	   camera.stopPreview();
	   previewing = false;
	  }
	  
	  if (camera != null){
	   try {
	    camera.setPreviewDisplay(surfaceHolder);
	    camera.startPreview();

	    prompt.setText(String.valueOf(
	      "Max Face: " + camera.getParameters().getMaxNumDetectedFaces()));
	    camera.startFaceDetection();
	    previewing = true;
	   } catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	   }
	  }
	 }

	 @Override
	 public void surfaceCreated(SurfaceHolder holder) {
	  // TODO Auto-generated method stub
	  camera = Camera.open(CameraInfo.CAMERA_FACING_FRONT);
	  camera.setFaceDetectionListener(faceDetectionListener);
	 }

	 @Override
	 public void surfaceDestroyed(SurfaceHolder holder) {
	  // TODO Auto-generated method stub
	  camera.stopFaceDetection();
	  camera.stopPreview();
	  camera.release();
	  camera = null;
	  previewing = false;
	 }
	 
	 Camera.FaceDetectionListener faceDetionListener = new Camera.FaceDetectionListener() {
		
		@Override
		public void onFaceDetection(Face[] faces, Camera camera) {
			// TODO Auto-generated method stub
			
		}
	};
	
	
	}



發佈了49 篇原創文章 · 獲贊 8 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章