Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 教學:透過 JNI 靜態(static)方式載入 OpenCV

編輯:關於 Android 程式設計

透過 JNI 靜態(static)方式載入 OpenCV

原文出處:https://blog.csdn.net/caroline_wendy

整體步驟如下:

1. 準備 OpenCV-Android 函式庫

把 OpenCV 的 SDK 複製到工作資料夾。

下載最新的 OpenCV android-sdk:OpenCV-2.4.9-android-sdk;

將其中的sdk 資料夾複製到工作目錄;sdk 資料夾裡包含 native、java、etc 三個子資料夾。

2. 載入 OpenCV-Android 函式庫

把工作目錄中的 java 資料夾匯入(import),並設定為當前專案的函式庫。

在 [專案名稱] 上按右鍵 → Android → Add Library → 選擇剛才新增的函式庫即可。此處不需要勾選 Is Library。

3. 建立 JniClient 類別

其中包含需要使用的方法(method),可以有多個:

/** * */ package com.example.hellomyjni; /** * @author Administrator * */ public class JniClient { static public native String sayName(); static public native int[] grayImage(int[] pixels, int w, int h); } 
接著使用命令列來產生標頭檔。

進入專案資料夾,用下面的命令產生 JNI 的標頭檔:

"javah -classpath bin/classes -d jni com.example.hellomyjni.JniClient"

或 javah -classpath D:/eclipse-android/android-sdk/platforms/android-17/android.jar;bin/classes -d jni com.example.mycamera.CartoonifierView

(需包含 Android 函式庫。)

命令解析:

javah:用來產生標頭檔;

-classpath:指定類別所在位置(bin/classes),都是 .class 檔;

-d jni:指定要產生 JNI 的類別(com.example.hellomyjni.JniClient),包含 [package].[classname]。

其餘參考:https://blog.csdn.net/caroline_wendy/article/details/39032551

4. 修改 JNI 設定檔

需要在 jni 資料夾內建立 Application.mk,主要負責 C++ 標準的使用(APP_STL)以及需要編譯的函式庫。

APP_STL := gnustl_static APP_CPPFLAGS := -frtti -fexceptions APP_ABI := armeabi-v7a 
接著修改 Android.mk,這裡不需要使用 OPENCV_LIB_TYPE:=STATIC。

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) #OPENCV_LIB_TYPE:=STATIC OPENCV_INSTALL_MODULES:=on include C:/Users/Administrator/android-workspace/opencv-sdk/native/jni/OpenCV.mk LOCAL_MODULE := HelloMyJni LOCAL_SRC_FILES := HelloMyJni.cpp LOCAL_LDLIBS += -llog -ldl #LOCAL_C_INCLUDES += ./ LOCAL_SRC_FILES += ./sayname.cpp include $(BUILD_SHARED_LIBRARY) 

5. JNI 函式

#include  #include  using namespace cv; #include "com_example_hellomyjni_JniClient.h" #include "sayname.h" JNIEXPORT jstring JNICALL Java_com_example_hellomyjni_JniClient_sayName (JNIEnv *env, jclass) { string str = sayname(); return env->NewStringUTF(str.c_str()); } JNIEXPORT jintArray JNICALL Java_com_example_hellomyjni_JniClient_grayImage (JNIEnv *env, jclass, jintArray buf, jint w, jint h ) { jint *cbuf; cbuf = env->GetIntArrayElements(buf, false); if (cbuf == NULL) { return 0; } Mat imgData(h, w, CV_8UC4, (unsigned char*) cbuf); uchar* ptr = imgData.ptr(0); for (int i = 0; i < w * h; i++) { int grayScale = (int) (ptr[4 * i + 2] * 0.299 + ptr[4 * i + 1] * 0.587 + ptr[4 * i + 0] * 0.114); ptr[4 * i + 1] = grayScale; ptr[4 * i + 2] = grayScale; ptr[4 * i + 0] = grayScale; } int size = w * h; jintArray result = env->NewIntArray(size); env->SetIntArrayRegion(result, 0, size, cbuf); env->ReleaseIntArrayElements(buf, cbuf, 0); return result; } 
在專案中加上 OpenCV 標頭檔的路徑檢查,可以避免因找不到標頭檔而報錯。

6. 修改 MainActivity

在主程式裡呼叫 JNI 函式。

package com.example.hellomyjni; import org.opencv.android.OpenCVLoader; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Bitmap.Config; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity{ private ImageView iv; private Bitmap bmp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.text_view); tv.setText(JniClient.sayName()); //recall JNI iv = (ImageView) findViewById(R.id.image_view); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.shoes); int w = bmp.getWidth(); int h = bmp.getHeight(); int[] pixels = new int[w * h]; bmp.getPixels(pixels, 0, w, 0, 0, w, h); int[] resultInt = JniClient.grayImage(pixels, w, h); //recall JNI Bitmap resultImg = Bitmap.createBitmap(w, h, Config.ARGB_8888); resultImg.setPixels(resultInt, 0, w, 0, 0, w, h); iv.setImageBitmap(resultImg); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } static { if (!OpenCVLoader.initDebug()) { } else { System.loadLibrary("HelloMyJni"); } } } 

7. 執行輸出

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved