如何将R、G和B通道分别拆分为双数组,并使用Android位图进行处理



我是Android和Java的新手,仍在使用DWT-DCT-SVD方法开发Android中的图像水印应用程序。我想在双类型阵列中分别处理R、G、B通道。

这是我的代码:

//Using intent, get image from album and pass the path to variable String picturePath
Cursor cursor = getContentResolver().query(photoUri, null, null, null, null);
                    cursor.moveToFirst();
                    int indexColumn = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    picturePath = cursor.getString(indexColumn);
                    BitmapFactory.Options opt = new BitmapFactory.Options();
                    opt.inDither = false;
                    opt.inScaled = false;
                    opt.inDensity = 0;
                    opt.inJustDecodeBounds = false;
                    opt.inPurgeable = false;
                    opt.inSampleSize = 1;
                    opt.inScreenDensity = 0;
                    opt.inTargetDensity = 0;
                    sourceHostBitmap = BitmapFactory.decodeFile(picturePath);

在谷歌上搜索了几次后,我发现在解码图像后,我把值放在intArray中,如下所示:

int intArray = new int[sourceHostBitmap.getWidth()*sourceHostBitmap.getHeight()]
sourceHostBitmap.getPixels(intArray, 0, sourceHostBitmap.getWidth(), 0, 0, sourceHostBitmap.getWidth(), sourceHostBitmap.getHeight());

从这一点上讲,我不知道如何将intArray分离为三个双数组R、G和B。请帮助我完成它,谢谢

编辑时间:根据Martin Cazares的解释,如果我是对的,我可以写下面这样的代码来获得双类型阵列中图像的R、G、B值

double[] valueRed = new double[hostWidth*hostHeight];
double[] valueGreen = new double[hostWidth*hostHeight];
double[] valueBlue = new double[hostWidth*hostHeight];
for (int i = 0; i < intArray.length; i++) {
    valueRed[i] = (double) ((intArray[i] & 0x00FF0000) >> 16);
    valueGreen[i] = (double) ((intArray[i] & 0x0000FF00) >> 8);
    valueBlue[i] = (double) ((intArray[i] & 0x000000FF));
}

再次编辑:根据@Martin的建议,我制作了一个模拟位图,值为alpha=100,红色=99,绿色=98,蓝色=97,它将使十六进制=0x64636261(提醒:存储在0xAARGGBB中的格式)

for (int i =0; i < intArray.length; i++) {
    intArray[i] = 0x64636261;
}

然后以随机索引访问每个信道值

int alpha, red, green, blue;
alpha = (intArray[0] & 0xFF000000) >> 24;
red = (intArray[1] & 0x00FF0000) >> 16;
green = (intArray[2] & 0x0000FF00) >> 8;
blue = (intArray[0] & 0x000000FF);
//throw it to the TextView
TextView tv = new TextView(R.id.tv);
tv.setText(alpha + " " + red + " " + green + " " + blue);

返回值就像我想象的alpha=100,红色=99,绿色=98,蓝色=97试试看!

在这一点上,你有一个"int"s的数组,其中每个int对应于(0xAARGGBB)"A=Alpha"、"R=Red"、"G=Green"one_answers"B=Blue",每个颜色需要两个字节,所以,你可以做这样的事情:

首先,使用掩码只隔离你感兴趣的比特,关闭所有其他比特:

pixel & 0xFF000000  // isolate most significant 8 bits (Alpha)
pixel & 0x00FF0000  // isolate next 8 bits (Red)
pixel & 0x0000FF00  // isolate next 8 bits (Green)
pixel & 0x000000FF  // isolate least significant  8 bits (Blue)

然后将位移位到最低有效位,以存储到a、b、c和d中(无符号位移位):

(pixel & 0xFF000000) >>> 24 // Shift bits 24-31 to bits 0-7 Actual value of Alpha
(pixel & 0x00FF0000) >>> 16 // Shift bits 16-23 to bits 0-7 Actual value of Red
(pixel & 0x0000FF00) >>> 8  // Shift bits 8-15 to bits 0-7 Actual value of Green
(pixel & 0x000000FF)        // No need to shift the last one! Actual value of Blue

希望这能为你指明正确的方向。

问候!

最新更新