【问题标题】:Incorrect colors on Android Bitmap after conversion to and from a byte array转换为字节数组和从字节数组转换后,Android Bitmap 上的颜色不正确
【发布时间】:2020-08-04 15:13:43
【问题描述】:

在 Android 中,我想加载一个 PNG,在一个字节数组中获取 RGB 值以进行一些计算,然后我想用新值重新创建一个位图。 为此,我编写了 2 个函数将位图转换为 RGB 字节数组,另一个将 RGB 字节数组转换回位图,可以忽略 alpha 通道。

这些是转换函数:

public static byte[] ARGB2byte(Bitmap img)
{
    int width = img.getWidth();
    int height = img.getHeight();

    int[] pixels = new int[height*width];
    byte rgbIm[] = new byte[height*width*3];

    img.getPixels(pixels,0,width,0,0,width,height);

    int pixel_count = 0;
    int count=width*height;

    while(pixel_count < count){
        int inVal = pixels[pixel_count];

        //Get the pixel channel values from int
        int a = (inVal >> 24) & 0xff;
        int r = (inVal >> 16) & 0xff;
        int g = (inVal >> 8) & 0xff;
        int b = inVal & 0xff;

        rgbIm[pixel_count*3] = (byte)(r);
        rgbIm[pixel_count*3 + 1] = (byte)(g);
        rgbIm[pixel_count*3 + 2] = (byte)(b);

        pixel_count++;
    }

    return rgbIm;
}

public static Bitmap byte2ARGB(byte[] data, int width, int height)
{

    int pixelsCount = data.length / 3;
    int[] pixels = new int[pixelsCount];
    for (int i = 0; i < pixelsCount; i++)
    {
        int offset = 3 * i;
        int r = data[offset];
        int g = data[offset + 1];
        int b = data[offset + 2];
        pixels[i] = Color.rgb(r, g, b);
    }

    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}

所以我尝试测试这些功能,只是从资产文件夹中加载图像,将其转换为字节数组,立即将其转换回 Bitmp 并将其保存到内部存储中,以检查最终结果是否与原始图像匹配。 不幸的是,它没有,色彩空间似乎不对。

例如,如果我加载这个 png:

并运行以下代码:

// Loads the png from assets folder
AssetManager am = getInstrumentation().getContext().getAssets();
InputStream is = am.open(filename);
Bitmap bitmap = BitmapFactory.decodeStream(is);

// Conversion to byte array
byte[] barray = ARGB2byte(bitmap);
Bitmap reconverted = Utils.byte2ARGB(barray, bitmap.getWidth(), bitmap.getHeight());

// Saving the reconverted Bitmap
try {
        String folder_path = context.getFilesDir().getAbsolutePath() + "/";
        File file = new File(folder_path + "test_conversion.png");
        FileOutputStream fos = new FileOutputStream(file);
        reconverted.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d("saving bitmap", "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d("saving bitmap", "Error accessing file: " + e.getMessage());
    }
 }

我得到这个结果:

我做错了什么? 如果我在加载后立即使用相同的代码保存原始位图,我得到的图像是正确的,所以我可能在转换过程中犯了一些错误。

我还检查了原始图像的字节数组的 R、G、B 值,将它们与从重新转换的图像获得的字节数组中的值进行比较,它们是相同的!! Android 的 Bitmap 库是否在幕后做了一些事情,也许是 alpha 通道?我想不通。

谢谢

【问题讨论】:

    标签: android arrays colors bitmap argb


    【解决方案1】:

    对不起,我先说英语不好。

    这是因为在java中将字节转换为int会得到意想不到的结果。

    改用这个

    int r = data[offset] & 0xFF;
    int g = data[offset + 1] & 0xFF;
    int b = data[offset + 2] & 0xFF;
    

    也见this post

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2014-08-08
      • 2012-01-17
      • 2010-10-08
      • 1970-01-01
      相关资源
      最近更新 更多