真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android如何通過(guò)cmake的方式接入opencv

這篇文章主要講解了Android如何通過(guò)cmake的方式接入opencv,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

創(chuàng)新互聯(lián)建站企業(yè)建站,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),專(zhuān)注于網(wǎng)站建設(shè)技術(shù),精于網(wǎng)頁(yè)設(shè)計(jì),有多年建站和網(wǎng)站代運(yùn)營(yíng)經(jīng)驗(yàn),設(shè)計(jì)師為客戶打造網(wǎng)絡(luò)企業(yè)風(fēng)格,提供周到的建站售前咨詢和貼心的售后服務(wù)。對(duì)于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)中不同領(lǐng)域進(jìn)行深入了解和探索,創(chuàng)新互聯(lián)在網(wǎng)站建設(shè)中充分了解客戶行業(yè)的需求,以靈動(dòng)的思維在網(wǎng)頁(yè)中充分展現(xiàn),通過(guò)對(duì)客戶行業(yè)精準(zhǔn)市場(chǎng)調(diào)研,為客戶提供的解決方案。

簡(jiǎn)述

上篇 我們通過(guò)Java sdk的方式已經(jīng)將opencv接入到項(xiàng)目中了,如果想使用opencv sdk 提供的 C++ 頭文件與 .so動(dòng)態(tài)庫(kù),自己封裝jni這樣使用上篇的方式顯然是不能實(shí)現(xiàn)的。所以本篇我們介紹通過(guò)cmake的方式接入opencv。

接入步驟

1、新建jni項(xiàng)目

Android如何通過(guò)cmake的方式接入opencv

具體創(chuàng)建過(guò)程參考上篇:通過(guò)Java sdk方式接入opencv 。

2、導(dǎo)入so庫(kù)

在項(xiàng)目app/src/main目錄下新建jniLibs,并將解壓后的opencv sdk 目錄下對(duì)應(yīng)的路徑 sdk/native/libs 中的文件復(fù)制到j(luò)niLibs中。

Android如何通過(guò)cmake的方式接入opencv

Android如何通過(guò)cmake的方式接入opencv

2、導(dǎo)入cpp文件

將opencv sdk 目錄下對(duì)應(yīng)的路徑 sdk/native/jni/include 中的文件復(fù)制到cpp目錄中。

Android如何通過(guò)cmake的方式接入opencv

Android如何通過(guò)cmake的方式接入opencv

3、修改CMakeLists

將src/main/cpp 中的CMakeLists移動(dòng)到app目錄下。

Android如何通過(guò)cmake的方式接入opencv

2.修改CMakeLists中的內(nèi)容

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# 設(shè)置CMAKE的版本號(hào)
cmake_minimum_required(VERSION 3.4.1)

# 設(shè)置include文件夾的地址
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)

# 設(shè)置opencv的動(dòng)態(tài)庫(kù)
add_library(libopencv_java4 SHARED IMPORTED)
set_target_properties(libopencv_java4 PROPERTIES IMPORTED_LOCATION
    ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java4.so)

add_library( # Sets the name of the library.
    native-lib #.so庫(kù)名 可自定義

    # Sets the library as a shared library.
    SHARED

    # Provides a relative path to your source file(s).
    src/main/cpp/native-lib.cpp)

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)

target_link_libraries( # Specifies the target library.
    native-lib
    libopencv_java4

    # Links the target library to the log library
    # included in the NDK.
    ${log-lib})

修改app 中的build.gradle文件 defaultConfig 中配置cmake和ndk

externalNativeBuild {
    cmake {
      cppFlags "-std=c++11"
      arguments "-DANDROID_STL=c++_shared"
    }
}
ndk{
   abiFilters "armeabi-v7a","arm64-v8a"
}

android 中配置jniLibs

sourceSets{
   main{
     jniLibs.srcDirs = ['src/main/jniLibs']
   }
}

android 中配置cmake和ndk相關(guān)

externalNativeBuild {
    cmake {
      path file('CMakeLists.txt')
      version "3.10.2"
    }
  }

splits {
  abi {
    enable true
    reset()
    include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' //select ABIs to build APKs for
    universalApk true //generate an additional APK that contains all the ABIs
  }
}

如果是老項(xiàng)目則不必配置splits否則會(huì)報(bào)錯(cuò),只需要干掉下面的代碼

splits {
  abi {
    enable true
    reset()
    include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' //select ABIs to build APKs for
    universalApk true //generate an additional APK that contains all the ABIs
  }
}

最終配置完的代碼為:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
  compileSdkVersion 29

  defaultConfig {
    applicationId "com.jd.opencv"
    minSdkVersion 23
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    externalNativeBuild {
      cmake {
        cppFlags "-std=c++11"
        arguments "-DANDROID_STL=c++_shared"
      }
    }
    ndk{
      abiFilters "armeabi-v7a","arm64-v8a"
    }
  }

  sourceSets{
    main{
      jniLibs.srcDirs = ['src/main/jniLibs']
    }
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }

  externalNativeBuild {
    cmake {
      path file('CMakeLists.txt')
      version "3.10.2"
    }
  }

  splits {
    abi {
      enable true
      reset()
      include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' //select ABIs to build APKs for
      universalApk true //generate an additional APK that contains all the ABIs
    }
  }

  project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9]

  android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
      output.versionCodeOverride =
          project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + android.defaultConfig.versionCode
    }
  }
}

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  implementation 'androidx.appcompat:appcompat:1.1.0'
  implementation 'androidx.core:core-ktx:1.2.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

使用

我們將一張彩色圖片通過(guò) opencv 處理成一張灰色的照片。

1、編寫(xiě)處理照片的代碼。

創(chuàng)建native代碼

object NativeLibUtils{

  init {
    System.loadLibrary("native-lib")
  }

  external fun bitmap2Grey(pixels: IntArray, w: Int, h: Int): IntArray
}

創(chuàng)建 jni 代碼

#include 
#include 
#include 
#include
#include
#include 
#include 

using namespace cv;
using namespace std;


extern "C"
JNIEXPORT jintArray JNICALL
Java_com_mp5a5_opencv_NativeLibUtils_bitmap2Gray(JNIEnv *env, jobject instance, jintArray pixels,
                         jint w, jint h) {
  jint *cur_array;

  jboolean isCopy = static_cast(false);

  cur_array = env->GetIntArrayElements(pixels, &isCopy);
  if (cur_array == NULL) {
    return 0;
  }

  Mat img(h, w, CV_8UC4, (unsigned char *) cur_array);

  cvtColor(img, img, CV_BGRA2GRAY);
  cvtColor(img, img, CV_GRAY2BGRA);

  int size = w * h;
  jintArray result = env->NewIntArray(size);
  env->SetIntArrayRegion(result, 0, size, (jint *) img.data);
  env->ReleaseIntArrayElements(pixels, cur_array, 0);
  return result;
}

調(diào)用 native 代碼來(lái)實(shí)現(xiàn)彩色圖片轉(zhuǎn)換成灰色圖片

private fun showGrayImg() {
  val w = bitmap.width
  val h = bitmap.height
  val pixels = IntArray(w * h)
  bitmap.getPixels(pixels, 0, w, 0, 0, w, h)
  val resultData: IntArray = NativeLibUtils.bitmap2Gray(pixels, w, h)
  val resultImage = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
  resultImage.setPixels(resultData, 0, w, 0, 0, w, h)
  iv_image.setImageBitmap(resultImage)
}

完整轉(zhuǎn)換的代碼

class OpenCvActivity : AppCompatActivity(), View.OnClickListener {

  private lateinit var bitmap: Bitmap

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_opencv)
    bitmap = BitmapFactory.decodeResource(resources, R.mipmap.person)
    iv_image.setImageBitmap(bitmap)
    btn_btn1.setOnClickListener(this)
    btn_btn2.setOnClickListener(this)
  }

  override fun onClick(v: View?) {
    v?.id?.let {
      when (it) {
        R.id.btn_btn1 -> {
          showGrayImg()
        }
        R.id.btn_btn2 -> {
          showRgbImg()
        }
      }
    }
  }

  private fun showRgbImg() {
    bitmap = BitmapFactory.decodeResource(resources, R.mipmap.person)
    iv_image.setImageBitmap(bitmap)
  }

  private fun showGrayImg() {
    val w = bitmap.width
    val h = bitmap.height
    val pixels = IntArray(w * h)
    bitmap.getPixels(pixels, 0, w, 0, 0, w, h)
    val resultData: IntArray = NativeLibUtils.bitmap2Gray(pixels, w, h)
    val resultImage = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
    resultImage.setPixels(resultData, 0, w, 0, 0, w, h)
    iv_image.setImageBitmap(resultImage)
  }
}
<?xml version="1.0" encoding="utf-8"?>


  

  

    

顯示效果:

Android如何通過(guò)cmake的方式接入opencv

效果圖

看完上述內(nèi)容,是不是對(duì)Android如何通過(guò)cmake的方式接入opencv有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


網(wǎng)頁(yè)題目:Android如何通過(guò)cmake的方式接入opencv
路徑分享:http://weahome.cn/article/jecsgs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部