我尝试为简单的 c 代码编写 jni 调用。当我尝试在运行 2.3.3 的手机上安装它时,它没有安装,有时即使它安装了它也会被强制关闭。请帮我解决这个问题。代码详情如下:
我生成头文件的java代码。
package com.hosa;
public class edgejava
{
static{
System.loadLibrary("edgejava");
}
public native int main();
}
生成的头文件如下
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_hosa_edgejava */
#ifndef _Included_com_hosa_edgejava
#define _Included_com_hosa_edgejava
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_hosa_edgejava
* Method: main
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_hosa_edgejava_main
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
本机功能的实现如下
#include "com_hosa_edgejava.h"
#include <jni.h>
#include <cv.h>
#include <highgui.h>
using namespace cv;
JNIEXPORT jint JNICALL Java_com_pes_edgejava_main(JNIEnv *, jobject){
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",CV_WINDOW_AUTOSIZE);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
android.mk 文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include /home/srijith/android-opencv/OpenCV-2.3.1/share/OpenCV/OpenCV.mk
LOCAL_MODULE := edgejava
LOCAL_SRC_FILES := edgecpp.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
应用程序.mk
APP_MODULES := edgejava
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
活动文件如下
package com.hosa;
import android.app.Activity;
import android.os.Bundle;
public class Andedge2Activity extends Activity {
edgejava nativelib;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nativelib= new edgejava();
int i=nativelib.main();
System.out.println("value returned to andedgeactivity "+i);
}
}
清单的所有必要更改都已完成,例如提及使用相机和设置权限。
编辑:我已更改com_pes_edgejava
为com_hosa_edgejava_main
但仍无法正常工作...
还有其他解决方法??????