美文网首页
iOS YUV(i420) to UIImage

iOS YUV(i420) to UIImage

作者: Raining7 | 来源:发表于2019-03-29 17:58 被阅读0次
        思路:YUV(i420) -> YUV(NV12) -> CIImage -> UIImage
    
            获取到的YUV Data格式:
                    (int width, 
                     int height, 
                     unsigned long long time_stamp,
                     unsigned char*yData, 
                     unsigned char* uData, 
                     unsigned char*vData,
                     int yStride, 
                     int uStride, 
                     int vStride)
    

    将YUV分量填入buffer 得到标准buffer
    I420: YYYYYYYY UU VV -> NV12: YYYYYYYY UVUV
    附上YUV图解:https://blog.csdn.net/xjhhjx/article/details/80291465

            NSDictionary *pixelAttributes = @{(NSString *)kCVPixelBufferIOSurfacePropertiesKey:@{}};
            CVPixelBufferRef pixelBuffer = NULL;
            CVReturn result = CVPixelBufferCreate(kCFAllocatorDefault,
                                                  width,
                                                  height,
                                                  kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,   //  NV12
                                                  (__bridge CFDictionaryRef)(pixelAttributes),
                                                  &pixelBuffer);
            if (result != kCVReturnSuccess) {
                NSLog(@"Unable to create cvpixelbuffer %d", result);
            }
            CVPixelBufferLockBaseAddress(pixelBuffer, 0);
            unsigned char *yDestPlane = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
            for (int i = 0, k = 0; i < height; i ++) {
                for (int j = 0; j < width; j ++) {
                    yDestPlane[k++] = yData[j + i * yStride]; 
                }
            }
            unsigned char *uvDestPlane = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
            for (int i = 0, k = 0; i < height / 2; i ++) {
                for (int j = 0; j < width / 2; j ++) {
                    uvDestPlane[k++] = uData[j + i * uStride]; 
                    uvDestPlane[k++] = vData[j + i * vStride]; 
                }
            }
    
    CIImage Conversion
            CIImage *coreImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
            CIContext *MytemporaryContext = [CIContext contextWithOptions:nil];
            CGImageRef MyvideoImage = [MytemporaryContext createCGImage:coreImage
                                                               fromRect:CGRectMake(0, 0, width, height)];
    
    UIImage Conversion
            UIImage *Mynnnimage = [[UIImage alloc] initWithCGImage:MyvideoImage
                                                             scale:1.0
                                                       orientation:UIImageOrientationUp];
            CVPixelBufferRelease(pixelBuffer);
            CGImageRelease(MyvideoImage);
    

    相关文章

      网友评论

          本文标题:iOS YUV(i420) to UIImage

          本文链接:https://www.haomeiwen.com/subject/vycrbqtx.html