opencv4android 图片处理,【OpenCV4Android学习二】 基于Android Studio的OpenCV简单使用
一. 准备工作参考链接:【OpenCV4Android学习一】 Android Studio配置OpenCV(最新版)二. UI界面的设计1. 复制lena.jpg图片放到下图目录中2. 将activity_main.xml切换到Design模式,分别向右边界面拖放Button和ImageView(在下图界面选择lena.jpg图片即可)最终activity_main.xml的代码如下:三.JN.
一. 准备工作
参考链接: 【OpenCV4Android学习一】 Android Studio配置OpenCV(最新版)
二. UI界面的设计
1. 复制lena.jpg图片放到下图目录中
2. 将activity_main.xml切换到Design模式,分别向右边界面拖放Button和ImageView(在下图界面选择lena.jpg图片即可)
最终activity_main.xml的代码如下:
三.JNI静态(static)加载OpenCV
1. 首先将libs目录下的库复制到(新建的)jniLibs
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();
}
}
五. 运行结果
点击灰度按钮转为灰度图!!!
参考链接: Native OpenCV with Android Studio
更多推荐
所有评论(0)