陳奕霖
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # How to Import OpenCV in Android NDK ###### tags: `android` `opencv` `ndk` ## Pre-Requirement * Please install Android SDK and NDK packages. (see [Android Native Development Kit (NDK)](https://hackmd.io/WO-nZYsfR1icmYbWy3jFrQ)) * Download and unzip OpenCV for Android [(link)](https://opencv.org/releases.html) ## Demo APP - OpenCVDemo ### Create a New Project * Enable C++ support ![](https://i.imgur.com/OKGg9Qw.png) * Enable Exception and RTTI Support ![](https://i.imgur.com/sG5mfsX.png) * Modify build.gradle(Module: app) to configure the ABI (armeabi-v7a as example) ``` defaultConfig { applicationId "com.jd.opencvdemo" minSdkVersion 23 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "-frtti -fexceptions" } } // Add below ndk { // Specifies the ABI configurations of your native // libraries Gradle should build and package with your APK. abiFilters 'armeabi-v7a' } } ``` ### User Interface ![](https://i.imgur.com/UcnOTHu.png) * There are several UI components * (Button) btn_load : open the Android Image Pick and load image from sd card. * (Button) btn_proess : trigger our native image process function * (Button) btn_save : save the current image into sd card * (ImageView) img_main : display the image ### Request Permission (AndroidManifest.xml) * Since this demo app requires the permission of accessing sd card to get the image, we need to add **<user-permission>** into the manifest file. ```xml= <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jd.opencvdemo"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application ... ``` * for more information about permission, see [Permission](https://developer.android.com/guide/topics/permissions/overview). ### Import Library * Copy OpenCV-android-sdk/sdk/native/jni/include folder to YourProject/app/src/main/cpp/ ![](https://i.imgur.com/9FDyAma.png) * Copy the libopencv_java3.so of corresponsed ABI (or all) in OpenCV-android-sdk/sdk/native/libs to your Android project app/src/main/jniLibs * arm64-v8a * armeabi * armeabi-v7a * mips * mips64 * x86 * x86_64 ![](https://i.imgur.com/qAQvGU2.png) * Modify CmakeList.txt * set the library path and its properties. ``` include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include) add_library( libopencv_java3 SHARED IMPORTED ) set_target_properties( libopencv_java3 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so ) ``` * link *libopencv_java3* ``` target_link_libraries( native-lib libopencv_java3 ${log-lib} ${android-lib}) ``` * Rebuild the project and you should find *libopencv_java3* is added. ![](https://i.imgur.com/DZ565A2.png) ### Build the Native Function (native-lib.cpp) * include the .h file ```cpp= #include <opencv2/opencv.hpp> #include <opencv2/core.hpp> using namespace cv; ``` * define the native function : imgProcess ```cpp= extern "C" JNIEXPORT void JNICALL Java_com_jd_opencvdemo_MainActivity_imgProcess( JNIEnv *env, jobject, /*this*/ jint h, jint w, jintArray RGBFrameData, jintArray ResFrameData) { // get data jint * pRGBFrameData = env->GetIntArrayElements(RGBFrameData, 0); jint * pResFrameData = env->GetIntArrayElements(ResFrameData, 0); // create the matrix of original image Mat mRGB(h, w, CV_8UC4, (unsigned char*)pRGBFrameData); // convert to gray image Mat mGray; cvtColor(mRGB, mGray, CV_RGBA2GRAY); // find edges by canny Mat mCanny; Canny(mGray,mCanny,10,100); // convert to RGBA image as result Mat mRes(h, w, CV_8UC4, (unsigned char*)pResFrameData); cvtColor(mCanny, mRes, CV_GRAY2RGBA); } ``` ### Call the Native Function (OpenCV.java) * load library * declare the native function ```java= public class OpenCV { // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ public native static void canny(int h, int w, int[] RGBFrameData, int[] ResFrameData); } ``` ### Control UI Component and Trigger the Process (MainActivity.java) * load image from Android gallery (btn_load) ```java= btn_load.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // open gallery Intent i = new Intent(Intent.ACTION_PICK); i.setType("image/*"); startActivityForResult(i, SELECT_IMAGE); } }); ``` * call the function by clicking button (btn_proc) ```java= btn_proc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // get image Bitmap bitmap = ((BitmapDrawable)img_main.getDrawable()).getBitmap(); int w = bitmap.getWidth(); int h = bitmap.getHeight(); // get rgb format data int[] rgb = new int[w * h]; bitmap.getPixels(rgb, 0, w, 0, 0, w, h); // call native function to process the image int[] img_res = new int[w * h]; OpenCV.canny(h, w, rgb, img_res); // show the result Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); bmp.setPixels(img_res,0,w,0,0,w,h); img_main.setImageBitmap(bmp); } }); ``` * save image in Pictures folder (btn_save) ```java= btn_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // permission check if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_SDCARD_PERMISSION); return; } boolean sdAvailable = checkSDCard(); if (!sdAvailable){ Log.e(LOG_TAG,"SD card is not available!"); return; } // find path File appDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (!appDir.exists() && !appDir.mkdirs()) { Log.e(LOG_TAG, "Directory not created"); } String fileName = "test_"+System.currentTimeMillis() + ".jpg"; File file = new File(appDir, fileName); // save file into gallery Bitmap bitmap = ((BitmapDrawable)img_main.getDrawable()).getBitmap(); try { FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } try { MediaStore.Images.Media.insertImage(getApplication().getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // make a notification to system to update the gallery getApplication().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath()))); // check whether it is saved successfully if(file.exists()){ Toast.makeText(getApplicationContext(),"Success!", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(getApplicationContext(), "Failed!", Toast.LENGTH_LONG).show(); } } }); ``` ### Result * Original Image ![](https://i.imgur.com/2c1CMZo.jpg) * Canny Image ![](https://i.imgur.com/ZNmsKjW.jpg) ## Reference * [Android NDK学习笔记:Android Studio3.1+CMAKE+OpenCV3.4配置](https://blog.csdn.net/CV_Jason/article/details/79758823) * [OpenCV使用Canny边缘检测器实现图像边缘检测](http://kongqw.com/2016/08/19/2016-08-19-OpenCV%E4%BD%BF%E7%94%A8Canny%E8%BE%B9%E7%BC%98%E6%A3%80%E6%B5%8B%E5%99%A8%E5%AE%9E%E7%8E%B0%E5%9B%BE%E5%83%8F%E8%BE%B9%E7%BC%98%E6%A3%80%E6%B5%8B/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully