android______________jni和java的参数传递函数

80 篇文章 0 订阅
19 篇文章 0 订阅

一些常用函数举例:来自:https://code.google.com/p/native-buffer/source/browse/

#include <jni.h>
#include <time.h>
#include <android/log.h>
#include <android/bitmap.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define  LOG_TAG    "libnativebuffer"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

/* Set to 1 to enable debug log traces. */
#define DEBUG 0

JNIEXPORT jint JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_setIntData(JNIEnv *env,jobject obj, jintArray array,jint length){
	void *data = malloc(length*sizeof(int));
	(*env)->GetIntArrayRegion(env, array, 0,length, (jint *)data);
	return data;
}

JNIEXPORT jint JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_setBitmapData2(JNIEnv *env,jobject obj, jobject bitmap){
    AndroidBitmapInfo info;
    void *pixels;
    int ret;

    if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
        return;
    }

    if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    }

	int length = info.stride*info.height;
    void *data = malloc(length*sizeof(int));
	memcpy(data,pixels,length);

    AndroidBitmap_unlockPixels(env, bitmap);

	return data;
}

JNIEXPORT jint JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_setData(JNIEnv *env,jobject obj, jbyteArray array,jint length){
	void *data = malloc(length);
	(*env)->GetByteArrayRegion(env, array, 0,length, (jbyte *)data);
	
	unsigned char *buffer=(unsigned char *)data;  ///<----just for test 
	LOGE("AndroidBitmap_getInfo() failed ! error============%.2x=%.2x=%.2x=%.2x=%.2x=%.2x",
	                buffer[0],
	                buffer[1],
	                buffer[2],
	                buffer[3],
	                buffer[4],
	                buffer[5]);
	return data;
}

JNIEXPORT jintArray JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_getIntData(JNIEnv * env, jobject  obj, jint pointer,jint length){
	jintArray array = (*env)->NewIntArray(env,length);
	(*env)->SetIntArrayRegion(env, array, 0,length, (jint *)pointer);
	return array;
}

JNIEXPORT void JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_getBitmapData2(JNIEnv * env, jobject  obj, jint pointer, jobject bitmap){
    AndroidBitmapInfo info;
    void *pixels;
    int ret;

    if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
        return;
    }

    if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    }

	int length = info.stride*info.height;
    void *data = (void*)pointer;
	memcpy(pixels,data,length);

    AndroidBitmap_unlockPixels(env, bitmap);
}

JNIEXPORT jbyteArray JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_getData(JNIEnv * env, jobject  obj, jint pointer,jint length){
	jbyteArray array = (*env)->NewByteArray(env,length);
	(*env)->SetByteArrayRegion(env, array, 0,length, (jbyte *)pointer);
	return array;
}

JNIEXPORT void JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_free2(JNIEnv * env, jobject  obj, jint pointer){
	free((void*)pointer);
}

JNIEXPORT jint JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_getMemFree(JNIEnv * env, jobject  obj) {
	FILE *meminfo = fopen("/proc/meminfo", "r");
    if(meminfo == NULL)
        return -1;

    char line[256];
    while(fgets(line, sizeof(line), meminfo))
    {
        int ram;
        if(sscanf(line, "MemFree: %d kB", &ram) == 1)
        {
            fclose(meminfo);
            return ram;
        }
    }

    // If we got here, then we couldn't find the proper line in the meminfo file:
    // do something appropriate like return an error code, throw an exception, etc.
    fclose(meminfo);
    return -1;
}

JNIEXPORT jint JNICALL Java_pl_polidea_nativebuffer_NativeBuffer_getMemTotal(JNIEnv * env, jobject  obj) {
	FILE *meminfo = fopen("/proc/meminfo", "r");
    if(meminfo == NULL)
        return -1;

    char line[256];
    while(fgets(line, sizeof(line), meminfo))
    {
        int ram;
        if(sscanf(line, "MemTotal: %d kB", &ram) == 1)
        {
            fclose(meminfo);
            return ram;
        }
    }

    // If we got here, then we couldn't find the proper line in the meminfo file:
    // do something appropriate like return an error code, throw an exception, etc.
    fclose(meminfo);
    return -1;
}

java层的调用:

package pl.polidea.nativebuffer;
import java.util.HashMap;
import android.graphics.Bitmap;

public class NativeBuffer {
    static int bytes;
    static HashMap<Integer, Integer> pointers = new HashMap<Integer, Integer>();
    static {
        System.loadLibrary("nativebuffer");
    }
    private static native int setIntData(int[] pixels, int size);
    private static native int setBitmapData2(Bitmap bitmap);
    private static native int setData(byte[] data, int size);

    public static int setIntData(int[] pixels) {
        bytes += pixels.length * 4;
        int pointer = setIntData(pixels, pixels.length);
        pointers.put(pointer, pixels.length * 4);
        return pointer;
    }

    public static int setBitmapData(Bitmap bitmap) {
        bytes += bitmap.getRowBytes() * bitmap.getHeight();
        int pointer = setBitmapData2(bitmap);
        pointers.put(pointer, bitmap.getRowBytes() * bitmap.getHeight());
        return pointer;
    }

  
    public static int setData(byte[] data) {
        bytes += data.length;
        int pointer = setData(data, data.length);
        pointers.put(pointer, data.length);
        return pointer;
    }
    private static native int[] getIntData(int pointer, int size);

    private static native void getBitmapData2(int pointer, Bitmap bitmap);

    private static native byte[] getData(int pointer, int size);  ///<----这里返回的仅仅是地址,用来以后对native中的一块内存的操作。

   
    public static int[] getIntData(int pointer) {
        int size = pointers.get(pointer) / 4;
        return getIntData(pointer, size);
    }

    public static void getBitmapData(int pointer, Bitmap bitmap) {
        getBitmapData2(pointer, bitmap);
    }

  
    public static byte[] getData(int pointer) {
        int size = pointers.get(pointer);
        return getData(pointer, size);
    }

    private static native void free2(int pointer);

   
    public static void free(int pointer) {
        int size = pointers.get(pointer);
        bytes -= size;
        pointers.remove(pointer);
        free2(pointer);
    }
    public static native int getMemFree();
    public static native int getMemTotal();
    public static int getBytes() {
        return bytes;
    }
}

其他的调用:


static void setLong(JNIEnv *env, jobject item, const char* field, jlong value) {
    jclass cls;
    jfieldID fieldId;


    /* Get a reference to item's class */
    cls = (*env)->GetObjectClass(env, item);


    /* Look for the instance field s in cls */
    fieldId = (*env)->GetFieldID(env, cls, field, "J");
    if (fieldId == NULL)
        return;


    (*env)->SetLongField(env, item, fieldId, value);
}

在native的C文件中直接设置: setLong(env, thiz, "mInternalMediaPlayerInstance", 0);
        setLong(env, thiz, "mMediaListPlayerInstance", 0);



============================全部的API在这里=============================

转自:http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html


const struct JNINativeInterface ... = {

    NULL, 
    NULL, 
    NULL, 
    NULL, 
    GetVersion, 

    DefineClass, 
    FindClass, 

    FromReflectedMethod,
    FromReflectedField,
    ToReflectedMethod,

    GetSuperclass, 
    IsAssignableFrom, 

    ToReflectedField, 

    Throw, 
    ThrowNew, 
    ExceptionOccurred, 
    ExceptionDescribe, 
    ExceptionClear, 
    FatalError, 

    PushLocalFrame, 
    PopLocalFrame, 

    NewGlobalRef, 
    DeleteGlobalRef, 
    DeleteLocalRef, 
    IsSameObject, 
    NewLocalRef, 
    EnsureLocalCapacity, 

    AllocObject, 
    NewObject, 
    NewObjectV, 
    NewObjectA, 

    GetObjectClass, 
    IsInstanceOf, 

    GetMethodID, 

    CallObjectMethod, 
    CallObjectMethodV, 
    CallObjectMethodA, 
    CallBooleanMethod, 
    CallBooleanMethodV, 
    CallBooleanMethodA, 
    CallByteMethod, 
    CallByteMethodV, 
    CallByteMethodA, 
    CallCharMethod, 
    CallCharMethodV, 
    CallCharMethodA, 
    CallShortMethod, 
    CallShortMethodV, 
    CallShortMethodA, 
    CallIntMethod, 
    CallIntMethodV, 
    CallIntMethodA, 
    CallLongMethod, 
    CallLongMethodV, 
    CallLongMethodA, 
    CallFloatMethod, 
    CallFloatMethodV, 
    CallFloatMethodA, 
    CallDoubleMethod, 
    CallDoubleMethodV, 
    CallDoubleMethodA, 
    CallVoidMethod, 
    CallVoidMethodV, 
    CallVoidMethodA, 

    CallNonvirtualObjectMethod, 
    CallNonvirtualObjectMethodV, 
    CallNonvirtualObjectMethodA, 
    CallNonvirtualBooleanMethod, 
    CallNonvirtualBooleanMethodV, 
    CallNonvirtualBooleanMethodA, 
    CallNonvirtualByteMethod, 
    CallNonvirtualByteMethodV, 
    CallNonvirtualByteMethodA, 
    CallNonvirtualCharMethod, 
    CallNonvirtualCharMethodV, 
    CallNonvirtualCharMethodA, 
    CallNonvirtualShortMethod, 
    CallNonvirtualShortMethodV, 
    CallNonvirtualShortMethodA, 
    CallNonvirtualIntMethod, 
    CallNonvirtualIntMethodV, 
    CallNonvirtualIntMethodA, 
    CallNonvirtualLongMethod, 
    CallNonvirtualLongMethodV, 
    CallNonvirtualLongMethodA, 
    CallNonvirtualFloatMethod, 
    CallNonvirtualFloatMethodV, 
    CallNonvirtualFloatMethodA, 
    CallNonvirtualDoubleMethod, 
    CallNonvirtualDoubleMethodV, 
    CallNonvirtualDoubleMethodA, 
    CallNonvirtualVoidMethod, 
    CallNonvirtualVoidMethodV, 
    CallNonvirtualVoidMethodA, 

    GetFieldID, 

    GetObjectField, 
    GetBooleanField, 
    GetByteField, 
    GetCharField, 
    GetShortField, 
    GetIntField, 
    GetLongField, 
    GetFloatField, 
    GetDoubleField, 
    SetObjectField, 
    SetBooleanField, 
    SetByteField, 
    SetCharField, 
    SetShortField, 
    SetIntField, 
    SetLongField, 
    SetFloatField, 
    SetDoubleField, 

    GetStaticMethodID, 

    CallStaticObjectMethod, 
    CallStaticObjectMethodV, 
    CallStaticObjectMethodA, 
    CallStaticBooleanMethod, 
    CallStaticBooleanMethodV, 
    CallStaticBooleanMethodA, 
    CallStaticByteMethod, 
    CallStaticByteMethodV, 
    CallStaticByteMethodA, 
    CallStaticCharMethod, 
    CallStaticCharMethodV, 
    CallStaticCharMethodA, 
    CallStaticShortMethod, 
    CallStaticShortMethodV, 
    CallStaticShortMethodA, 
    CallStaticIntMethod, 
    CallStaticIntMethodV, 
    CallStaticIntMethodA, 
    CallStaticLongMethod, 
    CallStaticLongMethodV, 
    CallStaticLongMethodA, 
    CallStaticFloatMethod, 
    CallStaticFloatMethodV, 
    CallStaticFloatMethodA, 
    CallStaticDoubleMethod, 
    CallStaticDoubleMethodV, 
    CallStaticDoubleMethodA, 
    CallStaticVoidMethod, 
    CallStaticVoidMethodV, 
    CallStaticVoidMethodA, 

    GetStaticFieldID, 

    GetStaticObjectField, 
    GetStaticBooleanField, 
    GetStaticByteField, 
    GetStaticCharField, 
    GetStaticShortField, 
    GetStaticIntField, 
    GetStaticLongField, 
    GetStaticFloatField, 
    GetStaticDoubleField, 

    SetStaticObjectField, 
    SetStaticBooleanField, 
    SetStaticByteField, 
    SetStaticCharField, 
    SetStaticShortField, 
    SetStaticIntField, 
    SetStaticLongField, 
    SetStaticFloatField, 
    SetStaticDoubleField, 

    NewString, 

    GetStringLength, 
    GetStringChars, 
    ReleaseStringChars, 
 
    NewStringUTF, 
    GetStringUTFLength, 
    GetStringUTFChars, 
    ReleaseStringUTFChars, 
 
    GetArrayLength, 
  
    NewObjectArray, 
    GetObjectArrayElement, 
    SetObjectArrayElement, 
 
    NewBooleanArray, 
    NewByteArray, 
    NewCharArray, 
    NewShortArray, 
    NewIntArray, 
    NewLongArray, 
    NewFloatArray, 
    NewDoubleArray, 
 
    GetBooleanArrayElements, 
    GetByteArrayElements, 
    GetCharArrayElements, 
    GetShortArrayElements, 
    GetIntArrayElements, 
    GetLongArrayElements, 
    GetFloatArrayElements, 
    GetDoubleArrayElements, 
 
    ReleaseBooleanArrayElements, 
    ReleaseByteArrayElements, 
    ReleaseCharArrayElements, 
    ReleaseShortArrayElements, 
    ReleaseIntArrayElements, 
    ReleaseLongArrayElements, 
    ReleaseFloatArrayElements, 
    ReleaseDoubleArrayElements, 
 
    GetBooleanArrayRegion, 
    GetByteArrayRegion, 
    GetCharArrayRegion, 
    GetShortArrayRegion, 
    GetIntArrayRegion, 
    GetLongArrayRegion, 
    GetFloatArrayRegion, 
    GetDoubleArrayRegion, 
    SetBooleanArrayRegion, 
    SetByteArrayRegion, 
    SetCharArrayRegion, 
    SetShortArrayRegion, 
    SetIntArrayRegion, 
    SetLongArrayRegion, 
    SetFloatArrayRegion, 
    SetDoubleArrayRegion, 
 
    RegisterNatives, 
    UnregisterNatives, 
 
    MonitorEnter, 
    MonitorExit, 
 
    GetJavaVM, 

    GetStringRegion,
    GetStringUTFRegion,

    GetPrimitiveArrayCritical,
    ReleasePrimitiveArrayCritical,

    GetStringCritical,
    ReleaseStringCritical,

    NewWeakGlobalRef,
    DeleteWeakGlobalRef,

    ExceptionCheck,

    NewDirectByteBuffer,
    GetDirectBufferAddress,
    GetDirectBufferCapacity
}; 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值