检测Android OpenCV java中捕获的图像上的面部

时间:2013-10-12 17:34:41

标签: java android opencv image-processing face-detection

当触摸屏幕时,调用此方法,highgui.imwrite假设将前一个商店覆盖为sdcard方法。

 public boolean onTouch(View v, MotionEvent event) {
         Log.i(TAG,"onTouch event");

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
     String currentDateandTime = sdf.format(new Date());
     String fileName = Environment.getExternalStorageDirectory().getPath() +
                               "/sample_picture_" + currentDateandTime + ".jpg";
     mOpenCvCameraView.takePicture(fileName);
     Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
  Mat a = Highgui.imread(fileName);

  MatOfRect faceDetections = new MatOfRect();


  if (mDetectorType == JAVA_DETECTOR) {
      if (mJavaDetector != null)
  mJavaDetector.detectMultiScale(a, faceDetections);
  }
  else if (mDetectorType == NATIVE_DETECTOR) {
      if (mNativeDetector != null)
          mNativeDetector.detect(mGray, faceDetections);
  }
  else {
      Log.e(TAG, "Detection method is not selected!");
  }


     for (Rect rect : faceDetections.toArray()){

       Core.rectangle(a, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
      Boolean bool = null;
      bool =   Highgui.imwrite(fileName,a);
     if (bool == true)
        Log.d(TAG, "SUCCESS writing image to external storage");
      else
        Log.d(TAG, "Fail writing image to external storage");

    }
    return false;
}

然后,此方法将创建fileName以存储在外部存储中并拍摄照片。

public void takePicture(final String fileName) {
            Log.i(TAG, "Taking picture");
            this.mPictureFileName = fileName;
            // Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
            // Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue   

            mCamera.setPreviewCallback(null);

            // PictureCallback is implemented by the current class    

            mCamera.takePicture(null, null, this);        
        }

此方法接受字节数组并存储到sdcard中。

 public void onPictureTaken(byte[] data, Camera camera) {
    Log.i(TAG, "Saving a bitmap to file");
    // The camera preview was automatically stopped. Start it again.
    mCamera.startPreview();

  //...

    mCamera.setPreviewCallback(this);

    // Write the image in a file (in jpeg format)
    try {
        FileOutputStream fos = new FileOutputStream(mPictureFileName);

        fos.write(data);
        fos.close();

    } catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
    }



}

现在我相信我已经使用highgui.imread(目录)转换为Mat类型。 但是现在我希望能够使用矩形检测该图像上的面部,然后使用high.imwrite将图像存储在SD卡中,但是当我打开图像时,它没有面部上的矩形。 关于我做错什么的任何想法或建议?任何回复都非常感谢。谢谢。

0 个答案:

没有答案
相关问题