创建Android ndk工程
添加sdk到工程
复制OpenCV-android-sdk\sdk\native\jni\include目录到R1_OpenCV2\app\src\main目录下,这里命名为opencv2_include。
复制OpenCV-android-sdk\sdk\native\libs目录到R1_OpenCV2\app\src\main目录下,这里命名为opencv2_libs。
复制一张图片到R1_OpenCV2\app\src\main\res\drawable目录下,文件名为logo。
activity_main.xml
文件<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical">
<ImageView
android:id="@+id/Original_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="高斯模糊"/>
<ImageView
android:id="@+id/Image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
MainActivity.java
文件package com.youyeetoo.r1_opencv2;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.youyeetoo.r1_opencv2.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
// Used to load the 'r1_opencv2' library on application startup.
static {
System.loadLibrary("r1_opencv2");
}
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Example of a call to a native method
// TextView tv = binding.sampleText;
// tv.setText(stringFromJNI());
Bitmap Original_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
ImageView Original_image=findViewById(R.id.Original_image);
Original_image.setImageBitmap(Original_bitmap);
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
opencv_test(bitmap);
ImageView Image=findViewById(R.id.Image);
Image.setImageBitmap(bitmap);
}
});
}
/**
* A native method that is implemented by the 'r1_opencv2' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public static native void opencv_test(Object bitmap);
}
CMakeLists.txt
文件# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.22.1)
# Declares and names the project.
project("r1_opencv2")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
r1_opencv2
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
set(OPENCV_LIBS_DIR ${CMAKE_SOURCE_DIR}/../opencv2_include)
include_directories(${OPENCV_LIBS_DIR})
add_library(libopencv_java4 SHARED IMPORTED)
set_target_properties(
libopencv_java4
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/../opencv2_libs/${ANDROID_ABI}/libopencv_java4.so)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
r1_opencv2
#jnigraphics库可以用来从android.bitmap.Graphics类 访问C / C ++中的位图缓冲区
jnigraphics
libopencv_java4
# Links the target library to the log library
# included in the NDK.
${log-lib})
native-lib.cpp
文件#include <jni.h>
#include <string>
#include <android/bitmap.h>
#include <opencv2/opencv.hpp>
using namespace cv;
extern "C" JNIEXPORT jstring JNICALL
Java_com_youyeetoo_r1_1opencv2_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_youyeetoo_r1_1opencv2_MainActivity_opencv_1test(JNIEnv *env, jclass clazz,
jobject bitmap) {
// TODO: implement opencv_test()
AndroidBitmapInfo info;
void *pixels;
CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0);
//判断图片是位图格式有RGB_565 、RGBA_8888
CV_Assert(info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 ||
info.format == ANDROID_BITMAP_FORMAT_RGB_565);
CV_Assert(AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0);
CV_Assert(pixels);
//将bitmap转化为Mat类
Mat image(info.height, info.width, CV_8UC4, pixels);
// 高斯模糊
GaussianBlur(image, image, Size(101, 101), 0);
}