简体   繁体   English

从3个字节的数组创建位图图像

[英]Creating a bitmap image from 3 byte arrays

I would like to create bitmap image from three arrays of bytes: Range_R represents red color. 我想从三个字节数组创建位图图像: Range_R代表红色。 Range_G represents green color. Range_G代表绿色。 Range_B represents blue. Range_B代表蓝色。 How can I do this in android studio? 如何在android studio中做到这一点?

This my code but I'm not sure because the resulted image has bad different colors than the original image. 这是我的代码,但是我不确定,因为生成的图像的颜色与原始图像的颜色不同。

for (int i = 0; i < srcImg.getWidth(); i++) {
        for (int j = 0; j < srcImg.getHeight(); j++) {
            int pixel = srcImg.getPixel(i, j);
            byte Red = (byte) Color.red(Range_R[i][j]);
            byte Green = ((byte) Color.green(Range_G[i][j]));
            byte Blue = (byte) Color.blue(Range_B[i][j]);
            int alpha = Color.alpha(pixel);
            newimage.setPixel(i, j, argb(alpha,Red, Green, Blue));
        }
    }

Don't use Color.red()/green()/blue() here. 请勿在此处使用Color.red()/green()/blue() Those are for extracting byte-sized values out of a larger bitfield ( int/long ). 这些用于从更大的位域( int/long )中提取字节大小的值。 You've stated that these are byte arrays, therefor the values are already of the correct size, and likely, scale. 您已经说过这些是byte数组,因此这些值已经具有正确的大小,并且可能是小数位数。

Also, you should make sure that Red , Green , and Blue are each in the range [0..255]. 另外,您应确保RedGreenBlue分别在[0..255]范围内。 Some of the array members are likely negative; 一些数组成员可能是负数; in this case, you will have to increase their value by 256: 在这种情况下,您必须将其值增加256:

if(Red < 0)
    Red += 256;

...and so forth. ...等等。 This is to meet the input specification of argb() . 这是为了满足argb()的输入规范。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM