title: Android, Face detection on camera preview
body: <p>I need to find face shape (round, oval, etc.) of user when camera is in preview mode and display face outline based on the shape. I'm using <a href="http://developer.android.com/reference/android/hardware/Camera.FaceDetectionListener.html" rel="nofollow">Camera.FaceDetectionListener</a> in my project and minimum system that my application is supporting is 14. Based on what document is saying, this function should be supported in this version but have no ieda why my device (SG2) doesn't support this feature while its version is 4.1.2.</p>
  
  <p>This is my code:</p>
  
  <pre><code>private Camera.FaceDetectionListener mFaceDetectionListener = new Camera.FaceDetectionListener() {
          @Override
          public void onFaceDetection(Camera.Face[] faces, Camera camera) {
              Log.d(TAG, "Face detected.");
  
              Camera.Face face[] = new Camera.Face[MAX_FACES];
              Log.d(TAG, face.length + " face(s) detected.");
  
              Rect rect = face[0].rect;
              Log.d(TAG, "width: " + rect.width());
              Log.d(TAG, "height: " + rect.height());
          }
      };
  
  @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          // Get context of application
          mContext = getActivity().getApplicationContext();
          if(!Device.checkCameraHardware(mContext))
              return null;
  
          // Assign layout to fragment
          View view = inflater.inflate(R.layout.fragment_face_camera, container, false);
  
          // Create an instance of Camera
          mCamera = getCameraInstance();
          mCamera.setFaceDetectionListener(mFaceDetectionListener);
  
          // Create our Preview view and set it as the content of our activity.
          mPreview = new CameraPreview(mContext, mCamera);
          FrameLayout preview = (FrameLayout) view.findViewById(R.id.camera_preview);
          preview.addView(mPreview);
  
          ImageButton captureButton = (ImageButton) view.findViewById(R.id.ibCaptureButton);
          captureButton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          // get an image from the camera
                          mCamera.takePicture(null, null, mPicture);
                      }
                  }
          );
  
          return view;
      }
  
      @Override
      public void onViewCreated(View view, Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          if(this.getView() == null) {
              Toast.makeText(mContext, "Device doesn't have camera!", Toast.LENGTH_LONG).show();
              return;
          }
  
          Camera.Parameters param = mCamera.getParameters();
          if(param.getMaxNumDetectedFaces() &gt; 0) {
              // start detecting number of faces
              mCamera.startFaceDetection();
          } else
              Log.d(TAG, "The device doesn't support face detection preview feature.");
      }
  
  @Override
      public void onPause() {
          super.onPause();
  
          releaseCamera();    // release the camera immediately on pause event
      }
  </code></pre>
  
  <p>My output is always "The device doesn't support face detection preview feature.". Does my code right?</p>
  
  <p>Based on my search I found this feature is not always trust-able (is not there in every Android devices even > v.14). So, I found <a href="http://opencv.org/platforms/android/opencv4android-samples.html" rel="nofollow">OpenCV</a> and <a href="https://code.google.com/p/javacv/" rel="nofollow">JavaCv</a> as replacement.  </p>
  
  <p>I tried to use a sample of OpenCV and it's biggest disadvantage is forcing user to download <a href="https://play.google.com/store/apps/details?id=org.opencv.engine" rel="nofollow">OpenCV Manager</a> application. Look at <a href="http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/O4A_SDK.html" rel="nofollow">this sample</a> for reference.</p>
  
  <p>There are some other libraries/techniques that finds faces in images but I'm looking this way. So, What is your suggestion? I need to have camera in preview mode and doing face recognition when camera is open, then finding user's face shape and display correct face outline (which is image as displays as overlay on top of preview).</p>
  
  <p>Any suggestion would be appreciated. Thanks</p>