1.下載openCV for Android 庫 鏈接 https://github.com/opencv/opencv/releases/tag/3.3.0
2.導(dǎo)入添加依賴,
QQ截圖20171113233914.jpg
QQ截圖20171113234127.jpg
QQ截圖20171113234538.jpg
openCV SDK結(jié)構(gòu)
QQ截圖20171113234710.jpg
3.導(dǎo)入libs
QQ截圖20171113234929.jpg
QQ截圖20171113235259.jpg
so mips就不導(dǎo)了
最終是這個樣子的
QQ截圖20171113235528.jpg
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.最后需要構(gòu)建自己的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
現(xiàn)在可以編寫代碼把一張彩色圖變成灰度圖啦
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="環(huán)境測試-灰度"
/>
<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>
效果如下
Screenshot_2017-11-14-00-14-24-139_com.doudoubird.png
變后
Screenshot_2017-11-14-00-14-28-472_com.doudoubird.png