项目中用到,用来对YUV数据(图片的yuv或者视频单帧yuv数据)进行裁剪。

格式介绍:http://blog.csdn.net/vblittleboy/article/details/10945143

import android.graphics.RectF;
import android.util.Log; /**
* 用来裁剪yuv图像
* @author willhua
*
*/
public class YuvCroper {
private static String LOG_TAG = "VideoCrop";
public static int YUV_420SP = 0; //也即NV12 YYY...UVUVUV...
public static int YUV_420P = 1; //也即I420 YYY...UUU...VVV... private final int mType;
private byte[] mData;
private final int mWidth;
private final int mHeight;
private final int mYLenght;
private final int mCropLeft;
private final int mCropTop;
private int mCropWidth;
private int mCropHeight; /**
*
* @param type yuv数据类型:YUV_SP() YUV_420P()
* @param width yuv数据的原始宽度
* @param height yuv数据的原始高度
* @param crop 裁剪的区域。比如(0.25,0,1,0.85),每个值都在0-1之间
* @throws Exception
*/
public YuvCroper(int type, int width, int height, RectF crop) throws Exception{
if(width <= 0 || height <= 0 || crop == null || crop.width() <= 0 || crop.height() <= 0){
throw new Exception("ViewCrop:Wrong param! size:" + width + "*" + height + ". Crop:"+ crop);
}
mType = type;
mWidth = width;
mHeight = height;
mYLenght = mWidth * mHeight;
mCropHeight = (int)(crop.height() * mHeight);
mCropWidth = (int)(crop.width() * mWidth);
mCropHeight = roundTo16(mCropHeight, mHeight);
mCropWidth = roundTo16(mCropWidth, mWidth);
mCropLeft = (int)(crop.left * mWidth) / 16 * 16;
mCropTop = (int)(crop.top * mHeight) / 16 * 16;
mData = new byte[mCropHeight * mCropWidth * 3 / 2];
Log.d(LOG_TAG, "crop point:" + mCropLeft + "*" + mCropTop + " size:" + mCropWidth + "*" + mCropHeight);
} /**
* 单帧yuv数据
* @param oriData
* @return
*/
public byte[] crop(byte[] oriData){
if(oriData == null || oriData.length != mYLenght * 3 / 2){
Log.d(LOG_TAG, "crop:wrong oriData!!!");
return mData;
}
if(mType == YUV_420SP){
processYuvsp(oriData);
}else if(mType == YUV_420P){
processYuv420p(oriData);
}
return mData;
} private void processYuvsp(byte[] oriData){
//copy y;
int index = 0;
int start = mCropLeft + mWidth * mCropTop;
int end = mWidth * (mCropTop + mCropHeight);
int oriStep = mWidth;
int newstep = mCropWidth;
index = copy(oriData, start, end, oriStep, newstep, index); //copy uv
start = mYLenght + mWidth * mCropTop / 2 + mCropLeft;
end = mYLenght + mWidth * (mCropTop + mCropHeight) / 2;
oriStep = mWidth;
newstep = mCropWidth;
copy(oriData, start, end, oriStep, newstep, index);
} private void processYuv420p(byte[] oriData){
int index = 0;
int start = mCropLeft + mCropTop * mWidth;
int end = mWidth * (mCropTop + mCropHeight);
int oriStep = mWidth;
int newstep = mCropWidth;
//copy y;
index = copy(oriData, start, end, oriStep, newstep, index); //copy u
start = mYLenght + (mWidth * mCropTop / 4 + mCropLeft / 2) ;
end = mYLenght + mWidth * (mCropTop + mCropHeight) / 4;
oriStep = mWidth / 2;
newstep = mCropWidth / 2;
index = copy(oriData, start, end, oriStep, newstep, index); //copy v
start = mYLenght / 4 * 5 + mWidth * mCropTop / 4 + mCropLeft / 2;
end = mYLenght / 4 * 5 + mWidth * (mCropTop + mCropHeight) / 4;
copy(oriData, start, end, oriStep, newstep, index);
} private int copy(byte[] oriData, int start, int end, int oriStep, int newstep, int index){
for(int i = start; i < end; i += oriStep){
System.arraycopy(oriData, i, mData, index, newstep);
index += newstep;
}
return index;
} /**
* 对于高通或者MTK平台,只支持宽高为16的整数备的数据
* @param size
* @param limit
* @return
*/
private int roundTo16(int size, int limit){
if(size >= limit){
return limit;
}
float f = size / 16f;
int m;
if(f - (int)f > 0.5f){
m = ((int)(f + 0.5)) * 16;
}else{
m = (int)f * 16;
}
return m < 16 ? 16 : m;
} }

对YUV数据进行裁剪的更多相关文章

  1. ffmpeg从AVFrame取出yuv数据到保存到char*中

    ffmpeg从AVFrame取出yuv数据到保存到char*中   很多人一直不知道怎么利用ffmpeg从AVFrame取出yuv数据到保存到char*中,下面代码将yuv420p和yuv422p的数 ...

  2. 入门视频采集与处理(学会分析YUV数据)

    做视频采集与处理,自然少不了要学会分析YUV数据.因为从采集的角度来说,一般的视频采集芯片输出的码流一般都是YUV数据流的形式,而从视频处理(例如H.264.MPEG视频编解码)的角度来说,也是在原始 ...

  3. Android上使用OpenGLES2.0显示YUV数据

    在Android上用OpenGLES来显示YUV图像,之所以这样做,是因为: 1.Android本身也不能直接显示YUV图像,YUV转成RGB还是必要的: 2.YUV手动转RGB会占用大量的CPU资源 ...

  4. Android用surface直接显示yuv数据(二)

    上一篇文章主要是參照AwesomePlayer直接用SoftwareRenderer类来显示yuv,为了能用到这个类,不惜依赖了libstagefright.libstagefright_color_ ...

  5. Android使用surface直接显示yuv数据(三)

    在本文中,Java创建UI和关节JNI经营层surface直接显示yuv数据(yv12).发展环境Android 4.4,驰A23平台. package com.example.myyuvviewer ...

  6. 学会分析YUV数据

    做视频采集与处理,自然少不了要学会分析YUV数据.因为从采集的角度来说,一般的视频采集芯片输出的码流一般都是YUV数据流的形式,而从视频处理(例如H.264.MPEG视频编解码)的角度来说,也是在原始 ...

  7. Android camera2 回调imagereader 从Image拿到YUV数据转化成RGB,生成bitmap并保存

    ImageUtil.java import android.graphics.ImageFormat; import android.media.Image; import android.os.Bu ...

  8. (转) 从ffmpeg中提取出YUV数据

    有时需要从ffmpeg中提取出YUV数据用作预览,另存什么的. ffmpeg是先解码成YUV, 再以这个YUV作为输入进行编码,所以YUV数据有两种:  解码后的YUV数据, 以及  编码重建的YUV ...

  9. ffmpeg最简单的解码保存YUV数据 <转>

    video的raw data一般都是YUV420p的格式,简单的记录下这个格式的细节,如有不对希望大家能指出.   YUV图像通常有两种格式,一种是packet 还有一种是planar    从字面上 ...

随机推荐

  1. 机器学习入门04 - 使用TensorFlow的起始步骤 (First Steps with TensorFlow)

    原文链接:https://developers.google.com/machine-learning/crash-course/first-steps-with-tensorflow/ 1- 工具包 ...

  2. 微信小程序内嵌网页的一些(最佳)实践

    前言 3 个月前,微信小程序推出了 web-view 组件引发了一波小高潮,笔者所在的大前端团队写过一篇浅析,详情可见:浅谈微信小程序前端生态. 我们曾大胆猜想,这一功能,可能直接导致小程序数量增长迎 ...

  3. python元组和字典的简单学习

    元组(tuple) 用圆括号()标识,定义元组后,元组元素不可修改.如果想修改元组只能重新定义元组. 因为元组不可更改,所以也没有增删改等用法,主要语法就是访问元组元素,遍历元组. 访问元组元素: t ...

  4. 使用 Kubeadm 安装部署 Kubernetes 1.12.1 集群

    手工搭建 Kubernetes 集群是一件很繁琐的事情,为了简化这些操作,就产生了很多安装配置工具,如 Kubeadm ,Kubespray,RKE 等组件,我最终选择了官方的 Kubeadm 主要是 ...

  5. JVM学习(一)、垃圾收集器简介

    一.垃圾收集算法 (1)标记-清除算法:最基础的收集算法“标记--清除”(Mark-sweep)算法,算法分为“标记”和“清除”两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收所有被标记的 ...

  6. 遇到的一些Jquery,js函数

     jQuery.extend()        jQuery.merge():函数用于合并两个数组内容到第一个数组. <script> $(function () { ,,], [,,] ...

  7. SpringBoot2.0源码分析(三):整合RabbitMQ分析

    SpringBoot具体整合rabbitMQ可参考:SpringBoot2.0应用(三):SpringBoot2.0整合RabbitMQ RabbitMQ自动注入 当项目中存在org.springfr ...

  8. 基于Consul的数据库高可用架构

    几个月没有更新博客了,已经长草了,特意来除草.本次主要分享如何利用consul来实现redis以及mysql的高可用.以前的公司mysql是单机单实例,高可用MHA加vip就能搞定,新公司mysql是 ...

  9. Python数据可视化的四种简易方法

    摘要: 本文讲述了热图.二维密度图.蜘蛛图.树形图这四种Python数据可视化方法. 数据可视化是任何数据科学或机器学习项目的一个重要组成部分.人们常常会从探索数据分析(EDA)开始,来深入了解数据, ...

  10. man exportfs(exportfs命令中文手册)

    本人译作集合:http://www.cnblogs.com/f-ck-need-u/p/7048359.html exportfs() System Manager's Manual exportfs ...