做图像处理的时候,经常需要进行相关图像的裁剪,旋转,复制等处理,
前文有一些这方面的文档,但是不适合我的要求,我的要求是:
剪裁一个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); }