Android JNI 讀寫Bitmap的方法

Java層建立Bitmap,經過JNI將Bitmap傳到C/C++進行處理java

Java部分android

public static native boolean greenBitmap(Bitmap bitmap);

C/C++部分code

JNIEXPORT jboolean JNICALL Java_com_test_Test_greenBitmap
  (JNIEnv *, jclass, jobject);
#include <android/bitmap.h>

JNIEXPORT jboolean JNICALL Java_com_test_Test_greenBitmap
  (JNIEnv *env, jclass, jobject src)
{
    unsigned char *srcData = NULL;
    AndroidBitmap_lockPixels(env, src, (void**)&srcData);
    AndroidBitmapInfo srcInfo;
    AndroidBitmap_getInfo(env, src, &srcInfo);

    int pixel = srcInfo.width * srcInfo.height;
    for(int i=0;i<pixel;i++)
    {
        srcData [4*i] = (unsigned char)0;//red
        srcData [4*i+1] = (unsigned char)255;//green
        srcData [4*i+2] = (unsigned char)0;//blue
        srcData [4*i+3] = (unsigned char)255;//alpha
    }

    AndroidBitmap_unlockPixels(env, src);
    return true;
}

Android.mkget

LOCAL_LDLIBS += -ljnigraphics
相關文章
相關標籤/搜索