好的,如果您已经为 Android 安装了 OpenCV,那么您有 Java 类和方法可用于您的目的;如果你真的想使用 C++ 对象,你必须去在 NDK 中开发......
但是在您的情况下,确实 CvHaardetectobjects
在 OpenCv 上没有 Java for android;但在这种情况下,您可以将 HaarCascade 加载到 aCascadeClassifier
中,然后您可以使用它detectMultiScale
来执行基于 HaarCascade 的检测。
你得到相同的结果CvHaardetectobjects
。
请看下面的代码:
/////////////Load the HaarCascade file into the clsssifier
try {
InputStream is3 = getResources().openRawResource(R.raw.haarcascade_mcs_mouth);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
File mCascadeFile = new File(cascadeDir, "haarcascade_mcs_mouth.xml");
FileOutputStream os3 = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
Log.e("","Non c'è problema PRIMA della lettura del file");
while ((bytesRead = is3.read(buffer)) != -1) {
os3.write(buffer, 0, bytesRead);
}
is3.close();
os3.close();
Log.e("","Non c'è problema con la lettura del file");
mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
if (mJavaDetector.empty()) {
Log.e(TAG, "Failed to load cascade classifier");
mJavaDetector = null;
} else
Log.e(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
}
//////Call detectMultiScale with the Detector that contains the cascade
mJavaDetector.detectMultiScale(mat,MapofRect,1.15,4,0,new Size(25,15),new Size(face.width(),face.height()/2));