Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 和OpenCV整合開發(2)---去除OpenCV Manager包的方法

Android 和OpenCV整合開發(2)---去除OpenCV Manager包的方法

編輯:關於Android編程

以修改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);
    }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved