2

当触摸屏幕时,调用此方法,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;
}

然后此方法将创建用于存储在外部存储中的文件名并拍摄照片。

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(directory) 转换为 Mat 类型。但是现在我希望能够使用矩形检测该图像上的人脸,然后使用 high.imwrite 将图像存储在 sdcard 中,但是当我打开图像时,人脸上没有矩形。关于我做错了什么的任何想法或建议?非常感谢任何回复。谢谢你。

4

0 回答 0