最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年03月15日 11:27:14   作者:watersink  
這篇文章主要介紹了基于Android studio3.6的JNI教程之opencv實例詳解,本文通過實例代碼截圖的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

基本環(huán)境:

Android studio3.6

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

Opencv3.4.1 android sdk

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

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

(3)任意找一張圖片,復(fù)制到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:id="@+id/imageView"
 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:id="@+id/show"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="show" />
 <Button
 android:id="@+id/process"
 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++的接口函數(shù),getEdge

實現(xiàn)點擊按鈕的方法,

整體代碼如下,

(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下進(jìn)入app\src\main\java這一層目錄,執(zhí)行,

javah com.example.opencvdemo.MainActivity

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

(7)修改app/src/main/cpp下面的native-lib.cpp,主要通過c++實現(xiàn)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 JNICALL
Java_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鏈接,增加目標(biāo)庫的鏈接(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.
#設(shè)置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++異常處理標(biāo)準(zhǔn)模板快,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)整體目錄結(jié)構(gòu)如下,

運行程序,

代碼鏈接:

References:

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

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

總結(jié)

到此這篇關(guān)于基于Android studio3.6的JNI教程之opencv實例詳解的文章就介紹到這了,更多相關(guān)android studio JNI教程opencv內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android實現(xiàn)類似網(wǎng)易新聞選項卡動態(tài)滑動效果

    Android實現(xiàn)類似網(wǎng)易新聞選項卡動態(tài)滑動效果

    這篇文章主要介紹了Android實現(xiàn)類似網(wǎng)易新聞選項卡動態(tài)滑動效果的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • Android發(fā)布項目到j(luò)itpack的完整步驟

    Android發(fā)布項目到j(luò)itpack的完整步驟

    這篇文章主要給大家介紹了關(guān)于Android發(fā)布項目到j(luò)itpack的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Android編程之OpenGL繪圖技巧總結(jié)

    Android編程之OpenGL繪圖技巧總結(jié)

    這篇文章主要介紹了Android編程之OpenGL繪圖技巧,結(jié)合實例形式總結(jié)分析了Android基于OpenGL繪圖的原理與具體步驟,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android編程自定義Notification實例分析

    Android編程自定義Notification實例分析

    這篇文章主要介紹了Android編程自定義Notification的用法,結(jié)合實例形式簡單分析了自定義Notification的具體功能與實現(xiàn)技巧,需要的朋友可以參考下
    2015-12-12
  • Android md5加密與php md5加密一致詳解

    Android md5加密與php md5加密一致詳解

    這篇文章主要介紹了Android md5加密與php md5加密一致詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Android實現(xiàn)隱藏手機底部虛擬按鍵

    Android實現(xiàn)隱藏手機底部虛擬按鍵

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)隱藏手機底部虛擬按鍵,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android判斷程序是否第一次啟動

    Android判斷程序是否第一次啟動

    這篇文章主要為大家詳細(xì)介紹了Android判斷程序是否第一次啟動的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android中Bitmap、File與Uri之間的簡單記錄

    Android中Bitmap、File與Uri之間的簡單記錄

    這篇文章主要給大家介紹了關(guān)于Android中Bitmap、File與Uri之間的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • android中px和dp,px和sp之間的轉(zhuǎn)換方法

    android中px和dp,px和sp之間的轉(zhuǎn)換方法

    在Android開發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時必不可少的。下面腳本之家小編給大家?guī)砹薬ndroid中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧
    2018-06-06
  • Android布局案例之人人android九宮格

    Android布局案例之人人android九宮格

    這篇文章主要為大家詳細(xì)介紹了Android布局案例之人人android九宮格,感興趣的小伙伴們可以參考一下
    2016-03-03

最新評論

盘锦市| 手游| 兰溪市| 海伦市| 双桥区| 巍山| 桦川县| 宁明县| 台南县| 隆子县| 温宿县| 丹阳市| 神农架林区| 沛县| 青浦区| 鄄城县| 山东省| 淄博市| 牙克石市| 景东| 年辖:市辖区| 翁牛特旗| 临漳县| 五常市| 历史| 镇平县| 霍林郭勒市| 闸北区| 玉树县| 含山县| 木里| 黄平县| 文安县| 阳朔县| 襄城县| 手机| 平泉县| 揭东县| 涿州市| 金湖县| 泰和县|