我试图编写一个程序,作为其功能的一部分,连续捕获视频并实时计算给定帧的视频数据的平均亮度,或接近实时可能。这是我第一次涉足任何视频内容/ iOS相机的事情,所以除了我自己的东西之外,我还有很多东西只是在互联网上研究过的。现在我的ViewController.m文件中的代码将在我的设备上编译和运行,但它似乎没有做任何事情:
- (void)viewDidLoad{
[super viewDidLoad];
_val = 0;
//Set up the video capture session.
NSLog(@"Setting up the capture session...\n");
captureSession = [[AVCaptureSession alloc] init];
//Add input.
NSLog(@"Adding video input...\n");
AVCaptureDevice *captureDevice = [self frontFacingCameraIfAvailable];
if(captureDevice){
NSError *error;
videoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if(!error){
if([captureSession canAddInput:videoInputDevice])
[captureSession addInput:videoInputDevice];
else
NSLog(@"Couldn't add video input.\n");
}else{
NSLog(@"Couldn't create video input.\n");
}
}else{
NSLog(@"Couldn't create capture device.\n");
}
//Add output.
NSLog(@"Adding video data output...\n");
vidOutput = [[AVCaptureVideoDataOutput alloc] init];
vidOutput.alwaysDiscardsLateVideoFrames = YES;
if([captureSession canAddOutput:vidOutput])
[captureSession addOutput:vidOutput];
else
NSLog(@"Couldn't add video output.\n");
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[vidOutput setVideoSettings:videoSettings];
dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
[vidOutput setSampleBufferDelegate:self queue:queue];
}
- (void)viewDidUnload{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(AVCaptureDevice *)frontFacingCameraIfAvailable{
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *captureDevice = nil;
for (AVCaptureDevice *device in videoDevices){
if (device.position == AVCaptureDevicePositionFront){
captureDevice = device;
break;
}
}
//couldn't find one on the front, so just get the default video device.
if (!captureDevice){
captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return captureDevice;
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection: (AVCaptureConnection *)connection{
// Create autorelease pool because we are not in the main_queue
@autoreleasepool {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the imagebuffer
CVPixelBufferLockBaseAddress(imageBuffer,0);
// Get information about the image
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
// size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
CVPlanarPixelBufferInfo_YCbCrBiPlanar *bufferInfo = (CVPlanarPixelBufferInfo_YCbCrBiPlanar *)baseAddress;
// This just moved the pointer past the offset
baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
// convert the image
UIImage *image = [self makeImage:baseAddress bufferInfo:bufferInfo width:width height:height bytesPerRow:bytesPerRow];
// Update the display with the captured image for DEBUG purposes
//dispatch_async(dispatch_get_main_queue(), ^{
//[self.vImage setImage:image];
//});
CGImageRef cgImage = [image CGImage];
CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
CFDataRef bitmapData = CGDataProviderCopyData(provider);
const UInt8* data = CFDataGetBytePtr(bitmapData);
int cols = width - 1;
int rows = height - 1;
float avgLuminance = 0.0;
for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
const UInt8* pixel = data + j*bytesPerRow + i*4;
avgLuminance += pixel[0]*0.299 + pixel[1]*0.587 + pixel[2]*0.114;
}
}
avgLuminance /= (cols*rows);
NSLog(@"Average Luminance: %f\n", avgLuminance);
}
}
-(UIImage *)makeImage:(uint8_t *)inBaseAddress bufferInfo:(CVPlanarPixelBufferInfo_YCbCrBiPlanar *)inBufferInfo width: (size_t)Width height:(size_t)Height bytesPerRow:(size_t)BytesPerRow{
NSUInteger yPitch = EndianU32_BtoN(inBufferInfo->componentInfoY.rowBytes);
uint8_t *rgbBuffer = (uint8_t *)malloc(Width * Height * 4);
uint8_t *yBuffer = (uint8_t *)inBaseAddress;
uint8_t val;
int bytesPerPixel = 4;
// for each byte in the input buffer, fill in the output buffer with four bytes
// the first byte is the Alpha channel, then the next three contain the same
// value of the input buffer
for(int y = 0; y < Height*Width; y++){
val = yBuffer[y];
// Alpha channel
rgbBuffer[(y*bytesPerPixel)] = 0xff;
// next three bytes same as input
rgbBuffer[(y*bytesPerPixel)+1] = rgbBuffer[(y*bytesPerPixel)+2] = rgbBuffer[y*bytesPerPixel+3] = val;
}
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbBuffer, yPitch, Height, 8,yPitch*bytesPerPixel, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
//UIImage *image = [[UIImage alloc] initWithCGImage:quartzImage scale:(CGFloat)0.5 orientation:UIImageOrientationRight];
UIImage *image = [UIImage imageWithCGImage:quartzImage];
CGImageRelease(quartzImage);
free(rgbBuffer);
return image;
}
我将.h文件设置为AVCaptureVideoDataOutputSampleBufferDelegate
,但我觉得我并不完全理解我需要在代码中做什么来不断从相机获取数据,如CaptureOutput方法永远不会被调用。我应该如何/在哪里调用它以获得恒定的数据流?
答案 0 :(得分:1)
[captureSession startRunning];
想象一下。嘿。