一. 准备工作

参考链接: 【OpenCV4Android学习一】 Android Studio配置OpenCV(最新版)

二. UI界面的设计

1. 复制lena.jpg图片放到下图目录中

95d4675d336d57146236b40befad63c4.png

2. 将activity_main.xml切换到Design模式,分别向右边界面拖放Button和ImageView(在下图界面选择lena.jpg图片即可)

1354d8f1e2ff7652ef70fcc2b395c999.png

8dcf493742ab992517683d36cb90c624.png

最终activity_main.xml的代码如下:

三.JNI静态(static)加载OpenCV

1. 首先将libs目录下的库复制到(新建的)jniLibs

74486e8a0889987c55e3a896ca016238.png

0c1b842c1867783c3ee80c82068a558c.png

2. 在MainActivity.java中添加

// 静态加载OpenCV库并初始化

private void staticLoadCVLibraries() {

boolean load = OpenCVLoader.initDebug();

if(load) {

Log.i("CV", "Open CV Libraries loaded...");

}

}

3. 在app\build.gradle文件添加

sourceSets {

main {

jniLibs.srcDirs = ['src/main/jniLibs']

}

}

完整代码如下:

apply plugin: 'com.android.application'

android {

compileSdkVersion 28

defaultConfig {

applicationId "com.example.cameratutorial2"

minSdkVersion 27

targetSdkVersion 28

versionCode 1

versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

externalNativeBuild {

cmake {

cppFlags "-std=c++11"

}

}

}

sourceSets {

main {

jniLibs.srcDirs = ['src/main/jniLibs']

}

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}

}

externalNativeBuild {

cmake {

path "src/main/cpp/CMakeLists.txt"

version "3.10.2"

}

}

}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

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(path: ':openCVLibrary341')

}

四. 使用OpenCV处理图像

MainActivity.java文件完整代码如下:

package com.example.cameratutorial2;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import org.opencv.android.OpenCVLoader;

import org.opencv.android.Utils;

import org.opencv.core.CvType;

import org.opencv.core.Mat;

import org.opencv.imgproc.Imgproc;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button grayBtn = findViewById(R.id.gray_btn);

grayBtn.setOnClickListener(this);

staticLoadCVLibraries();

}

// 静态加载OpenCV库并初始化

private void staticLoadCVLibraries() {

boolean load = OpenCVLoader.initDebug();

if(load) {

Log.i("CV", "Open CV Libraries loaded...");

}

}

@Override

public void onClick(View view) {

convert2Gray();

}

private void convert2Gray() {

Mat src = new Mat();

Mat temp = new Mat();

Mat dst = new Mat();

Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.lena);

Utils.bitmapToMat(image, src);

Imgproc.cvtColor(src, temp, Imgproc.COLOR_RGB2BGR);

Log.i("CV", "image type:" + (temp.type() == CvType.CV_8UC3));

Imgproc.cvtColor(temp, dst, Imgproc.COLOR_BGR2GRAY);

Utils.matToBitmap(dst, image);

ImageView imageView = findViewById(R.id.imageView);

imageView.setImageBitmap(image);

// release memory

src.release();

temp.release();

dst.release();

}

}

五. 运行结果

645fafda9b60fd94408f25c6c49806d6.png

点击灰度按钮转为灰度图!!!

参考链接: Native OpenCV with Android Studio

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐