Android Bitmap 像素格式与字节数组

Android踩坑指南之Bitmap:ARGB8888

下面是一个Android中通过BItMap show 一张argb图片的sample

    private void writeTxtToFile(byte[] strcontent, String fileName) {
        //生成文件夹之后,再生成文件,不然会出错

        Log.d(TAG,"TwriteTxtToFile");

        try {
            File file = new File(this.getExternalFilesDir(
                    Environment.DIRECTORY_PICTURES)+"/data.raw");
            if (!file.createNewFile()) {
                Log.d(TAG, "Directory not created");
            }
            OutputStream out = new FileOutputStream(file);
            out.write(strcontent);
        } catch (Exception e) {
            Log.e("TestFile", "Error on write File:" + e);
        }
    }
void showAvartar(byte[] picSrc) {
        if(null == picSrc) {
            Log.e(TAG, "showAvartar: picSrc is null");
            return;
        }
        Log.d(TAG, "showAvartar: ");
        Bitmap bitmap;
        //Bitmap.createBitmap()
        for(int i =0; i<128*72 ; i++) {
//            Log.d(TAG, "R: " + Integer.toHexString(picSrc[0] & 0xff));
//            Log.d(TAG, "G: " + Integer.toHexString(picSrc[0+1] & 0xff));
//            Log.d(TAG, "B: " + Integer.toHexString(picSrc[0+2] & 0xff));
//            Log.d(TAG, "A: " + Integer.toHexString(picSrc[0+3] & 0xff));
        }
        if (picSrc.length != 0) {
            //toast(0,0,0,0,"AvatarData size:" + picSrc.length,"",null);
            int[] imageData = new int[picSrc.length/4];
            int j =0 ;
            for(int i =0 ; i<128*72*4 ; i=i+4 )
            {
                imageData[j] = (picSrc[i+3] & 0xff) << 24 | (picSrc[i+2] & 0xff) << 16 | (picSrc[i+1] & 0xff) << 8 | (picSrc[i] & 0xff);
                j++;
            }
            bitmap = Bitmap.createBitmap(imageData,128, 72, Bitmap.Config.ARGB_8888);
            String fileName = "data.raw";
            writeTxtToFile(picSrc,fileName);
            if(bitmap != null){
                MainActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        imageView.setImageBitmap(bitmap);
                        imageView.invalidate();
                    }
                });
            }else {
                Log.e(TAG, "showAvartar: bitmap is null");
            }
        }else{
            Log.d(TAG, "showAvartar: picSrc.length is 0");
        }
    }

ARGB8888 32位 一个像素4个字节
踩坑:
如API中所述:

int color = (A & 0xff) << 24 | (B & 0xff) << 16 | (G & 0xff) << 8 | (R & 0xff);

也就是说 实际BitMap中图片通道顺序是ABGR

补充

skia/opencv/jpeg 引擎中的像素与字节顺序
注意这里按字节送到接口中的图像字节数组顺序是BGRA
在这里插入图片描述
所以在使用接口前要注意其像素排列与字节数组的关系是否正确 避免踩坑

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值