inPreferredConfig() 在 Android Gingerbread 中不起作用

发布于 2024-12-18 00:39:20 字数 3585 浏览 0 评论 0原文

我有一个在 Android 2.2 中构建的应用程序,我正在使用 inPreferredConfig() 将位图切换为 ARGB_8888 格式,但是,这在检查时似乎不起作用之后位图仍为 RGB_565 格式。我尝试将其更改为任何其他格式,但这些格式都不起作用。

如果手机或模拟器运行的是 Android 2.2,则该功能可以正常工作,但高于此的任何功能都会失败。有谁知道为什么会发生这种情况? inPreferredConfig() 在以后的 Android 版本中是否被弃用?


我正在做什么:

我正在使用 NDK 和一些我发现的 C 代码来运行一些图像处理函数(取自 http://www.ibm.com/developerworks/opensource/tutorials/os-androidndk/section5.html)。 C代码期望图像格式为ARGB_8888,尽管Android文档说默认情况下该格式应该已经为8888,但它肯定是565,所以我很困惑。

我猜我可以用 C 语言转换它......但我对 C 语言很糟糕,所以我不知道从哪里开始。

我的 C 函数:

{
    AndroidBitmapInfo  infocolor;
    void*              pixelscolor; 
    AndroidBitmapInfo  infogray;
    void*              pixelsgray;
    int                ret;
    int             y;
    int             x;


    LOGI("convertToGray");
    if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) {
        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
        return;
    }


    if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) {
        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
        return;
    }


    LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infocolor.width,infocolor.height,infocolor.stride,infocolor.format,infocolor.flags);
    if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
        LOGE("Bitmap format is not RGBA_8888 !");
        return;
    }


    LOGI("gray image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infogray.width,infogray.height,infogray.stride,infogray.format,infogray.flags);
    if (infogray.format != ANDROID_BITMAP_FORMAT_A_8) {
        LOGE("Bitmap format is not A_8 !");
        return;
    }


    if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    }

    if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    }

    // modify pixels with image processing algorithm

    for (y=0;y<infocolor.height;y++) {
        argb * line = (argb *) pixelscolor;
        uint8_t * grayline = (uint8_t *) pixelsgray;
        for (x=0;x<infocolor.width;x++) {
            grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue;
        }

        pixelscolor = (char *)pixelscolor + infocolor.stride;
        pixelsgray = (char *) pixelsgray + infogray.stride;
    }

    LOGI("unlocking pixels");
    AndroidBitmap_unlockPixels(env, bitmapcolor);
    AndroidBitmap_unlockPixels(env, bitmapgray);


}

我的 Java 函数:

 // load bitmap from resources
    BitmapFactory.Options options = new BitmapFactory.Options();
    // Make sure it is 24 bit color as our image processing algorithm expects this format
    options.inPreferredConfig = Config.ARGB_8888;
    bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.sampleimage,options);
    if (bitmapOrig != null)
        ivDisplay.setImageBitmap(bitmapOrig);

-

bitmapWip = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(),Config.ALPHA_8);
    convertToGray(bitmapOrig,bitmapWip);
    ivDisplay.setImageBitmap(bitmapWip);

谢谢,N

P.S 我同一主题的最后一个问题被删除了,这很烦人,因为我在任何地方都找不到任何答案。

I have an app built in Android 2.2 and I'm using inPreferredConfig() to switch a bitmap to ARGB_8888 format, however, this doesn't seem to work as when checked immediately afterwards the bitmap is still in RGB_565 format. I've tried changing it to any of the other formats and neither of those work either.

The function works fine if the phone or emulator is running Android 2.2, but anything above that fails. Does anyone know why this is happening? Is inPreferredConfig() depreciated in later Android versions?


What I'm doing:

I'm using the NDK with some C code I've found to run some image processing functions (taken from http://www.ibm.com/developerworks/opensource/tutorials/os-androidndk/section5.html). The C code expects the image format to be in ARGB_8888 and although the Android documentation says that the format should already be in 8888 by default but it's definitely in 565 so I'm very confused.

I'm guessing I could convert it in C...but I'm terrible at C so I wouldn't know where to start.

My C function:

{
    AndroidBitmapInfo  infocolor;
    void*              pixelscolor; 
    AndroidBitmapInfo  infogray;
    void*              pixelsgray;
    int                ret;
    int             y;
    int             x;


    LOGI("convertToGray");
    if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) {
        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
        return;
    }


    if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) {
        LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
        return;
    }


    LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infocolor.width,infocolor.height,infocolor.stride,infocolor.format,infocolor.flags);
    if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
        LOGE("Bitmap format is not RGBA_8888 !");
        return;
    }


    LOGI("gray image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",infogray.width,infogray.height,infogray.stride,infogray.format,infogray.flags);
    if (infogray.format != ANDROID_BITMAP_FORMAT_A_8) {
        LOGE("Bitmap format is not A_8 !");
        return;
    }


    if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    }

    if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) {
        LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
    }

    // modify pixels with image processing algorithm

    for (y=0;y<infocolor.height;y++) {
        argb * line = (argb *) pixelscolor;
        uint8_t * grayline = (uint8_t *) pixelsgray;
        for (x=0;x<infocolor.width;x++) {
            grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue;
        }

        pixelscolor = (char *)pixelscolor + infocolor.stride;
        pixelsgray = (char *) pixelsgray + infogray.stride;
    }

    LOGI("unlocking pixels");
    AndroidBitmap_unlockPixels(env, bitmapcolor);
    AndroidBitmap_unlockPixels(env, bitmapgray);


}

My Java functions:

 // load bitmap from resources
    BitmapFactory.Options options = new BitmapFactory.Options();
    // Make sure it is 24 bit color as our image processing algorithm expects this format
    options.inPreferredConfig = Config.ARGB_8888;
    bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.sampleimage,options);
    if (bitmapOrig != null)
        ivDisplay.setImageBitmap(bitmapOrig);

-

bitmapWip = Bitmap.createBitmap(bitmapOrig.getWidth(),bitmapOrig.getHeight(),Config.ALPHA_8);
    convertToGray(bitmapOrig,bitmapWip);
    ivDisplay.setImageBitmap(bitmapWip);

Thanks, N

P.S My last question of the same subject got deleted, which is annoying as I can't find any answers to this anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗忘曾经 2024-12-25 00:39:20

根据文档,图像默认使用 ARGB_8888 配置加载,因此我猜测它会识别位图的 RGB_565 格式并更改其配置。如果原始图像是 RGB_565 格式并且没有透明度,我不明白为什么这会成为问题。

这是文档 - 阅读最后一点:

如果该值非空,解码器将尝试解码为该内部配置。如果为空,或者无法满足请求,解码器将尝试根据系统的屏幕深度和原始图像的特征(例如是否具有每像素 alpha)来选择最佳匹配的配置(需要一个配置做)。默认情况下,图像使用 ARGB_8888 配置加载。

http://developer.android.com/reference/android/graphics /BitmapFactory.Options.html#inPreferredConfig

Images are loaded with the ARGB_8888 config by default, according to the documentation, so my guess is that it recognizes the RGB_565 format of your bitmap and changes the config for that. I don't see why this should be a problem if the original image is of RGB_565 format and has no transparency.

Here's the documentation - read the last bit:

If this is non-null, the decoder will try to decode into this internal configuration. If it is null, or the request cannot be met, the decoder will try to pick the best matching config based on the system's screen depth, and characteristics of the original image such as if it has per-pixel alpha (requiring a config that also does). Image are loaded with the ARGB_8888 config by default.

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inPreferredConfig

只是在用心讲痛 2024-12-25 00:39:20

这是旧的,但到目前为止还没有令人满意的答案:
前几天刚刚遇到同样的问题。

到目前为止我无法解决这个问题,我正在考虑编写一个转换器。因为我使用的是 OpenCV 并且他们不支持 565 格式。

保存图像并使用不同的配置再次加载它是可行的,但不幸的是对于实时相机应用程序来说这是不可行的。

看看这段代码:

如何将16位RGB565转换为24位RGB888?

This is old, but so far not satisfactory answered:
Just ran into the same problem the other day.

So far I couldn't solve it and I'm considering writing a converter. Since I'm using OpenCV and they don't support the 565 format.

Saving the image and loading it again with different configuration works, but unfortunately for a real time camera application this is not feasible.

Have a look at this code:

How does one convert 16-bit RGB565 to 24-bit RGB888?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文