Android Face Recognition

Source: Internet
Author: User

Currently, Android facial recognition (4.2) only supports face recognition, rather than face comparison.

The code below is as follows:

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() {@Overridepublic void onFaceDetection(Face[] faces, Camera camera) {// TODO Auto-generated method stub}};}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.