1.下载openCV for Android 库 链接 https://github.com/opencv/opencv/releases/tag/3.3.0
2.导入添加依赖,



openCV SDK结构

3.导入libs


so mips就不导了
最终是这个样子的

4.openCVLibrary的build.gradle和你项目的build.gradle 的 需要保持一致
自己项目的:
compileSdkVersion 26
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.doudoubird.opencv_one"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
openCV的
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
// applicationId "com.doudoubird.myopencv"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
5.最后需要构建自己的Native_Libs, build.gradle中需要这样写
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.doudoubird.opencv_one"
minSdkVersion 15
targetSdkVersion 25
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')
compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
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:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile project(':openCVLibrary330')
}
task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
destinationDir file("$projectDir/libs")
baseName "Native_Libs2"
extension "jar"
from fileTree(dir: "libs", include: "**/*.so")
into "lib"
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
以上也可以参考这篇博客 http://blog.csdn.net/qq_18870023/article/details/58203990
现在可以编写代码把一张彩色图变成灰度图啦
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static String TAG = "TAG";
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLoadOpenCVLibs();
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
private void initLoadOpenCVLibs() {
boolean success = OpenCVLoader.initDebug();
if (success){
Log.e(TAG, "加载完了 ");
}
}
@Override
public void onClick(View view) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.mipmap.ic_launcher,options);
Mat src = new Mat();
Mat dst = new Mat();
Utils.bitmapToMat(bitmap,src);
Imgproc.cvtColor(src,dst,Imgproc.COLOR_BGRA2GRAY);
Utils.matToBitmap(dst,bitmap);
ImageView imagview = (ImageView) this.findViewById(R.id.imageView);
imagview.setImageBitmap(bitmap);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doudoubird.opencv_one.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="环境测试-灰度"
/>
<ImageView
android:layout_below="@+id/button"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@mipmap/ic_launcher"
/>
</RelativeLayout>
效果如下

变后

网友评论