gpt4 book ai didi

java - 从纯 c 方法调用 JNI 方法

转载 作者:行者123 更新时间:2023-11-30 15:47:19 24 4
gpt4 key购买 nike

我有两个c语言类(picture.c和decoder.c)和一个Java类(VirtualACtivity.java)。我的项目有以下结构:

-android
---vlc
-----src
-------input
---------**picture.c**
-------misc
---------**decoder.c**
---vlc-android
-----src
-------org
--------videolan
---------vlc
----------gui
-----------video
--------------**VirtualActivity.java**

我在picture.c中声明了一个 native 方法:

JNIEXPORT jintArray JNICALL
Java_org_videolan_vlc_gui_video_VirtualActivity_pasoArrays(JNIEnv * env, jobject jobj, jintArray array_color){
int ancho = 480;
int alto = 270;
int size = ancho * alto;

jintArray result;
result = (*env)->NewIntArray(env, size);
if (result == NULL) {
return NULL; /* out of memory error thrown */
}

// move from the temp structure to the java structure
// SetIntArrayRegion(env, array, start, length, values);
(*env)->SetIntArrayRegion(env, result, 0, size, array_color);
return result;
}

在 VirtualActivity.java 中,

static {   
try{
Log.d(LOGTAG, "Cargando la librería native");
System.loadLibrary("native");
Log.d(LOGTAG, "Library loaded - native");
}catch (Exception e){
Log.d(LOGTAG, "Did not load library - native");
}
}

private native int[] pasoArrays(int array[]);

libnative.so是从picture.c创建的。

我想调用JNIEXPORT jintArray JNICALL
来自decoder.c 的纯c 方法的java_org_videolan_vlc_gui_video_VirtualActivity_pasoArrays(JNIEnv * env, jobject jobj, jintArray array_color)
方法。然后,我的问题是我不知道如何初始化 JNIEnv * envjobject jobj 以及是否可能。

我知道 native 代码用于连接c和Java,但我需要从c类调用其他c类的 native 方法。

<小时/>

好的,我听从了你的建议,并编写了下一个代码:

decoder.c中:

   msg_Warn( p_dec, "Definición del array");
int i;
int size = 129600;
for ( i= 0; i< size ;i++){
aux[i] = 2 ;
}
newFunction(aux) ;

picture.c中:

void newFunction(int color) {


JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[1];

args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=/home/vmg/android/android/vlc-android/src/";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;

JNI_CreateJavaVM(&jvm, (void **)&env, &args);

jclass cls = (*env)->FindClass(env,"org.videolan.vlc.gui.video.VirtualActivity");
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "arrays", "([I)V");
(*env)->CallStaticVoidMethod(env,cls, mid, color);

}

VirtualActivity.java中:

public void arrays(int arr[]){
int i;
System.out.println("La longitud del array es" + arr.length );
for (i = 0; i < arr.length; i++) {
System.out.println("Este mensaje se está imprimiendo desde java" + ", array = " + arr[i] + " para " + " i = " + i );
}
}

但是当我编译时,我收到下一个错误:

/home/vmg/android/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ../vlc/android/src/.libs/libvlccore.a(picture.o): in function newFunction:../../src/misc/picture.c:646: error: undefined reference to 'JNI_CreateJavaVM'

Android.mk中:

include $(CLEAR_VARS)
LOCAL_MODULE := native
LOCAL_SRC_FILES := ../../vlc/src/misc/picture.c

ARCH=$(ANDROID_ABI)

LOCAL_C_INCLUDES := $(VLC_SRC_DIR)/include \
$(VLC_SRC_DIR)/include/vlc \
$(ANDROID_NDK)/platforms/android-9/arch-arm/usr/include \
$(ANDROID_NDK)/platforms/android-9/arch-arm/usr/include/android \
/usr/lib/jvm/java-6-openjdk/include \
/usr/lib/jvm/java-6-openjdk/include/linux \


CPP_STATIC=$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++$(CXXSTL)/libs/$(ARCH)/libgnustl_static.a

LOCAL_CFLAGS := -std=gnu99
ifeq ($(ARCH), armeabi)
LOCAL_CFLAGS += -DHAVE_ARMEABI
# Needed by ARMv6 Thumb1 (the System Control coprocessor/CP15 is mandatory on ARMv6)
# On newer ARM architectures we can use Thumb2
LOCAL_ARM_MODE := arm
endif
ifeq ($(ARCH), armeabi-v7a)
LOCAL_CFLAGS += -DHAVE_ARMEABI_V7A
endif
LOCAL_LDLIBS := -L$(VLC_CONTRIB)/lib \
$(VLC_MODULES) \
$(VLC_BUILD_DIR)/lib/.libs/libvlc.a \
$(VLC_BUILD_DIR)/src/.libs/libvlccore.a \
$(VLC_BUILD_DIR)/compat/.libs/libcompat.a \
-L/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server \
-ldl -lz -lm -llog \
-ldvbpsi -lebml -lmatroska -ltag \
-logg -lFLAC -ltheora \
-lmpeg2 -ldca -la52 \
-lavformat -lavcodec -lswscale -lavutil -lpostproc -lgsm -lopenjpeg \
-lliveMedia -lUsageEnvironment -lBasicUsageEnvironment -lgroupsock \
-lspeex -lspeexdsp \
-lxml2 -lpng -lgnutls -lgcrypt -lgpg-error \
-lfreetype -liconv -lass -lfribidi -lopus \
-ljvm \
$(CPP_STATIC)
include $(BUILD_SHARED_LIBRARY)

在/usr/lib/jvm/java-6-openjdk/include文件夹中是jni.h文件,其中声明:

#ifdef _JNI_IMPLEMENTATION_
#define _JNI_IMPORT_OR_EXPORT_ JNIEXPORT
#else
#define _JNI_IMPORT_OR_EXPORT_ JNIIMPORT
#endif
_JNI_IMPORT_OR_EXPORT_ jint JNICALL
JNI_GetDefaultJavaVMInitArgs(void *args);

_JNI_IMPORT_OR_EXPORT_ jint JNICALL
JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args);

_JNI_IMPORT_OR_EXPORT_ jint JNICALL
JNI_GetCreatedJavaVMs(JavaVM **, jsize, jsize *);

我做错了什么?

非常感谢!

最佳答案

您不应该从 Java 以外的任何地方调用 native 方法。您的目标可以通过将所有功能包装在另一个 c 函数中,然后从 pasoArrays 和 detector.c 调用该 c 函数来实现。像这样的事情:

int[] newFunction(int[] color) {
// all the functionality goes here
}

JNIEXPORT jintArray JNICALL Java_org_videolan_vlc_gui_video_VirtualActivity_pasoArrays(JNIEnv * env, jobject jobj, jintArray array_color){
return newFunction(color);
}

// from decoder.c
return newFunction(whatever);

关于java - 从纯 c 方法调用 JNI 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17492918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com