yuv420sp crop from any startpoint and width and height

25 篇文章 0 订阅



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

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

剪裁一个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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值