RGB转NV21格式


NV21 格式属于 YUV420SP 类型,是安卓中有的模式,它的存储顺序是先存 Y 分量,再 VU 交替存储。存储大小=height * width * 3 / 2。
注意, 对于图片宽、高为奇数时,需要进行处理,否则会在byte[] yuv时发生越界。如果将byte[] yuv的初始化采用byte [] yuv = new byte[height * width + 2 * (int) Math.ceil(height / 2.0) * (int) Math.ceil(width / 2.0)],保存出的YUV格式图片不正确。 因此,采用了将奇数的宽、高,进行减一变成偶数

读取本地图片生成Bitmap

FileInputStream fis = new FileInputStream("/sdcard/test/test.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(fis);

RGB转NV21

int width = bitmap.getWidth();
int height = bitmap.getHeight();

if(width % 2 != 0){
   
	width -= 1;
}
if(height % 2 != 0){
   
	height -= 1;
}
byte[] imgData = getNV21(width, height, bitmap);

public static byte[] getNV21(int inputWidth, int inputHeight, Bitmap srcBitmap) {
   
        int[] argb = new int[inputWidth * inputHeight];
        if (null !=
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值