2013-10-11 18 views
7

Sto lavorando con AVCaptureVideoDataOutput e voglio convertire CMSampleBufferRef in UIImage. Molte risposte sono le stesse, come questo UIImage created from CMSampleBufferRef not displayed in UIImageView? e AVCaptureSession with multiple previewsConverti CMSampleBufferRef in UIImmagine con lo spazio colore YUV?

Funziona bene se ho impostato lo spazio colore VideoDataOutput a BGRA (accreditati a questa risposta CGBitmapContextCreateImage error)

NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
[dataOutput setVideoSettings:videoSettings]; 

Senza i videoSettings di cui sopra, io ricevo il seguenti errori

CGBitmapContextCreate: invalid data bytes/row: should be at least 2560 for 8 integer bits/component, 3 components, kCGImageAlphaPremultipliedFirst. 
<Error>: CGBitmapContextCreateImage: invalid context 0x0 

Lavorare con BGRA non è una buona scelta, poiché non v'è in testa conversione da YUV (spazio colore AVCaptureSession impostazione predefinita) per BGRA, come affermato da Brad e Codo i n How to get the Y component from CMSampleBuffer resulted from the AVCaptureSession?

Quindi c'è un modo per convertire CMSampleBufferRef in UIImage e lavorare con lo spazio colore YUV?

+0

Qualsiasi soluzione per questo? –

+0

@AdarshVC perché non voti la domanda per renderla più evidente? – onmyway133

+0

spunta questa domanda http://stackoverflow.com/questions/3305862/uiimage-created-from-cmsamplebufferref-not-displayed-in-uiimageview –

risposta

7

Dopo aver fatto un sacco di ricerche e aver letto i documenti Apple e wikipedis. Ho capito la risposta e funziona perfettamente per me. Così, per i futuri lettori Im condividere il codice per convertire CMSampleBufferRef-UIImage quando tipo di video pixel è impostato come kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

// Create a UIImage from sample buffer data 
// Works only if pixel format is kCVPixelFormatType_420YpCbCr8BiPlanarFullRange 
-(UIImage *) imageFromSamplePlanerPixelBuffer:(CMSampleBufferRef) sampleBuffer{ 

    @autoreleasepool { 
     // Get a CMSampleBuffer's Core Video image buffer for the media data 
     CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
     // Lock the base address of the pixel buffer 
     CVPixelBufferLockBaseAddress(imageBuffer, 0); 

     // Get the number of bytes per row for the plane pixel buffer 
     void *baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0); 

     // Get the number of bytes per row for the plane pixel buffer 
     size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0); 
     // Get the pixel buffer width and height 
     size_t width = CVPixelBufferGetWidth(imageBuffer); 
     size_t height = CVPixelBufferGetHeight(imageBuffer); 

     // Create a device-dependent gray color space 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 

     // Create a bitmap graphics context with the sample buffer data 
     CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
                bytesPerRow, colorSpace, kCGImageAlphaNone); 
     // Create a Quartz image from the pixel data in the bitmap graphics context 
     CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
     // Unlock the pixel buffer 
     CVPixelBufferUnlockBaseAddress(imageBuffer,0); 

     // Free up the context and color space 
     CGContextRelease(context); 
     CGColorSpaceRelease(colorSpace); 

     // Create an image object from the Quartz image 
     UIImage *image = [UIImage imageWithCGImage:quartzImage]; 

     // Release the Quartz image 
     CGImageRelease(quartzImage); 

     return (image); 
    } 
} 
+0

nota che l'immagine in uscita sarà in scala di grigi. – Bluewings

+0

E gli altri colori? – JULIIncognito

+0

@JULIIncognito modificare lo spazio colore in rgb – Bluewings

Problemi correlati