yuv420sp crop from any startpoint and width and height

来源:互联网 发布:高德导航端口检测工具 编辑:程序博客网 时间:2024/06/05 14:11



做图像处理的时候,经常需要进行相关图像的裁剪,旋转,复制等处理,

前文有一些这方面的文档,但是不适合我的要求,我的要求是:

剪裁一个yuv420sp 的数据,调试了一段时间,现把相关代码分享出来。

当初有一个坑,就是在x_start   ,y_start/2的时候写成x_start>>1,y_start>>1 的时候,出现错误,后来改为x_start   ,y_start/2 问题就好了。

当然也可以把yBuf,uvBuf 合为一个大buf,这样就是crop 出一个完整的yuv420sp数据,由于我正好需要分开出来y,uv 分量,所以我做了两个buf。


 ByteBuffer yBuf; ByteBuffer uvBuf;public void cropYUV420sp(byte[] src,int src_width,int src_height,int x_start,int y_start,int dst_width,int dst_height){      int oldyuvlen = src_width*src_height;        yBuf = ByteBuffer.allocateDirect(dst_height*dst_width);    yBuf.order(ByteOrder.nativeOrder()).position(0);    uvBuf = ByteBuffer.allocateDirect(dst_height*dst_width/2);    uvBuf.order(ByteOrder.nativeOrder()).position(0);           for(int i=0;i<dst_height;i++)        yBuf.put(src,(x_start)+(y_start+i)*src_width,dst_width);    for(int i=0;i<dst_height>>1;i++)    {        uvBuf.put(src,(y_start/2 +i)*src_width+x_start+oldyuvlen,dst_width);    }    yBuf.position(0);    uvBuf.position(0);}

阅读全文
0 0
原创粉丝点击