【问题标题】:Android NDK set RGB bitmap pixelsAndroid NDK 设置 RGB 位图像素
【发布时间】:2012-08-02 10:11:28
【问题描述】:

我正在尝试使用 androids ndk 进行一些简单的图像过滤,但似乎在获取和设置位图的 rgb 值时遇到了一些问题。

我已经去掉了所有的实际处理,只是试图将位图的每个像素都设置为红色,但我最终得到了一个蓝色的图像。我认为我忽略了一些简单的事情,但感谢您提供任何帮助。

static void changeIt(AndroidBitmapInfo* info, void* pixels){
int x, y, red, green, blue;

for (y=0;y<info->height;y++) {


     uint32_t * line = (uint32_t *)pixels;
        for (x=0;x<info->width;x++) {

            //get the values
            red = (int) ((line[x] & 0xFF0000) >> 16);
            green = (int)((line[x] & 0x00FF00) >> 8);
            blue = (int) (line[x] & 0x0000FF);

            //just set it to all be red for testing
            red = 255;
            green = 0;
            blue = 0;

            //why is the image totally blue??
            line[x] =
              ((red << 16) & 0xFF0000) |
              ((green << 8) & 0x00FF00) |
              (blue & 0x0000FF);
        }

        pixels = (char *)pixels + info->stride;
    }
}

我应该如何获取并设置每个像素的 rgb 值??

更新答案
正如下面所指出的,似乎使用了小端,所以在我的原始代码中,我只需要切换红色和蓝色变量:

static void changeIt(AndroidBitmapInfo* info, void* pixels){
int x, y, red, green, blue;

for (y=0;y<info->height;y++) {


     uint32_t * line = (uint32_t *)pixels;
        for (x=0;x<info->width;x++) {

            //get the values
            blue = (int) ((line[x] & 0xFF0000) >> 16);
            green = (int)((line[x] & 0x00FF00) >> 8);
            red = (int) (line[x] & 0x0000FF);

            //just set it to all be red for testing
            red = 255;
            green = 0;
            blue = 0;

            //why is the image totally blue??
            line[x] =
              ((blue<< 16) & 0xFF0000) |
              ((green << 8) & 0x00FF00) |
              (red & 0x0000FF);
        }

        pixels = (char *)pixels + info->stride;
    }
}

【问题讨论】:

    标签: android c image-processing android-ndk bit-manipulation


    【解决方案1】:

    这取决于像素格式。大概您的位图是 RGBA 格式的。所以0x00FF0000对应字节序列0x00、0x00、0xFF、0x00(小端序),即为蓝色,透明度为0。

    我不是 Android 开发人员,所以我不知道是否有帮助函数来获取/设置颜色组件,或者您是否必须自己根据 AndroidBitmapInfo.format 字段进行操作。您必须阅读 API 文档。

    【讨论】:

    • 除非我弄错了,否则我相信位图是 ARGB。上面的代码实际上是 C 代码,因为它是 NDK(本机开发工具包)而不是 SDK。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    相关资源
    最近更新 更多