//Video Capture Session init
- (BOOL)initVideoCapture
{
AVCaptureDeviceInput *captureInput =
[AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]
error:nil];
AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
[captureOutput setSampleBufferDelegate:self queue:NULL];
NSString *key = (NSString *)kCVPixelBufferPixelFormatTypeKey;
NSNumber *val = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary *videoSettings = [NSDictionary dictionaryWithObject:val forKey:key];
[captureOutput setVideoSettings:videoSettings];
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
[captureSession addInput:captureInput];
[captureSession addOutput:captureOutput];
[captureSession beginConfiguration];
[captureSession setSessionPreset:AVCaptureSessionPresetHigh];
[captureSession commitConfiguration];
[captureSession startRunning];
return YES;
}
//Video frame capture call back
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the image buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// Get information of the image
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create Colorspace
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
// Copy image data
/*
CFDataRef dataref = CGDataProviderCopyData(CGImageGetDataProvider(newImage));
memcpy(mImageData, dataref, 640 * 480 * sizeof(char) * 4);
CFRelease(dataref);
*/
UIImage *image = [UIImage imageWithCGImage:newImage];
[imageView setImage:image];
[imageView setFrame:CGRectMake(0, 0, width, height)];
// Release it
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
CGImageRelease(newImage);
// Unlock the image buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// CVBufferRelease(imageBuffer);
}
ref: http://forum.unity3d.com/viewtopic.php?p=300819
댓글 없음:
댓글 쓰기