以修改FaceDetection例子工程为例>
- 1,修改makefile【Android.mk】(在jni目录下)
-
OpenCV_CAMERA_MODULES:=
on
OpenCV_INSTALL_MODULES:=
on
OpenCV_LIB_TYPE:=
SHARED
include ${OpenCVROOT}/sdk/native/jni/OpenCV.mk
2,修改FdActivity.java文件
<1>增加静态函数
static {
Log.i(TAG,"OpenCV library load!");
if (!OpenCVLoader.initDebug()) {
Log.i(TAG,"OpenCV load not successfully");
} else {
System.loadLibrary("detection_based_tracker");// load other libraries
}
}
<2>删除FdActivity.java的OnResume()方法的最后那句,不让它去访问OpenCV Manager
@Override public void onResume() { super.onResume();
//OpenCVLoader.initAsync(OpenCVLoader.OpenCV_VERSION_2_4_3, this, mLoaderCallback);//
}
<3>修改FdActivity.java的OnCreate()方法
public void onCreate(Bundle savedInstanceState) { Log.i(TAG,"called onCreate"); super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.face_detect_surface_view);
// add start
try { // load cascade file from application resources InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface); File cascadeDir = getDir("cascade", Context.MODE_PRIVATE); mCascadeFile = new File(cascadeDir,"lbpcascade_frontalface.xml"); FileOutputStream os = new FileOutputStream(mCascadeFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath()); if (mJavaDetector.empty()) { Log.e(TAG,"Failed to load cascade classifier"); mJavaDetector = null; } else Log.i(TAG,"Loaded cascade classifier from"+ mCascadeFile.getAbsolutePath()); mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);// hujiawei cascadeDir.delete(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG,"Failed to load cascade. Exception thrown:"+ e); }
//add end
mOpenCVCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view); mOpenCVCameraView.enableView();
// add.
mOpenCVCameraView.setCvCameraViewListener(this); }
时间: 2024-10-14 07:17:01