IOS攝像頭采集YUV420數據


這里簡單說下IOS攝像頭采集YUV420數據。

步驟: 

1.初始采集數據需要的對象

2.設置采集回調的代理並開始

3.在回調中獲取到采集的數據(注意:這里采集到的數據格式NV12並不是YUV420格式數據)

4.將NV12轉成我們需要的YUV420


代碼如下:

 //攝像頭采集

    capturSession=[[AVCaptureSessionalloc]init];

    

    capturSession.sessionPreset=AVCaptureSessionPreset1920x1080;//設置采集的分辨率

    captureDevice=[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error=nil;

    captureDeviceInput=[AVCaptureDeviceInputdeviceInputWithDevice:captureDeviceerror:&error];

    if (captureDeviceInput) {

        [capturSessionaddInput:captureDeviceInput];

    }else{

        NSLog(@"%@",error);

    }

    

    //初始化流輸出對象

    captureVideoDataOutput=[[AVCaptureVideoDataOutputalloc]init];

    [captureVideoDataOutputsetAlwaysDiscardsLateVideoFrames:YES];

    //設置輸出參數

    NSDictionary *settingsDic = [[NSDictionaryalloc]initWithObjectsAndKeys:

                                 [NSNumbernumberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange],

                                 kCVPixelBufferPixelFormatTypeKey,

                                 nil];// X264_CSP_NV12

    captureVideoDataOutput.videoSettings = settingsDic;

    

    dispatch_queue_t queue =dispatch_queue_create("myQueue",NULL);

    

    [captureVideoDataOutputsetSampleBufferDelegate:selfqueue:queue];

    [capturSessionaddOutput:captureVideoDataOutput];

    

    

    /* ---------------------------------------------------------------------- */

    previewLayer = [[AVCaptureVideoPreviewLayeralloc]initWithSession:capturSession];

    //設置顯示的視圖位置

    previewLayer.frame =CGRectMake(50,330,160,200);

    

    [self.view.layeraddSublayer:previewLayer];

    //開始采集

    [capturSessionstartRunning];



//回調函數


#pragma mark --  AVCaptureVideo(Audio)DataOutputSampleBufferDelegate method

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

{

    if (captureOutput == captureVideoDataOutput) {

        CVImageBufferRef imageBuffer =CMSampleBufferGetImageBuffer(sampleBuffer);

        

        // 獲取采集的數據

        CVPixelBufferLockBaseAddress(imageBuffer,0);

        CMTime pts =CMSampleBufferGetPresentationTimeStamp(sampleBuffer);

        CMTime duration =CMSampleBufferGetDuration(sampleBuffer);


        void *imageAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer,0);//YYYYYYYY

        size_t row0=CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0);

        

        void *imageAddress1=CVPixelBufferGetBaseAddressOfPlane(imageBuffer,1);//UVUVUVUV

        size_t row1=CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,1);

        

        size_t width =CVPixelBufferGetWidth(imageBuffer);

        size_t height =CVPixelBufferGetHeight(imageBuffer);

        

        CVPixelBufferUnlockBaseAddress(imageBuffer,0);

        size_t a=width*height;

        //開始將NV12轉換成YUV420

        uint8_t *yuv420Data=(uint8_t*)malloc(a*1.5);

        

        for (int i=0; i<height; ++i) {

            memcpy(yuv420Data+i*width, imageAddress+i*row0, width);

        }

        

        uint8_t *UV=imageAddress1;

        uint8_t *U=yuv420Data+a;

        uint8_t *V=U+a/4;

        for (int i=0; i<0.5*height; i++) {

            for (int j=0; j<0.5*width; j++) {

//                printf("%d\n",j<<1);

                *(U++)=UV[j<<1];

                *(V++)=UV[(j<<1)+1];

            }

            UV+=row1;

        }

        //這里根據自己的情況對YUV420數據進行處理

        

        

//...........

        //最后記得釋放哦

        free(yuv420Data);

        

    }

}




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com