[Android Studio] Using Java to call OpenCV

时间:2024-03-21 21:33:44

Environment: Android studio 2.2.3, OpenCV 2.4.9 For Android, Android 6 with api 23 for X86


一、File: build.gradle (app)

apply plugin: 'com.android.application'

android {
compileSdkVersion
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.unsw.jeff"
minSdkVersion
targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
} dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.1.1'
testCompile 'junit:junit:4.12'
compile project(':openCVLibrary249')
}

二、File: build.gradle (openCVLibrary249)

apply plugin: 'com.android.library'

android {
compileSdkVersion
buildToolsVersion "23.0.2" defaultConfig {
minSdkVersion
targetSdkVersion
} buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}

三、模拟器(支持本地摄像头):

[Android Studio] Using Java to call OpenCV

模拟器中app摄像头权限打开:

Set --> app --> 摄像头权限Open

显示效果:

[Android Studio] Using Java to call OpenCV

开发代码


一、布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id ="@+id/activity_main"
android:layout_width ="match_parent"
android:layout_height="match_parent" tools:context="com.example.unsw.jeff.MainActivity"> <org.opencv.android.JavaCameraView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/java_camera_view"
/> </RelativeLayout>

二、功能实现核心:JavaCameraView

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);   javaCameraView = (JavaCameraView)findViewById(R.id.java_camera_view);
  javaCameraView.setVisibility(SurfaceView.VISIBLE);
  javaCameraView.setCvCameraViewListener(this); // 因为MainActivity实现了接口:CameraBridgeViewBase.CvCameraViewListener2
}
    @Override
protected void onPause() {
super.onPause();
if (javaCameraView != null) {
javaCameraView.disableView();
}
} @Override
protected void onDestroy() {
super.onDestroy();
if (javaCameraView != null) {
javaCameraView.disableView();
}
} @Override
protected void onResume() {
super.onResume();
if (OpenCVLoader.initDebug()) {
Log.i(TAG, "OpenCV loaded successfully.");
mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
else {
Log.i(TAG, "OpenCV not loaded.");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallBack);
}
}

其他Activity生命周期

    @Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
} @Override
public void onCameraViewStopped() {
mRgba.release();
} @Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();      // By opencv api in java here, do what you want to do.
return mRgba;
}

补充:

OpenCV4Android开发实录(1):移植OpenCV3.3.0库到Android Studio