openCV移植到Android Studio
国内chatgpt入口: https://chatgptmirror.com?share=AHO1O5
1、引入OpenCV库
将openCV库以module的形式引入进来:File --> Import Module
2、添加依赖
点击 Project Structure --> 选中 openCVLibrary330 --> 把Compile Sdk Version改为 API 28
点击 Project Structure --> 选中 test01 --> 点击 Dependecies --> 点击 ‘+’ ,选中 Module Dependecy
3、添加jniLibs
在build.gradle中添加sourceSets
然后切换到Android视图,会自动生成一个jniLibs目录,把opencv的native目录中libs目录中的arm64-v8a,armeabi和armeabi-v7a三个文件夹复制到jniLibs中
4、主要文件
HelloOpenCVActivity
package com.example.test01;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;-v7
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.WindowManager;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* 第一个OpenCV例子
* Created by jiangdongguo on 2017/12/29.
*/
public class HelloOpenCVActivity extends AppCompatActivity {
private static final String TAG = "HelloOpenCVActivity";
@BindView(R.id.HelloOpenCvView)
CameraBridgeViewBase mOpenCvCameraView;
private LoaderCallbackInterface mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
// OpenCV引擎初始化加载成功
case LoaderCallbackInterface.SUCCESS:
Log.i(TAG, "OpenCV loaded successfully.");
// 连接到Camera
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全屏显示
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_hello);
// 绑定View
ButterKnife.bind(this);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
// 注册Camera连接状态事件监听器
mOpenCvCameraView.setCvCameraViewListener(new CameraBridgeViewBase.CvCameraViewListener2() {
@Override
public void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
return inputFrame.rgba();
}
});
}
@Override
protected void onResume() {
super.onResume();
// OpenCVLoader.initDebug()静态加载OpenCV库
// OpenCVLoader.initAsync()为动态加载OpenCV库,即需要安装OpenCV Manager
if (!OpenCVLoader.initDebug()) {
Log.w(TAG, "static loading library fail,Using Manager for initialization");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_3_0, this, mLoaderCallback);
} else {
Log.w(TAG, "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
@Override
protected void onPause() {
super.onPause();
// 断开与Camera的连接
if (mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// 断开与Camera的连接
if (mOpenCvCameraView != null) {
mOpenCvCameraView.disableView();
}
}
}
activity_hello.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<org.opencv.android.JavaCameraView
android:id="@+id/HelloOpenCvView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
opencv:camera_id="any"
opencv:show_fps="true" />
</LinearLayout>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test01">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".HelloOpenCVActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.test01"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':openCVLibrary330')
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}