#include <jni.h>#include <libusb/libusb/libusbi.h>
__attribute__((section (".mytext"))) staticstaticint
nativeSendStream(JNIEnv *env, jobject thiz, jbyteArray byteArray, int timeout) {
if (mFaceDtr == NULL || mFaceDtr->mFaceDtrDev == NULL) {
LOGE("facedtr not init");
return -1;
}
if (byteArray == NULL) {
LOGE("Byte array is null");
return -1;
}
jsize length = env->GetArrayLength(byteArray);
if (length == 0) {
LOGE("Byte array is empty");
return -1;
}
jbyte *jbuffer = env->GetByteArrayElements(byteArray, NULL);
int len = 0;
int result = libusb_bulk_transfer(mFaceDtr->mFaceDtrDev->handle,
mFaceDtr->mFaceDtrDev->bulk_endpoint_out,
(unsignedchar *) jbuffer, length,
&len,
timeout);
if (jbuffer != NULL) {
env->ReleaseByteArrayElements(byteArray, jbuffer, JNI_ABORT);
env->DeleteLocalRef(byteArray);
}
env->DeleteLocalRef(byteArray);
if (result != LIBUSB_SUCCESS) {
len = -1;
}
return len;
}
static JNINativeMethod methods[] = {
{"nativeSendStream", "([BI)I", (void *) nativeSendStream},
};
jint registerNativeMethods(JNIEnv *env, constchar *class_name, JNINativeMethod *methods,
int num_methods) {
int result = 0;
jclass clazz = env->FindClass(class_name);
if (LIKELY(clazz)) {
int result = env->RegisterNatives(clazz, methods, num_methods);
if (UNLIKELY(result < 0)) {
LOGE("registerNativeMethods failed(class=%s)", class_name);
}
} else {
LOGE("registerNativeMethods: class'%s' not found", class_name);
}
return result;
}
int register_libFaceDtr(JNIEnv *env) {
LOGV("register_libfacedtr:");
if (registerNativeMethods(env,
"com/xxx/xx/FaceDtr",
methods, NUM_ARRAY_ELEMENTS(methods)) < 0) {
return -1;
}
return0;
}
在Android.mk文件中注册代码
# Copyright (C) 2009 The Android Open Source Project## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/ \
$(LOCAL_PATH)/../ \
LOCAL_MODULE := facedetection
LOCAL_CFLAGS := -fvisibility=hidden
LOCAL_LDLIBS += -llog
LOCAL_SRC_FILES := \
_onload.cpp \
face_dtr_jni.cpp \
include $(BUILD_SHARED_LIBRARY)