android - 在 NDK 中处理 RGB_565 位图

标签 android c++ bitmap android-ndk java-native-interface

我在处理 RGB_565 位图时遇到问题。我的代码适用于 ARGB_8888: 以下是我用于 ARGB_8888 的一些代码 fragment (效果很好):

typedef struct
{

    uint8_t red;
    uint8_t green;
    uint8_t blue;
    uint8_t alpha;
} argb;
.....
.....        
void* pixelscolor;
    int ret;
    int y;
    int x;
    uint32_t *pixel;


    if ((ret = AndroidBitmap_getInfo(env, bmp, &infocolor)) < 0) {
        //return null;
    }

    if ((ret = AndroidBitmap_lockPixels(env, bmp, &pixelscolor)) < 0) {
    }
    int width = infocolor.width;
    int height = infocolor.height;

    for (y = 0; y <  height; y++) {
        argb * line = (argb *) pixelscolor;
        for (int n = 0; n < width; n++) {
            int newValue = line[n].alpha+line[n].red+line[n].green+line[n].blue;
......
....

我得到这样的结果 ARGB_8888 results .

但是在尝试 RGB_565 格式时:

typedef struct
{

    uint8_t red;
    uint8_t green;
    uint8_t blue;

} rgb;
.....
.....        
void* pixelscolor;
    int ret;
    int y;
    int x;
    uint32_t *pixel;


    if ((ret = AndroidBitmap_getInfo(env, bmp, &infocolor)) < 0) {
        //return null;
    }

    if ((ret = AndroidBitmap_lockPixels(env, bmp, &pixelscolor)) < 0) {
    }
    int width = infocolor.width;
    int height = infocolor.height;

    for (y = 0; y <  height; y++) {
        rgb * line = (rgb *) pixelscolor;
        for (int n = 0; n < width; n++) {
            int newValue = line[n].red+line[n].green+line[n].blue;
......
....

我得到以下结果:RGB_565 result

最佳答案

RGB_565 每个像素仅使用 2 个字节,即 16 位:

 1         1
 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|   red   |   green   |  blue   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

所以要访问单独的颜色 channel ,您可以使用下一个代码:

uint16_t u16_pix;

red   = (u16_pix >> 11) & 0x1f;
green = (u16_pix >>  5) & 0x3f;
blue  = (u16_pix >>  0) & 0x1f;

设置它们:

u16_pix = (red << 11) | (green << 5) | (blue);

请注意,您必须确保颜色 channel 值必须符合其限制,即

red:   0 to 31
green: 0 to 63
blue:  0 to 31

关于android - 在 NDK 中处理 RGB_565 位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38541909/

相关文章:

android - 发布有排行榜但没有成就的 Android 应用程序

c++ - Protocol Buffers - 读取所有消息通用的 header (嵌套消息)

android - 最佳内存缓存技术

c++ - 有选择地在并行区域内启用 OpenMP for 循环

java - 如何在Android中的AsyncTask中应用此代码?

C++:从位构建迭代器

android - 使用数组适配器的微调器实现

android - 如何从PlacesAutocomplete的位置中选取值并将其存储在TextFormField中

android - 解锁屏幕 Galaxy Nexus ADB

c++ - 如何设置 CreateFile() 打开 COM 端口时将使用的 DTR/RTS 状态