Android开发笔记之Jni编程第二弹

继上篇http://blog.csdn.net/u011697203/article/details/51731152博文介绍了Android studio使用jni编程的方便使用,在这篇文章中带来Android studio jni调试以及jni与java不得不说的基础故事

调试

jni调试步骤:
Android开发笔记之Jni编程第二弹_第1张图片
第一步先添加Android Native
Android开发笔记之Jni编程第二弹_第2张图片

添加完成后即可进行调试
Android开发笔记之Jni编程第二弹_第3张图片

接下来记录一下在jni学习过程中的”故事”:

其实大部分网上都有

1、jni修改java对象属性值,并回调java方法

package com.bean;

/**
 * 测试的bean类
 */
public class SkillFul {
    public String strSkill;
    public boolean booleanSkill;
    public int intSkill;

    public SkillFul() {
    }
}
     public static native void ModifyObj(SkillFul skillFul);

     JNIEXPORT void JNICALL
     Java_com_test_TestJni_ModifyObj(JNIEnv *env, jclass type, jobject skillFul) {
    //访问java对象的类
    jclass job = (*env)->GetObjectClass(env,skillFul);
    if(job == NULL){
        LOGE("=========== jni %s","对象为空");
        return;
    }
    //获得得SkillFul类的属性id
    jfieldID strFieldId = (*env)->GetFieldID(env,job,"strSkill","Ljava/lang/String;");
    jfieldID intFieldid = (*env)->GetFieldID(env,job,"intSkill","I");
    jfieldID boolFiedid = (*env)->GetFieldID(env,job,"booleanSkill","Z");
    //获得属性值
    jstring strSkill = (*env)->GetObjectField(env,skillFul,strFieldId);
    jint intSkill = (*env)->GetIntField(env,skillFul,intFieldid);
    jboolean boolSkill = (*env)->GetBooleanField(env,skillFul,boolFiedid);
    //输出jstring需要先转换为char *类型
    const char * charSkill = (*env)->GetStringUTFChars(env,strSkill,0);
    LOGE("==== 修改前的值:%s == %d ==  %d",charSkill,intSkill,boolSkill);
    //修改属性值
    (*env)->SetIntField(env,skillFul,intFieldid,250);
    //这里个Java对象的String属性赋值,需要先将本地的string类型转换为UTF-8格式的String对象NewStringUTF
    (*env)->SetObjectField(env,skillFul,strFieldId,(*env)->NewStringUTF(env,"修改后的str"));
    (*env)->SetBooleanField(env,skillFul,boolFiedid,1);

    // java 反射
    //1 . 找到java代码的 class文件,,注意这里需要包名
    jclass MainActivity = (*env)->FindClass(env,"com/ly/jnidemo/MainActivity");
    if (MainActivity == 0){
        LOGE("====find class MainActivity error");
        return;
    }
    LOGE("=====find class ");
    //2 寻找class里面的方法
    jmethodID jmethodObj = (*env)->GetMethodID(env,MainActivity,"setSkill","(Lcom/bean/SkillFul;)V");
    if (jmethodObj == 0){
        LOGE("=====find method setSkill error");
        return;
    }
    LOGE("=====find method ");
    //  默认构造函数,不传参数
    jmethodID construction_id = (*env)->GetMethodID(env,MainActivity,"", "()V");
    // 3 通过NewObject来创建对象
    jobject mainActiviyObj = (*env)->NewObject(env,MainActivity,construction_id);
    //回调Java方法
    (*env)->CallVoidMethod(env,mainActiviyObj,jmethodObj,skillFul);

    //用完之后释放指向UTF-8格式的char*的指针
    (*env)->ReleaseStringUTFChars(env,strSkill,charSkill);
     }

2、将数组传递到jni并给java多个对象赋值,并返回对象列表

    public static native ArrayList getArrayObj(int[] ints);

    JNIEXPORT jobject JNICALL
    Java_com_test_TestJni_getArrayObj(JNIEnv *env, jclass type, jintArray ints_) {
    //获取数组的长度
    int length = (*env)->GetArrayLength(env,ints_);

    jint *ints = (*env)->GetIntArrayElements(env, ints_, NULL);
    //JNI返回arraylist类型
    jclass arrayList_cla = (*env)->FindClass(env,"java/util/ArrayList");
    //获取默认初始化的构造方法id
    jmethodID construct = (*env)->GetMethodID(env,arrayList_cla,"","()V");
    jobject arrayList_obj = (*env)->NewObject(env,arrayList_cla,construct,"");
    //获得arraylist的add()方法
    jmethodID arrayList_add = (*env)->GetMethodID(env,arrayList_cla,"add","(Ljava/lang/Object;)Z");
    //找到java代码的 class文件,注意这里需要包名
    jclass SkillFul = (*env)->FindClass(env,"com/bean/SkillFul");
    if (SkillFul == 0){
        LOGE("====find class MainActivity error");
    } else{
        LOGE("=====find class ");
    }
    //  默认构造函数,不传参数
    jmethodID construct_Skillful = (*env)->GetMethodID(env,SkillFul,"","()V");
    if (construct_Skillful == 0){
        LOGE("====find method MainActivity error");
    } else{
        LOGE("=====find method ");
    }
    int i = 0;
    //这里注意for循环会报这个错误,需要将i的初始化移出来
    //Error:(74, 5) error: 'for' loop initial declarations are only allowed in C99 or C11 mode
    for(; i < length; i++) {
        //通过NewObject来创建对象
        jobject obj_Skillful = (*env)->NewObject(env,SkillFul,construct_Skillful);
        jfieldID intFieldid = (*env)->GetFieldID(env,SkillFul,"intSkill","I");
        //赋值
        (*env)->SetIntField(env,obj_Skillful,intFieldid,ints[i]);
        (*env)->CallBooleanMethod(env,arrayList_obj,arrayList_add,obj_Skillful);
    }
    (*env)->ReleaseIntArrayElements(env, ints_, ints, 0);
    return arrayList_obj;
    }

大概就上面这些了,代码注释还是较详细

之前在网上看到一篇使用jni来实现高斯模糊的方法,在模糊之前最好先对bitmap进行缩放处理,这样能加速模糊
参考 https://github.com/qiujuer/ImageBlurring

    public static native String gaussBlueBitmap(Bitmap bitmap, int r);


    #include 
    #include 
    #include 

    #define ABS(a) ((a)<(0)?(-a):(a))
    #define MAX(a, b) ((a)>(b)?(a):(b))
    #define MIN(a, b) ((a)<(b)?(a):(b))

    JNIEXPORT void JNICALL
    Java_com_ly_gaussblur_GaussBlurUtil_gaussBlueBitmap(JNIEnv *env, jclass type, jobject bitmap,jint r) {

    //对bitmap的一些基本处理
    AndroidBitmapInfo infoBitmap;
    void *pixels;
    //得到图片信息,不成功则直接返回
    if (AndroidBitmap_getInfo(env, bitmap, &infoBitmap) != ANDROID_BITMAP_RESULT_SUCCESS) {
        return;
    }

    // Check image
    if (infoBitmap.format != ANDROID_BITMAP_FORMAT_RGBA_8888 &&
        infoBitmap.format != ANDROID_BITMAP_FORMAT_RGB_565) {
        return;
    }
    // Lock all images
    if (AndroidBitmap_lockPixels(env, bitmap, &pixels) != ANDROID_BITMAP_RESULT_SUCCESS) {
        return;
    }

    // height width
    int h = infoBitmap.height;
    int w = infoBitmap.width;

    // Start
    if (infoBitmap.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
        pixels = blur_ARGB_8888((int *) pixels, w, h, r);
    } else if (infoBitmap.format == ANDROID_BITMAP_FORMAT_RGB_565) {
        pixels = blur_RGB_565((short *) pixels, w, h, r);
    }
    // End
    // Unlocks everything
    AndroidBitmap_unlockPixels(env, bitmap);
    }

下面的算法在网上都能找到,具体开始出自哪不知

    int *blur_ARGB_8888(int *pix, int w, int h, int radius) {
    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    short *r = (short *) malloc(wh * sizeof(short));
    short *g = (short *) malloc(wh * sizeof(short));
    short *b = (short *) malloc(wh * sizeof(short));
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;

    int *vmin = (int *) malloc(MAX(w, h) * sizeof(int));

    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    short *dv = (short *) malloc(256 * divsum * sizeof(short));
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (short) (i / divsum);
    }

    yw = yi = 0;

    int(*stack)[3] = (int (*)[3]) malloc(div * 3 * sizeof(int));
    int stackpointer;
    int stackstart;
    int *sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + (MIN(wm, MAX(i, 0)))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);

            rbs = r1 - ABS(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            }
            else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (y == 0) {
                vmin[x] = MIN(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = MAX(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];

            rbs = r1 - ABS(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            }
            else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            // Preserve alpha channel: ( 0xff000000 & pix[yi] )
            pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (x == 0) {
                vmin[y] = MIN(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi += w;
        }
    }

    free(r);
    free(g);
    free(b);
    free(vmin);
    free(dv);
    free(stack);
    return (pix);
}

short *blur_RGB_565(short *pix, int w, int h, int radius) {
    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    short *r = (short *) malloc(wh * sizeof(short));
    short *g = (short *) malloc(wh * sizeof(short));
    short *b = (short *) malloc(wh * sizeof(short));

    int rsum, gsum, bsum, x, y, p, i, yp, yi, yw;

    int *vmin = (int *) malloc(MAX(w, h) * sizeof(int));

    int divsum = (div + 1) >> 1;
    divsum *= divsum;

    short *dv = (short *) malloc(256 * divsum * sizeof(short));

    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (short) (i / divsum);
    }

    yw = yi = 0;

    int(*stack)[3] = (int (*)[3]) malloc(div * 3 * sizeof(int));
    int stackpointer;
    int stackstart;
    int *sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;
    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + (MIN(wm, MAX(i, 0)))];
            sir = stack[i + radius];
            sir[0] = (((p) & 0xF800) >> 11) << 3;
            sir[1] = (((p) & 0x7E0) >> 5) << 2;
            sir[2] = ((p) & 0x1F) << 3;

            rbs = r1 - ABS(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            }
            else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (y == 0) {
                vmin[x] = MIN(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (((p) & 0xF800) >> 11) << 3;
            sir[1] = (((p) & 0x7E0) >> 5) << 2;
            sir[2] = ((p) & 0x1F) << 3;

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = MAX(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];

            rbs = r1 - ABS(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
            }
            else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            // Not have alpha channel
            pix[yi] = ((((dv[rsum]) >> 3) << 11) | (((dv[gsum]) >> 2) << 5) | ((dv[bsum]) >> 3));

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];

            if (x == 0) {
                vmin[y] = MIN(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];

            yi += w;
        }
    }

    free(r);
    free(g);
    free(b);
    free(vmin);
    free(dv);
    free(stack);
    return (pix);
    }

资源下载

你可能感兴趣的:(学习记录)