When touch on the screen, this method is invoked.
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();
return false;
}
Then this method will create fileName for storing in external storage and takes the picture.
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);
}
This method takes bytes array and store into sdcard and imread takes it and process them.
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();
Mat a = Highgui.imread(mPictureFileName);
MatOfRect faceDetections = new MatOfRect();
Mat newImage = new Mat();
fd.mNativeDetector.detect(a, faceDetections);
// Rect[] facesArray = faceDetections.toArray();
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(mPictureFileName,newImage);
if (bool == true)
Log.d(TAG, "SUCCESS writing image to external storage");
else
Log.d(TAG, "Fail writing image to external storage");
}
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
}
Now I believe I have converted to type Mat with highgui.imread(directory). But now I want to able to detect faces on that image with rectangle then store the image in sdcard using high.imwrite but when i open the image it does not have the rectangle on faces. Any idea or suggestions on what Im doing wrong? Any reply is greatly appreciated. Thank you.