电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

基于Android studio3.6的JNI教程之opencv實例詳解

瀏覽:74日期:2022-09-26 16:01:13

基本環境:

Android studio3.6

NDK:r14b(盡量使用該版本)

Opencv3.4.1 android sdk

基于Android studio3.6的JNI教程之opencv實例詳解

(1)新建工程OpenCVDemo,選擇,一定要選擇Native c++類型,最后要選c++14支持。

(2)File->Project Structure->SDK Location,設置這3個路徑,NDK選擇r14b。

(3)任意找一張圖片,復制到res/drawable。

(4)修改布局文件res/layout/ activity_main.xml

<?xml version='1.0' encoding='utf-8'?><RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent'> <ImageView android: android:layout_width='match_parent' android:layout_height='match_parent' /> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_alignParentBottom='true' android:orientation='horizontal'> <Button android: android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_weight='1' android:text='show' /> <Button android: android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_weight='1' android:text='process' /> </LinearLayout></RelativeLayout>

(5)修改java文件,app/src/main/java/ com.example.opencvdemo/ MainActivity

主要修改包括修改

繼承OnClickListener類,

修改onCreate方法

增加c++的接口函數,getEdge

實現點擊按鈕的方法,

整體代碼如下,

(6)Termi

package com.example.opencvdemo;import androidx.appcompat.app.AppCompatActivity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener{ // Used to load the ’native-lib’ library on application startup. static { System.loadLibrary('native-lib'); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); findViewById(R.id.show).setOnClickListener(this); findViewById(R.id.process).setOnClickListener(this); } /** * A native method that is implemented by the ’native-lib’ native library, * which is packaged with this application. */ //獲得Canny邊緣 public native void getEdge(Object bitmap); private ImageView imageView; @Override public void onClick(View v) { if (v.getId() == R.id.show) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test); imageView.setImageBitmap(bitmap); } else { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test); getEdge(bitmap); imageView.setImageBitmap(bitmap); } }}

nal下進入appsrcmainjava這一層目錄,執行,

javah com.example.opencvdemo.MainActivity

將生成的com_example_opencvdemo_MainActivity.h,剪切到app/src/main/cpp目錄下。

(7)修改app/src/main/cpp下面的native-lib.cpp,主要通過c++實現getEdge方法,主要代碼如下,

#include <jni.h>#include <string>#include 'com_example_opencvdemo_MainActivity.h'#include <android/bitmap.h>#include <opencv2/opencv.hpp>using namespace cv;extern 'C'JNIEXPORT void JNICALLJava_com_example_opencvdemo_MainActivity_getEdge(JNIEnv *env, jobject obj, jobject bitmap){ // TODO: implement getEdge() AndroidBitmapInfo info; void *pixels; CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0); 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); if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) { Mat temp(info.height, info.width, CV_8UC4, pixels); Mat gray; cvtColor(temp, gray, COLOR_RGBA2GRAY); Canny(gray, gray, 125, 225); cvtColor(gray, temp, COLOR_GRAY2RGBA); } else { Mat temp(info.height, info.width, CV_8UC2, pixels); Mat gray; cvtColor(temp, gray, COLOR_RGB2GRAY); Canny(gray, gray, 125, 225); cvtColor(gray, temp, COLOR_GRAY2RGB); } AndroidBitmap_unlockPixels(env, bitmap);}

(8)修改CMakeLists.txt

包括增加opencv包含路徑,增加opencv鏈接,增加目標庫的鏈接(OpenCV_LIBS和jnigraphics)

全部代碼如下,

# 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.4.1)# 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.#設置OpenCV-android-sdk路徑set( OpenCV_DIR E:/Android/OpenCV-android-sdk/sdk/native/jni )find_package(OpenCV REQUIRED )if(OpenCV_FOUND) include_directories(${OpenCV_INCLUDE_DIRS}) message(STATUS 'OpenCV library status:') message(STATUS ' version: ${OpenCV_VERSION}') message(STATUS ' libraries: ${OpenCV_LIBS}') message(STATUS ' include path: ${OpenCV_INCLUDE_DIRS}')else(OpenCV_FOUND) message(FATAL_ERROR 'OpenCV library not found')endif(OpenCV_FOUND) set(CMAKE_SHARED_LINKER_FLAGS '${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a') add_library( # Sets the name of the library. native-lib # 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. 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. native-lib ${OpenCV_LIBS} jnigraphics # Links the target library to the log library # included in the NDK. ${log-lib} )

(9)修改app/build.gradle

主要增加cmake的cppFlags,arguments

全部代碼如下,

apply plugin: ’com.android.application’android { compileSdkVersion 29 buildToolsVersion '29.0.3' defaultConfig { applicationId 'com.example.opencvdemo' minSdkVersion 16 targetSdkVersion 29 versionCode 1 versionName '1.0' testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' externalNativeBuild { cmake { cppFlags '-std=c++14 -frtti -fexceptions' arguments ’-DANDROID_STL=gnustl_shared’ //支持C++異常處理標準模板快,ndk16+需要注釋 //abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64' } } } 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 ’androidx.appcompat:appcompat:1.1.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’}

(10)整體目錄結構如下,

基于Android studio3.6的JNI教程之opencv實例詳解

運行程序,

基于Android studio3.6的JNI教程之opencv實例詳解

代碼鏈接:

References:

https://www.jianshu.com/p/6e16c0429044

https://www.bilibili.com/video/av55834524/

總結

到此這篇關于基于Android studio3.6的JNI教程之opencv實例詳解的文章就介紹到這了,更多相關android studio JNI教程opencv內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 小威小说网 - 新小威小说网 - 小威小说网小说搜索引擎 | 耐腐蚀泵,耐腐蚀真空泵,玻璃钢真空泵-淄博华舜耐腐蚀真空泵有限公司 | 防水套管厂家-柔性防水套管-不锈钢|刚性防水套管-天翔管道 | PVC快速门-硬质快速门-洁净室快速门品牌厂家-苏州西朗门业 | 硬度计_影像测量仪_维氏硬度计_佛山市精测计量仪器设备有限公司厂家 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 老城街小面官网_正宗重庆小面加盟技术培训_特色面馆加盟|牛肉拉面|招商加盟代理费用多少钱 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 蒸压釜_蒸养釜_蒸压釜厂家-山东鑫泰鑫智能装备有限公司 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 西安耀程造价培训机构_工程预算实训_广联达实作实操培训 | 洗石机-移动滚筒式,振动,螺旋,洗矿机-青州冠诚重工机械有限公司 | 深圳办公室装修,办公楼/写字楼装修设计,一级资质 - ADD写艺 | 深圳法律咨询【24小时在线】深圳律师咨询免费 | 超声波清洗机_超声波清洗机设备_超声波清洗机厂家_鼎泰恒胜 | 变压器配件,变压器吸湿器,武强县吉口变压器配件有限公司 | 海尔生物医疗四川代理商,海尔低温冰箱四川销售-成都壹科医疗器械有限公司 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 压力变送器-上海武锐自动化设备有限公司 | 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 【连江县榕彩涂料有限公司】官方网站 | 硅胶制品-硅橡胶制品-东莞硅胶制品厂家-广东帝博科技有限公司 | 冷热冲击试验箱_温度冲击试验箱价格_冷热冲击箱排名_林频厂家 | 营养师网,营养师考试时间,报名入口—网站首页 | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 海峰资讯 - 专注装饰公司营销型网站建设和网络营销培训 | 【中联邦】增稠剂_增稠粉_水性增稠剂_涂料增稠剂_工业增稠剂生产厂家 | 定制液氮罐_小型气相液氮罐_自增压液氮罐_班德液氮罐厂家 | 液压升降平台_剪叉式液压/导轨式升降机_传菜机定做「宁波日腾升降机厂家」 | 郑州水质检测中心_井水检测_河南废气检测_河南中环嘉创检测 | 车牌识别道闸_停车场收费系统_人脸识别考勤机_速通门闸机_充电桩厂家_中全清茂官网 | 行吊_电动单梁起重机_双梁起重机_合肥起重机_厂家_合肥市神雕起重机械有限公司 | 档案密集柜_手动密集柜_智能密集柜_内蒙古档案密集柜-盛隆柜业内蒙古密集柜直销中心 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | YAGEO国巨电容|贴片电阻|电容价格|三星代理商-深圳市巨优电子有限公司 | 微型气泵-真空-蠕动-水泵-厂家-深圳市品亚科技有限公司 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 |