手机版
你好,游客 登录 注册
背景:
阅读新闻

Android - JNI静态(static)加载OpenCV

[日期:2014-10-05] 来源:Linux社区  作者:caroline_wendy [字体:大 中 小]

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 <jni.h>
#include <opencv2/opencv.hpp>

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. 输出

 

 

 

--------------------------------------分割线 --------------------------------------

Ubuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm

--------------------------------------分割线 --------------------------------------

OpenCV的详细介绍:请点这里
OpenCV的下载地址:请点这里

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/107632.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款