2012-07-11 50 views
1

我正在使用captureOutput:didOutputSampleBuffer:fromConnection:代理方法AVCaptureVideoDataOutput。在iPad上测试时,图像缓冲区大小始终为360x480,这看起来很奇怪,我认为这应该是iPad屏幕的大小。captureOutput:didOutputSampleBuffer:fromConnection:即使在iPad上,图像缓冲区大小始终为360x480

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

    @autoreleasepool { 

     CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
     /*Lock the image buffer*/ 
     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); 

     /*Create a CGImageRef from the CVImageBufferRef*/ 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
     CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
     CGImageRef newImage = CGBitmapContextCreateImage(newContext); 

     NSLog(@"image size: h %zu, w %zu", height, width); 

     /*We unlock the image buffer*/ 
     CVPixelBufferUnlockBaseAddress(imageBuffer,0); 

     CGRect zoom = CGRectMake(self.touchPoint.y, self.touchPoint.x, 120, 120); 
     CGImageRef newImage2 = CGImageCreateWithImageInRect(newImage, zoom); 

     /*We release some components*/ 
     CGContextRelease(newContext); 
     CGColorSpaceRelease(colorSpace); 

     UIImage* zoomedImage = [[UIImage alloc] initWithCGImage:newImage2 scale:1.0 orientation:UIImageOrientationUp]; 
     [self.zoomedView.layer performSelectorOnMainThread:@selector(setContents:) withObject:(__bridge id)zoomedImage.CGImage waitUntilDone:YES]; 

     CGImageRelease(newImage); 
     CGImageRelease(newImage2); 

    } 

}//end 

即使在iPad上,图像缓冲区会如此之小,是否有原因?

回答

2

AVCaptureSession的质量由sessionPreset属性确定,该属性默认为AVCaptureSessionPresetHigh。它并不关心捕捉设备上屏幕的分辨率是多少;捕捉质量是设备相机的功能。

如果您希望捕捉分辨率更接近地匹配屏幕分辨率,则必须更改sessionPreset。只要注意,没有任何预设的直接对应于任何屏幕分辨率,而它们对应于常见的视频格式,如VGA,720P,1080P等:

NSString *const AVCaptureSessionPresetPhoto; 
NSString *const AVCaptureSessionPresetHigh; 
NSString *const AVCaptureSessionPresetMedium; 
NSString *const AVCaptureSessionPresetLow; 
NSString *const AVCaptureSessionPreset352x288; 
NSString *const AVCaptureSessionPreset640x480; 
NSString *const AVCaptureSessionPreset1280x720; 
NSString *const AVCaptureSessionPreset1920x1080; 
NSString *const AVCaptureSessionPresetiFrame960x540; 
NSString *const AVCaptureSessionPresetiFrame1280x720; 
+0

啊是有道理的。你知道前置摄像头可以发送到图像缓冲器的最大分辨率吗? – 2012-07-12 22:56:35

+0

@NicHubbard它会在不同的设备上有所不同。较新的相机可以在较高的分辨率下捕捉。并非所有这些预设都适用于所有捕捉源。我确定有一个列表,但我不知道它... – 2012-07-12 23:24:38

+0

谢谢,感谢帮助。 – 2012-07-13 00:21:10