android I420转NV21 , NV21转I420

    /**
     * I420转nv21
     */
    public static byte[] I420Tonv21(byte[] data, int width, int height) {
        byte[] ret = new byte[data.length];
        int total = width * height;
        
        ByteBuffer bufferY = ByteBuffer.wrap(ret, 0, total);
        ByteBuffer bufferVU = ByteBuffer.wrap(ret, total, total / 2);
        
        bufferY.put(data, 0, total);
        for (int i = 0; i < total / 4; i += 1) {
            bufferVU.put(data[i + total + total / 4]);
            bufferVU.put(data[total + i]);
        }
        
        return ret;
    }
	
	
	/**
	 * nv21转I420
	 * @param data 
	 * @param width
	 * @param height
	 * @return
	 */
	public static byte[] nv21ToI420(byte[] data, int width, int height) {  
	    byte[] ret = new byte[data.length];  
	    int total = width * height;  
	      
	    ByteBuffer bufferY = ByteBuffer.wrap(ret, 0, total);  
	    ByteBuffer bufferU = ByteBuffer.wrap(ret, total, total / 4);  
	    ByteBuffer bufferV = ByteBuffer.wrap(ret, total + total / 4, total / 4);  
	      
	    bufferY.put(data, 0, total);  
	    for (int i=total; i<data.length; i+=2) {  
	        bufferV.put(data[i]);  
	        bufferU.put(data[i+1]);  
	    }  
	      
	    return ret;  
	}  
	
	
	
	public static void main(String[] args) {
		//模拟nv21 数据    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2]
		//模拟I420 数据    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3]
			byte[] data=new byte[30];
			for(int i=0;i<20;i++){
				data[i]=1;
			}
			
			for(int i=0;i<10;i++){
				if(i%2==0){
					data[i+20]=3;
				}else{
					data[i+20]=2;
				}
			}
			
			System.out.println("NV21 数据  --> "+Arrays.toString(data));
			byte[] i420data=nv21ToI420(data,5,4);
			System.out.println("I420 数据  --> "+Arrays.toString(i420data));
			byte[] nv21data=I420Tonv21(i420data,5,4);
			System.out.println("NV21 数据  --> "+Arrays.toString(nv21data));
	}
	

一张图片的大小为widthheight, YUV420图片数据的大小就是widthheight*3/2

NV21 数据格式:
widthheight个字节存的是每个像素的Y分量,后面的widthheight/2字节是VUVUVUVU…
如: YYYY…YYVUVUVUVU…

I420 是数据格式:
widthheight个字节还是每个像素的Y分量,接下来的widthheight/4字节是U分量,最后的width*height/4字节是V分量
如: YYYY…YYUUUUVVVV…

模拟数据测试结果如下:
其中: 1表示是Y, 2表示是U ,3表示是V

NV21 数据 --> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2]
I420 数据 --> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3]
NV21 数据 --> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值