2

我正在使用以下代码从位图中获取像素数据,然后通过 JNI 将数组传递给 cocos2dx。当我在 cocos2d-x 中渲染图像时,红色和蓝色通道是相反的。有没有办法从像素的 int 值分别访问红色、蓝色通道,然后相互交换它们?

JAVA代码

    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    int len = bitmap.getWidth() * bitmap.getHeight();
    onFacebookImageNative(pixels, len, bitmap.getWidth(), bitmap.getHeight(), bitsPerComponent);

JNI 代码

JNIEXPORT void JNICALL Java_com_xxxx_yyyyo_yyyyo_onFacebookImageNative(JNIEnv* env, jobject,
    jintArray pixels, jint dataLen, jint width, jint height, jint bitsPerComponent)
{
yyyyo *yyyyo = yyyyo::singleton();

jint *jArr = env->GetIntArrayElements(pixels, NULL);
int pixelsInt[dataLen];
for (int i=0; i<dataLen; i++){
    pixelsInt[i] = (int)jArr[i];
}

yyyyo->onFacebookImage(pixelsInt, (int) dataLen,
        CCImage::kFmtRawData, (int) width, (int) height, (int) bitsPerComponent);
}
4

1 回答 1

2

好吧,可能是我问得太早了,没有太努力。

JAVA代码

    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    int[] finalArray = new int[bitmap.getWidth() * bitmap.getHeight()];

    for(int i = 0; i < len; i++) {

    int red = Color.red(pixels[i]);
    int green = Color.green(pixels[i]);
    int blue = Color.blue(pixels[i]);
    finalArray[i] = Color.rgb(blue, green, red);//invert sequence here.
    }


    int len = bitmap.getWidth() * bitmap.getHeight();
    onFacebookImageNative(pixels, len, bitmap.getWidth(), bitmap.getHeight(), bitsPerComponent);

如果有人有更好的答案,不涉及迭代每个像素,请在此处分享。

于 2013-03-29T09:59:15.620 回答