Crop YUV data and YUV data

Source: Internet
Author: User

Crop YUV data and YUV data

Used in the project to crop YUV data (the yuv of an image or the yuv data of a single video frame.

Format Introduction: http://blog.csdn.net/vblittleboy/article/details/10945143

Import android. graphics. rectF; import android. util. log;/*** used to crop yuv images * @ author willhua **/public class YuvCroper {private static String LOG_TAG = "VideoCrop"; public static int YUV_420SP = 0; // that is, NV12 YYY... UVUVUV... public static int YUV_420P = 1; // that is, I420 YYY... UUU... VVV... private final int mType; private byte [] mData; private final int mWidth; private final int mHeight; private final int mYL Enght; private final int mCropLeft; private final int mCropTop; private int mCropWidth; private int mCropHeight;/***** @ param type yuv data type: YUV_SP () YUV_420P () * @ param width the original width of yuv data * @ param height the original height of yuv data * @ param crop the cropped area. For example (0.25, 0.85,), each value is between 0 and 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 );} /*** single-frame yuv data * @ 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 = mYL Enght/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;}/*** for Qualcomm or MTK platforms, only data in integer backup with a width of 16 * @ param size * @ param limit * @ re Turn */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 ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.