无法使AVFoundation与AVCaptureSessionPresetPhoto解决方案一起使用

时间:2012-10-03 17:35:27

标签: image-processing opencv uiimage avfoundation avcapturesession

我似乎无法在AVCaptureSessionPresetPhoto分辨率下使用AVFoundation进行像素对齐。像素对齐可以在较低分辨率下正常工作,如AVCaptureSessionPreset1280x720(AVCaptureSessionPreset1280x720_Picture)。AVCaptureSessionPreset1280x720_picture AVCaptureSessionPresetPhoto_picture

具体来说,当我取消注释这些行时:     if([captureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]){         [captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
    } else {         NSLog(@"无法将分辨率设置为AVCaptureSessionPresetPhoto");     } 我得到一个错过的对齐图像,如下面的第二张图所示。任何意见/建议都非常感谢。

这是我的代码,用于设置1)捕获会话,2)委托回调,以及3)保存一个蒸汽图像以验证像素对齐。
1.捕获会话设置

    - (void)InitCaptureSession {
captureSession = [[AVCaptureSession alloc] init];
if ([captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {
    [captureSession setSessionPreset:AVCaptureSessionPreset1280x720];        
} else {
    NSLog(@"Unable to set resolution to AVCaptureSessionPreset1280x720");
}

//    if ([captureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) {
//        [captureSession setSessionPreset:AVCaptureSessionPresetPhoto];        
//    } else {
//        NSLog(@"Unable to set resolution to AVCaptureSessionPresetPhoto");
//    }

captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
videoInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil];

AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES; 

dispatch_queue_t queue;
queue = dispatch_queue_create("cameraQueue", NULL);
[captureOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
    NSNumber* value = [NSNumber     numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];     
[captureOutput setVideoSettings:videoSettings]; 

[captureSession addInput:videoInput];
[captureSession addOutput:captureOutput];
    [captureOutput release];

    previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    CALayer *rootLayer = [previewView layer];// self.view.layer; //

[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:[rootLayer bounds]];
[rootLayer addSublayer:previewLayer];
[captureSession startRunning];  
}


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

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
static int processedImage = 0;
processedImage++;
if (processedImage==100) {
    [self SaveImage:sampleBuffer]; 
}

[pool drain];
} 

// Create a UIImage CMSampleBufferRef and save for verifying pixel alignment
- (void) SaveImage:(CMSampleBufferRef) sampleBuffer 
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
CVPixelBufferLockBaseAddress(imageBuffer, 0); 
CvSize imageSize;
imageSize.width = CVPixelBufferGetWidth(imageBuffer); ;
imageSize.height = CVPixelBufferGetHeight(imageBuffer); 
IplImage *image = cvCreateImage(imageSize, IPL_DEPTH_8U, 1);     
void *y_channel = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0); 
char *tempPointer = image->imageData;
memcpy(tempPointer, y_channel, image->imageSize);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGImageRef imageRef = CGImageCreate(image->width, image->height,
                                    8, 8, image->width,
                                    colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault,
                                    provider, NULL, false, kCGRenderingIntentDefault);
UIImage *Saveimage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
UIImageWriteToSavedPhotosAlbum(Saveimage, nil, nil, nil);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}

1 个答案:

答案 0 :(得分:3)

SaveImage内,CGImageCreate的第五个参数是bytesPerRow,您不应该传递image->width,因为在内存对齐的情况下,每行的字节数可能不同。这是AVCaptureSessionPresetPhoto width = 852(带有iPhone 4摄像头)的情况,而1-st平面(Y)的每行字节数是864,因为它是size_t bpr = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0); 16字节对齐。

1 /你应该得到每行的字节数如下:

IplImage

2 /然后在将像素复制到char *y_channel = (char *) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0); // row by row copy for (int i = 0; i < image->height; i++) memcpy(tempPointer + i*image->widthStep, y_channel + i*bpr, image->width); 时,请注意每行的字节数:

[NSData dataWithBytes:image->imageData length:image->imageSize];

您可以保持image->imageSize,因为imageSize = height*widthStep考虑了对齐(IplImage)。

3 /最后将CGImageCreate宽度步长作为CGImageCreate(image->width, image->height, 8, 8, image->widthStep, ...); 第5个参数传递:

{{1}}